From 98c4199fe0e48d9cf6f46473cdbc0df39b92d58b Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Mon, 6 Feb 2023 07:17:14 +0000 Subject: [PATCH] launcher: add allowRedirects option (wip) --- src/index.js | 7 ++++--- src/launcher/inject.js | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index cbd7265..ba358bf 100644 --- a/src/index.js +++ b/src/index.js @@ -185,7 +185,7 @@ const getBrowserType = name => { // todo: not need this const portRange = [ 10000, 60000 ]; const generatePort = () => (Math.floor(Math.random() * (portRange[1] - portRange[0] + 1)) + portRange[0]); -const startBrowser = async (url, { allowHTTP, windowSize, forceBrowser, forceEngine }) => { +const startBrowser = async (url, { allowHTTP = false, allowRedirects = 'same-origin', windowSize, forceBrowser, forceEngine }) => { const [ browserPath, browserName ] = await findBrowserPath(forceBrowser, forceEngine); const browserFriendlyName = getFriendlyName(browserName); @@ -219,14 +219,15 @@ const startBrowser = async (url, { allowHTTP, windowSize, forceBrowser, forceEng openingLocal, closeHandlers, browserType, - dataPath + dataPath, + allowRedirects }); return Window; }; export const open = async (url, opts = {}) => { - const { onLoad, allowHTTP } = opts; + const { onLoad, allowHTTP = false } = opts; if (allowHTTP !== true && url.startsWith('http://')) throw new Error(`HTTP URLs are blocked by default. Please use HTTPS, or if not possible, enable the 'allowHTTP' option.`); diff --git a/src/launcher/inject.js b/src/launcher/inject.js index bd7074a..2a85b2e 100644 --- a/src/launcher/inject.js +++ b/src/launcher/inject.js @@ -27,7 +27,7 @@ const acquireTarget = async (CDP, filter = () => true) => { })).sessionId; }; -export default async (CDP, proc, injectionType = 'browser', { dataPath, browserName, browserType, openingLocal, localUrl, url, closeHandlers }) => { +export default async (CDP, proc, injectionType = 'browser', { dataPath, browserName, browserType, openingLocal, localUrl, url, allowRedirects, closeHandlers }) => { let pageLoadCallback, pageLoadPromise = new Promise(res => pageLoadCallback = res); let frameLoadCallback = () => {}, onWindowMessage = () => {}; CDP.onMessage(msg => { @@ -39,6 +39,20 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN injectIPC(); // ensure IPC injection again } catch { } } + + if (msg.method === 'Page.frameScheduledNavigation' || msg.method === 'Page.frameNavigated') { + let newUrl = msg.params?.frame?.url ?? msg.params?.url; + if (allowRedirects === true) return; + if (allowRedirects === 'same-origin' && new URL(newUrl).origin === new URL(url).origin) return; + + CDP.sendMessage('Page.stopLoading', {}, sessionId); + + if (msg.method === 'Page.frameNavigated') { + CDP.sendMessage('Page.navigate', { url: 'about:blank' }, sessionId); + // CDP.sendMessage('Page.navigate') + } + // CDP.sendMessage('Page.navigate', { url: 'about:blank' }, sessionId); + } }); // when the process has exited (all windows closed), clean up window internally