Search code examples
powershellgoogle-chrome-devtoolsmicrosoft-edgechromium

Setting Dev-Tool Sensor (Time Zone) with only Powershell


I came up with the following script to set a sensor in order to to change the time zone for the edge brwoser:

# 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() 

The Problem I have is that i always get : Response from WebSocket:{"id":1,"error":"code":-32601,"message":"'Emulation.setTimezoneOverride' wasn't found"...

The goal is to change the Time Zone for this browsersession only and not interfering with the original user profile or other Tabs / Windwos

When opening Dev Tools (F12) and then manipulating the the sensors works as I need to, but I want to set the sensor when opening up this Edge variant or link.

Hope this clarifys a bit what i am trying to do.

Bonus: No additional software can be installed like e.g. selenium.


Solution

  • Not familiar with Powershell scripts, but you seem to have missed sessionId. Here's the idea you can refer to.

    1. Get a list of the possible targetId with Target.getTargets
    2. Connect to the Target with Target.attachToTarget and note the sessionId returned
    3. Set Emulation.setTimezoneOverride with the sessionId you've just got