Last active
August 4, 2024 17:29
-
-
Save jimevans/b69d1083a4ef13b097ec2776326925f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The below is pseudocode. | |
// mapperTab.js is the output of the build process from | |
// https://github.com/GoogleChromeLabs/chromium-bidi | |
string bidiMapperTabContent = getFileContent("/path/to/mapperTab.js"); | |
// WebSocket address is found by monitoring console output of | |
// executing Chrome, and parsing the "DevTools listening on ws://xxx" | |
string webSocketUrl = launchChrome(); | |
// Connect to the WebSocket (assume this all does the right thing | |
// to handle the duplex communication). | |
WebSocketConnection connection = new(webSocketUrl); | |
object response = connection.send(JSON.stringify({ | |
id: 0, | |
method: "Target.getTargets", | |
params: {} | |
}); | |
string targetId = response["result"]["targetInfos"][0]["targetId"]; | |
response = connection.send(JSON.stringify({ | |
id: 1, | |
method: "Target.attachToTarget", | |
params: { | |
targetId: targetId, | |
flatten: true | |
} | |
}); | |
string sessionId = response["result"]["sessionId"] | |
response = connection.send(JSON.stringify({ | |
id: 2, | |
method: "Target.exposeDevToolsProtocol", | |
params: { | |
bindingName: "cdp", | |
targetId: targetId | |
} | |
}); | |
response = connection.send(JSON.stringify({ | |
id: 3, | |
method: "Runtime.addBinding", | |
params: { | |
name: "sendBidiResponse" | |
}, | |
sessionId: sessionId | |
}); | |
// To prevent the beforeunload prompt from appearing on close | |
response = connection.send(JSON.stringify({ | |
id: 4, | |
method: "Runtime.evaluate", | |
params: { | |
expression: "document.body.click()", | |
userGesture: true | |
} | |
}); | |
response = connection.send(JSON.stringify({ | |
id: 5, | |
method: "Runtime.evaluate", | |
params: { | |
expression: `"${bidiMapperTabContent}"`, | |
}, | |
sessionId: sessionId | |
}); | |
response = connection.send(JSON.stringify({ | |
id: 6, | |
method: "Runtime.evaluate", | |
params: { | |
expression: `"window.runMapperInstance(${targetId}, {})"`, | |
awaitPromise: true | |
}, | |
sessionId: sessionId | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment