launcher: add allowRedirects option (wip)

This commit is contained in:
CanadaHonk 2023-02-06 07:17:14 +00:00
parent 20dd0a9e4d
commit 98c4199fe0
2 changed files with 19 additions and 4 deletions

View File

@ -185,7 +185,7 @@ const getBrowserType = name => { // todo: not need this
const portRange = [ 10000, 60000 ]; const portRange = [ 10000, 60000 ];
const generatePort = () => (Math.floor(Math.random() * (portRange[1] - portRange[0] + 1)) + portRange[0]); 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 [ browserPath, browserName ] = await findBrowserPath(forceBrowser, forceEngine);
const browserFriendlyName = getFriendlyName(browserName); const browserFriendlyName = getFriendlyName(browserName);
@ -219,14 +219,15 @@ const startBrowser = async (url, { allowHTTP, windowSize, forceBrowser, forceEng
openingLocal, openingLocal,
closeHandlers, closeHandlers,
browserType, browserType,
dataPath dataPath,
allowRedirects
}); });
return Window; return Window;
}; };
export const open = async (url, opts = {}) => { 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.`); 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.`);

View File

@ -27,7 +27,7 @@ const acquireTarget = async (CDP, filter = () => true) => {
})).sessionId; })).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 pageLoadCallback, pageLoadPromise = new Promise(res => pageLoadCallback = res);
let frameLoadCallback = () => {}, onWindowMessage = () => {}; let frameLoadCallback = () => {}, onWindowMessage = () => {};
CDP.onMessage(msg => { CDP.onMessage(msg => {
@ -39,6 +39,20 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN
injectIPC(); // ensure IPC injection again injectIPC(); // ensure IPC injection again
} catch { } } 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 // when the process has exited (all windows closed), clean up window internally