diff --git a/src/api/v8Cache.js b/src/api/v8Cache.js index bfb7574..7679be8 100644 --- a/src/api/v8Cache.js +++ b/src/api/v8Cache.js @@ -13,8 +13,6 @@ export default async (CDP, evaluate, { browserType, dataPath }) => { }; } - await CDP.send('Page.enable'); - const getDefaultPath = () => join(dataPath, 'v8Cache.json'); const getScriptUrls = async (includePreload = true) => (await evaluate(`[...document.querySelectorAll('script[src]${includePreload ? ', link[as=script]' : ''}')].map(x => x.src ?? x.href).join(';')`)).split(';'); diff --git a/src/index.js b/src/index.js index ba358bf..3c5be37 100644 --- a/src/index.js +++ b/src/index.js @@ -202,7 +202,7 @@ const startBrowser = async (url, { allowHTTP = false, allowRedirects = 'same-ori const basePath = isAbsolute(url) ? url : join(ranJsDir, url); const closeHandlers = []; - if (openingLocal && browserType === 'firefox') closeHandlers.push(await LocalHTTP({ localUrl, url: basePath })); + if (openingLocal && browserType === 'firefox') closeHandlers.push(await LocalHTTP({ url: localUrl, basePath })); const Window = await (browserType === 'firefox' ? Firefox : Chromium)({ dataPath, @@ -214,8 +214,8 @@ const startBrowser = async (url, { allowHTTP = false, allowRedirects = 'same-ori extensions: ExtensionsAPI._extensions[browserType] }, { browserName: browserFriendlyName, - url: openingLocal ? basePath : url, - localUrl, + url: openingLocal ? localUrl : url, + basePath, openingLocal, closeHandlers, browserType, diff --git a/src/launcher/inject.js b/src/launcher/inject.js index 2a85b2e..bf9671a 100644 --- a/src/launcher/inject.js +++ b/src/launcher/inject.js @@ -27,10 +27,10 @@ const acquireTarget = async (CDP, filter = () => true) => { })).sessionId; }; -export default async (CDP, proc, injectionType = 'browser', { dataPath, browserName, browserType, openingLocal, localUrl, url, allowRedirects, closeHandlers }) => { +export default async (CDP, proc, injectionType = 'browser', { dataPath, browserName, browserType, openingLocal, url, basePath, allowRedirects, closeHandlers }) => { let pageLoadCallback, pageLoadPromise = new Promise(res => pageLoadCallback = res); let frameLoadCallback = () => {}, onWindowMessage = () => {}; - CDP.onMessage(msg => { + CDP.onMessage(async msg => { if (msg.method === 'Runtime.bindingCalled' && msg.params.name === '_gluonSend') onWindowMessage(JSON.parse(msg.params.payload)); if (msg.method === 'Page.frameStoppedLoading') frameLoadCallback(msg.params); if (msg.method === 'Page.loadEventFired') pageLoadCallback(); @@ -45,13 +45,21 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN if (allowRedirects === true) return; if (allowRedirects === 'same-origin' && new URL(newUrl).origin === new URL(url).origin) return; + console.log(url, new URL(url).origin); + CDP.sendMessage('Page.stopLoading', {}, sessionId); if (msg.method === 'Page.frameNavigated') { CDP.sendMessage('Page.navigate', { url: 'about:blank' }, sessionId); - // CDP.sendMessage('Page.navigate') + + const history = await CDP.sendMessage('Page.getNavigationHistory', {}, sessionId); + const oldUrl = history.entries[history.currentIndex - 1].url; + + CDP.sendMessage('Page.navigate', { + url: oldUrl, + frameId: msg.params.frame.id + }, sessionId); } - // CDP.sendMessage('Page.navigate', { url: 'about:blank' }, sessionId); } }); @@ -66,9 +74,10 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN let sessionId; if (injectionType === 'browser') sessionId = await acquireTarget(CDP, target => target.url !== 'about:blank'); - if (openingLocal && browserType === 'chromium') await LocalCDP(CDP, { sessionId, localUrl, url }); + if (openingLocal && browserType === 'chromium') await LocalCDP(CDP, { sessionId, url, basePath }); await CDP.sendMessage('Runtime.enable', {}, sessionId); // enable runtime API + CDP.sendMessage('Page.enable', {}, sessionId); // enable page API CDP.sendMessage('Runtime.addBinding', { // setup sending from window to Node via Binding name: '_gluonSend' diff --git a/src/lib/local/cdp.js b/src/lib/local/cdp.js index 8e7cddf..2701cdc 100644 --- a/src/lib/local/cdp.js +++ b/src/lib/local/cdp.js @@ -1,8 +1,8 @@ import createLocalFulfill from './fulfill.js'; import { log } from '../logger.js'; -export default async (CDP, { sessionId, url: givenPath, localUrl }) => { - const localFulfill = createLocalFulfill(givenPath); +export default async (CDP, { sessionId, basePath, url }) => { + const localFulfill = createLocalFulfill(basePath); CDP.onMessage(async msg => { if (msg.method === 'Fetch.requestPaused') { @@ -21,7 +21,7 @@ export default async (CDP, { sessionId, url: givenPath, localUrl }) => { await CDP.sendMessage('Fetch.enable', { patterns: [ { - urlPattern: `${localUrl}*` + urlPattern: `${url}*` } ] }); diff --git a/src/lib/local/fulfill.js b/src/lib/local/fulfill.js index 9b05ef2..186ed7d 100644 --- a/src/lib/local/fulfill.js +++ b/src/lib/local/fulfill.js @@ -12,7 +12,7 @@ const generatePath = (pathname, indexFile) => { export default givenPath => { const basePath = extname(givenPath) ? dirname(givenPath) : givenPath; - const indexFile = extname(givenPath) ? basename(givenPath) : 'index.html'; + const indexFile = extname(basePath) ? basename(basePath) : 'index.html'; return async url => { url = new URL(url); diff --git a/src/lib/local/http.js b/src/lib/local/http.js index 97add21..23451c6 100644 --- a/src/lib/local/http.js +++ b/src/lib/local/http.js @@ -2,12 +2,12 @@ import { createServer } from 'http'; import createLocalFulfill from './fulfill.js'; import { log } from '../logger.js'; -export default async ({ url: givenPath, localUrl }) => { - const localFulfill = createLocalFulfill(givenPath); +export default async ({ basePath, url }) => { + const localFulfill = createLocalFulfill(basePath); - const port = parseInt(localUrl.split(':').pop()); + const port = parseInt(url.split(':').pop()); const server = createServer(async (req, res) => { - const { status, body, headers } = await localFulfill(localUrl + decodeURI(req.url)); + const { status, body, headers } = await localFulfill(url + decodeURI(req.url)); res.writeHead(status, headers); res.end(body, 'utf8');