mirror of
https://github.com/stablyai/orca.git
synced 2026-10-09 16:02:46 +00:00
31 lines
938 B
JavaScript
31 lines
938 B
JavaScript
import { WebSocket } from 'ws'
|
|
import { evaluateHostedDocumentWithRetry } from './hosted-webview-cdp-session.mjs'
|
|
|
|
export async function navigateHostedWebViewRoute(document, route, WebSocketCtor = WebSocket) {
|
|
if (!isSafeHostedRoute(route)) {
|
|
throw new Error('Hosted WebView route is invalid')
|
|
}
|
|
const value = await evaluateHostedDocumentWithRetry(
|
|
document,
|
|
`(() => {
|
|
const route = ${JSON.stringify(route)};
|
|
history.pushState(history.state, '', route);
|
|
dispatchEvent(new PopStateEvent('popstate', { state: history.state }));
|
|
return String(location.pathname) + String(location.search);
|
|
})()`,
|
|
WebSocketCtor
|
|
)
|
|
if (value !== route) {
|
|
throw new Error('Hosted WebView route navigation was not retained')
|
|
}
|
|
}
|
|
|
|
function isSafeHostedRoute(route) {
|
|
return (
|
|
typeof route === 'string' &&
|
|
route.startsWith('/h/') &&
|
|
!route.startsWith('//') &&
|
|
!route.includes('#')
|
|
)
|
|
}
|