我最近遇到一个问题,我正在开发一些 PowerShell 脚本来管理和更新一些数字显示器。请参阅下面我正在进行的工作(尚未完成)。
我正在努力能够通过终端或任何方式(自动文本文件或通过命令)输出活动或当前打开的 Chromium 选项卡。我想要实现的想法是,如果当前选项卡对应于需要打开的选项卡,则跳过强制刷新(关闭和打开 Chromium)。
我尝试过 BroTab,但根本无法让它工作,似乎是 Ubuntu 和 Chromium 的问题。有谁知道有什么可以产生这种影响吗?
我已经能够处理打开的活动选项卡的历史记录,但这仅在手动打开浏览器而不是通过终端打开时更新。
当出现故障时,脚本需要工作和错误记录/电子邮件通知,以便可以对屏幕进行物理调查。第一步是让这个工作起来……当它每 30 分钟刷新一次时,它的表现就很丑陋——我宁愿这只是作为一种保持活动的方式。我知道这需要工作,因此对所有代码的建议持开放态度。我也会使用函数,但也很难让它们按照我的意愿工作......
我期待您的建议。
##SCRIPT TO RERESH AND MANAGE PI BOXES FOR DISPLAYS##
$getr=$null;
##SET VARIABLES TO GET DATES AND FOR SPECIFIC TIMES IN REGARDS TO UPDATES
$Calendar = Get-Date
$updatestart = Get-Date -Hour 11 -Minute 55
$updatecutoff = Get-Date -Hour 12 -Minute 01
##SET VARIABLES FOR PI INSTRUCTIONS
$exitchrome = "killall -9 chromium-browser &>/dev/null; exit;" ##QUITS BROWSER AND EXITS SCRIPT - HAS TO BE DONE AS CANNOT BE RUN WITH FOLLWOING COMMANDS
Get-Content -Path "C:\temp\ip.txt" | Foreach-Object { ##LOOP THROUGH CONTENTS OF TEXT FILE - IP.TXT
$textfile = $_;
$pos = $textfile.IndexOf("|") ## LOOK FOR PIPE CHARECTER IN TEXT FILE
$link_string = $textfile.Substring($pos+1) ##CONTENTS AFTER PIPE CHARECTER
$ip = $textfile.Substring(0, $pos) ##CONTENTS BEFORE PIPE CHARECTER
echo $ip
$chrome = "DISPLAY=:0 chromium-browser " + $link_string + " --kiosk --disable-session-crashed-bubble --disable-infobars &> /dev/null &" ##STARTS BROWSER IN KIOSK MODE ON DESKTOP WITH THE DEFINED URLS - ALL ON SCREEN MESSAGES DISABLED
##PLAY CODE##
$i = 0
$c = 0
$w = 1
$wb_chk = "cat -e ~/.config/chromium/Default/Sessions/Tabs_* | sed -e 's/\^@/\n/g' -e '/_\/chrome/d' | grep -Po '(http|https)://\K.*' | sort -u" ##CHECK BROWSER FOR OPEN TABS ASSIGNED TO VARIABLE
$wb_chk_res = ssh orangepi@"$ip" -X "$wb_chk" | Out-String
foreach($url_array in -split $link_string)
{
$url_array_pos = $url_array.IndexOf("/") ## LOOK FOR CHARECTER RETRUN IN VARIABLE
$url_array = $url_array.Substring($url_array_pos+2) ##REMOVE CONTENTS AFTER CHARECTER RETRUN
$i++
foreach($wbl_chk in -split $wb_chk_res)
{
$w++
if ($wbl_chk -eq $url_array) {
$c++
}
}
}
echo "$w websites were checked"
echo "There are $c live on the display"
echo "$c websites match the $i URLS in the config file"
##PLAY CODE##
If ($Calendar.Day -eq '28' -AND $Calendar.TimeOfDay -gt $updatestart.TimeOfDay -AND $Calendar.TimeOfDay -lt $updatecutoff.TimeOfDay) { ##IF END OF THE MONTH, RUN UPDATES
ssh -p 222 pi@"$ip" -X "sudo apt-get update; sudo apt-get upgrade -y; sudo reboot;"
Start-Sleep -s 60
}
##CEHCK DISPLAY RES - CANNOT TO GET TO WORK AS A FUCNTION...
$getr = ssh orangepi@"$ip" -X "xrandr -display :0.0;" | Out-String
$res_pos = $getr.IndexOf(",") ## LOOK FOR COMMA CHARECTER IN VARIABLE
$getr = $getr.Substring($res_pos+2) ##REMOVE CONTENTS AFTER COMMA CHARECTER
$res_pos = $getr.IndexOf(",") ## LOOK FOR COMMA CHARECTER IN VARIABLE
$getr = $getr.Substring(0, $res_pos) ##REMOVE CONTENTS BEFORE COMMA CHARECTER
$res_pos = $getr.IndexOf(" ") ## LOOK FOR CHARECTER RETRUN IN VARIABLE
$getr = $getr.Substring($res_pos+1) ##REMOVE CONTENTS AFTER CHARECTER RETRUN
$uptime = ssh orangepi@"$ip" -X "awk '{print $1}' /proc/uptime" | Out-String
$up_pos = $uptime.IndexOf(" ") ## LOOK FOR CHARECTER RETRUN IN VARIABLE
$uptime = $uptime.Substring(0,$up_pos) ##CONTENTS AFTER CHARECTER RETRUN
echo $uptime
echo $getr
if ($uptime -lt "1800.00" -Or $getr -ne "1920 x 1080") {
echo "Forcing Display Resolution";
ssh orangepi@"$ip" -X "sudo service lightdm restart;"
Start-Sleep -s 10;
ssh orangepi@"$ip" -X "$chrome";
Start-Sleep -s 10;
ssh orangepi@"$ip" -X "$exitchrome";
Start-Sleep -s 10;
$getr=$null;
}
$MATICS = select-string -pattern "xxx.my.leadermes.com" -InputObject $link_string ##LOOK FOR MATICS URL FOR SYNTHEIC MOUSE CLICKS
if ($MATICS -ne $null)
{
$getr = ssh orangepi@"$ip" -X "xrandr -display :0.0;" | Out-String
$res_pos = $getr.IndexOf(",") ## LOOK FOR COMMA CHARECTER IN VARIABLE
$getr = $getr.Substring($res_pos+2) ##CONTENTS AFTER COMMA CHARECTER
$res_pos = $getr.IndexOf(",") ## LOOK FOR COMMA CHARECTER IN VARIABLE
$getr = $getr.Substring(0, $res_pos) ##CONTENTS BEFORE COMMA CHARECTER
$res_pos = $getr.IndexOf(" ") ## LOOK FOR CHARECTER RETRUN IN VARIABLE
$getr = $getr.Substring($res_pos+1) ##CONTENTS AFTER CHARECTER RETRUN
echo "MATICS ON DISPLAY";
ssh orangepi@"$ip" -X "$exitchrome";
ssh orangepi@"$ip" -X "$chrome";
if ($getr -eq "1920 x 1080") {
echo "$getr live";
Start-Sleep -s 15
ssh orangepi@"$ip" -X "DISPLAY=:0 xdotool key Return; exit;"
Start-Sleep -s 20
ssh orangepi@"$ip" -X "DISPLAY=:0 xdotool key Tab Tab space mousemove 220 60 click 1; exit;"
Start-Sleep -s 20
ssh orangepi@"$ip" -X "DISPLAY=:0 xdotool mousemove 1860 180 click 1 mousemove 1920 1080; exit;"
$getr=$null;
}
}
else
{
echo "MATICS IS NOT ON THIS DISPLAY";
ssh orangepi@"$ip" -X "$exitchrome";
ssh orangepi@"$ip" -X "$chrome";
}
}
我想我再次找到了自己问题的答案...尽管我看起来没有办法在 Chrome 或其他网络浏览器中快速访问打开的 URL...这可以被视为安全风险。
我的解决方法是循环遍历每个打开的窗口(完全感谢,只是 Chrome,但这也可以在代码中排除)并循环遍历选项卡以一次获取它们。命令 wmctrl -l 帮助我实现了这一点。
我仍然怀疑可能有更好的方法,但这就是我们现在正在采用的方法。
该代码仍在工作中,如下所示: