You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Orange Pi 5终端获取Kiosk模式Chromium的打开标签页(PowerShell脚本开发场景)

在Orange Pi 5终端获取Kiosk模式Chromium的打开标签页(PowerShell脚本开发场景)

I get where you're coming from with wanting to avoid those jarring Chromium refreshes every 30 minutes—keeping things running smoothly as a "keep alive" makes way more sense. Let's tackle the core issue first: reliably fetching active tabs from Chromium when it's running in kiosk mode on your Orange Pi, then go over some script improvements you can make.

解决标签页检测问题:使用Chromium远程调试协议

BroTab can be finicky on Ubuntu/Chromium setups, so let's use a built-in feature that's far more reliable: Chromium's remote debugging protocol. Here's how to implement it:

Step 1: Launch Chromium with remote debugging enabled

Modify your $chrome variable to add the remote debugging port flag. This lets you query Chromium's tab state via HTTP:

$chrome = "DISPLAY=:0 chromium-browser " + $link_string + " --kiosk --disable-session-crashed-bubble --disable-infobars --remote-debugging-port=9222 &> /dev/null &"

The --remote-debugging-port=9222 flag opens a local HTTP endpoint that exposes tab data in JSON format.

Step 2: Fetch and parse active tabs

Replace your current $wb_chk and tab checking logic with this. You'll use curl to pull the tab data, then parse it in PowerShell:

# Fetch tab data from Chromium's remote debugging endpoint
$tabDataJson = ssh orangepi@"$ip" "curl -s http://localhost:9222/json" | Out-String
$tabs = $tabDataJson | ConvertFrom-Json

# Extract active tab URLs (filter out background tabs if needed)
$activeTabUrls = $tabs | Where-Object { $_.type -eq "page" } | Select-Object -ExpandProperty url

# Now compare against your required URLs
$requiredUrls = $link_string -split " " | ForEach-Object { $_ } # Split your link string into individual URLs
$matchingTabs = 0

foreach ($url in $requiredUrls) {
    if ($activeTabUrls -contains $url) {
        $matchingTabs++
    }
}

Write-Host "Checked $($activeTabUrls.Count) active tabs"
Write-Host "$matchingTabs out of $($requiredUrls.Count) required URLs are already open"

This method works in real-time, even when Chromium is launched via terminal—no more relying on outdated session files.

脚本优化建议

Your script is functional, but here are some tweaks to make it cleaner, more maintainable, and robust:

1. 使用函数减少重复代码

You have duplicate logic for fetching display resolution—wrap that into a reusable function:

function Get-DisplayResolution {
    param([string]$ip)
    $getr = ssh orangepi@"$ip" -X "xrandr -display :0.0;" | Out-String
    $res_pos = $getr.IndexOf(",")
    $getr = $getr.Substring($res_pos+2)
    $res_pos = $getr.IndexOf(",")
    $getr = $getr.Substring(0, $res_pos)
    $res_pos = $getr.IndexOf(" ")
    return $getr.Substring($res_pos+1)
}

# Usage:
$currentResolution = Get-DisplayResolution -ip $ip

Do the same for parsing URLs or fetching uptime—this makes the script easier to debug and update.

2. 添加错误处理与日志记录

Wrap critical commands (like SSH calls) in try/catch blocks to catch failures, and log errors to a file. You can then set up email alerts:

try {
    $tabDataJson = ssh orangepi@"$ip" "curl -s http://localhost:9222/json" | Out-String
}
catch {
    $errorMsg = "Failed to fetch tabs from $ip at $(Get-Date): $_"
    Add-Content -Path "C:\temp\display_errors.log" -Value $errorMsg
    # Send email alert
    Send-MailMessage -From "display-alerts@yourdomain.com" -To "admin@yourdomain.com" -Subject "Display Error on $ip" -Body $errorMsg -SmtpServer "smtp.yourdomain.com"
}

3. 简化月末更新逻辑

Instead of hardcoding day 28, check if it's the last day of the month:

$isLastDayOfMonth = $Calendar.Day -eq [DateTime]::DaysInMonth($Calendar.Year, $Calendar.Month)
if ($isLastDayOfMonth -AND $Calendar.TimeOfDay -gt $updatestart.TimeOfDay -AND $Calendar.TimeOfDay -lt $updatecutoff.TimeOfDay) {
    # Run updates
}

4. 使用更清晰的变量名

Replace vague names like $getr, $c, $i with descriptive ones like $displayResolution, $matchingTabCount, $requiredUrlCount—this makes the script easier to follow for future you or other developers.

最终思路

With the remote debugging protocol, you can reliably check if all required tabs are open, then skip the refresh if everything is already running correctly. Combine that with the script improvements above, and you'll have a much cleaner, more robust display management system.

备注:内容来源于stack exchange,提问作者M.Atkinson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 06:28:00