仅使用 Powershell 设置开发工具传感器(时区)

问题描述 投票:0回答:1

我想出了以下脚本来设置传感器,以便更改边缘浏览器的时区:

# Start Edge with remote debugging enabled and a custom profile
Start-Process "msedge.exe" "--remote-debugging-port=9222 --auto-open-devtools-for-tabs --user-data-dir=C:\TestProfile https://example.com"

# Wait for Edge to start
Start-Sleep -Seconds 5

# Connect to the DevTools protocol
$websocketDebuggerUrl = (Invoke-RestMethod -Uri "http://localhost:9222/json/version").webSocketDebuggerUrl

# Function to send messages to WebSocket
function Send-WebSocketMessage {
    param (
        [Parameter(Mandatory=$true)]
        [string]$message
    )
    $buffer = [System.Text.Encoding]::UTF8.GetBytes($message)
    $segment = [System.ArraySegment[byte]]::new($buffer)
    $websocket.SendAsync($segment, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, [Threading.CancellationToken]::None).Wait()
}

# Establish WebSocket connection
$websocket = New-Object System.Net.WebSockets.ClientWebSocket
$websocket.Options.KeepAliveInterval = [TimeSpan]::FromSeconds(20)
$websocket.ConnectAsync([Uri]$websocketDebuggerUrl, [Threading.CancellationToken]::None).Wait()

# Set timezone
$timezonePayload = @{
    id = 1
    method = "Emulation.setTimezoneOverride"
    params = @{
        timezoneId = "Asia/Tokyo"
    }
} | ConvertTo-Json -Compress
Send-WebSocketMessage -message $timezonePayload

# Receive response
$receiveBuffer = New-Object byte[] 1024
$receiveSegment = [System.ArraySegment[byte]]::new($receiveBuffer)
$websocket.ReceiveAsync($receiveSegment, [Threading.CancellationToken]::None).Wait()
$response = [System.Text.Encoding]::UTF8.GetString($receiveBuffer).TrimEnd([char]0)
Write-Output "Response from WebSocket: $response"

$websocket.Dispose() 

我遇到的问题是我总是得到:

Response from WebSocket:{"id":1,"error":"code":-32601,"message":"'Emulation.setTimezoneOverride' wasn't found"...

目标是仅更改此浏览器会话的时区,而不干扰原始用户配置文件或其他选项卡/Windwos

打开开发工具 (F12),然后根据需要操作传感器,但我想在打开此 Edge 变体或链接时设置传感器。

希望这能澄清我正在尝试做的事情。

奖励:无需安装其他软件,例如硒。

powershell google-chrome-devtools microsoft-edge chromium
1个回答
0
投票

不熟悉Powershell脚本,但你似乎错过了

sessionId
。这是你可以参考的想法。

  1. 使用
    targetId
     获取可能的 
    Target.getTargets
  2. 列表
  3. 使用
    Target.attachToTarget
    连接到目标并记下返回的
    sessionId
  4. Emulation.setTimezoneOverride
    设置为您刚刚获得的
    sessionId
© www.soinside.com 2019 - 2024. All rights reserved.