|
| 1 | +'use strict'; |
| 2 | +const {illegalArgumentError, newPageWaitTimeoutError, CONTENT_SCRIPT_ABORTED_ERROR} = require('../../../../lib/scriptErrors'); |
| 3 | +const delay = require('../../../../lib/delay'); |
| 4 | + |
| 5 | +const validateTabId = (method, tabManager, tabId) => { |
| 6 | + if (typeof tabId !== 'string' || !tabManager.hasTab(tabId)) { |
| 7 | + throw illegalArgumentError(`tabs.${method}(): invalid argument \`tabId\``); |
| 8 | + } |
| 9 | +}; |
| 10 | + |
| 11 | +const validateFrameId = (method, tabManager, tabId, frameId) => { |
| 12 | + if (typeof frameId !== 'number' || |
| 13 | + frameId < 0 || |
| 14 | + !Number.isFinite(frameId) || |
| 15 | + !tabManager.getTab(tabId).hasFrame(frameId) |
| 16 | + ) { |
| 17 | + throw illegalArgumentError(`tabs.${method}(): invalid argument \`frameId\` (${tabId} : ${frameId})`); |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +const run = async ({tabManager, tabId, frameId, code, arg}) => { |
| 22 | + validateTabId('run', tabManager, tabId); |
| 23 | + validateFrameId('run', tabManager, tabId, frameId); |
| 24 | + if (typeof code !== 'string') { |
| 25 | + throw illegalArgumentError('tabs.run(): invalid argument `code`'); |
| 26 | + } |
| 27 | + |
| 28 | + const metadata = Object.freeze({ |
| 29 | + runBeginTime: Date.now(), |
| 30 | + }); |
| 31 | + |
| 32 | + return await tabManager.runContentScript(tabId, frameId, code, {arg, metadata}); |
| 33 | +}; |
| 34 | + |
| 35 | +const waitForNewPage = async ({tabManager, tabId, frameId, code, arg, timeoutMs}) => { |
| 36 | + validateTabId('waitForNewPage', tabManager, tabId); |
| 37 | + validateFrameId('waitForNewPage', tabManager, tabId, frameId); |
| 38 | + if (typeof code !== 'string') { |
| 39 | + throw illegalArgumentError('tabs.waitForNewPage(): invalid argument `code`'); |
| 40 | + } |
| 41 | + |
| 42 | + const metadata = Object.freeze({ |
| 43 | + runBeginTime: Date.now(), |
| 44 | + }); |
| 45 | + |
| 46 | + const waitForNewContentPromise = tabManager.waitForNewContent(tabId, frameId); |
| 47 | + try { |
| 48 | + const {reject} = await tabManager.runContentScript(tabId, frameId, code, {arg, metadata}); |
| 49 | + // do not return `resolve` to avoid timing inconsistencies (e.g. the script may have been canceled because of the navigation) |
| 50 | + if (reject) { |
| 51 | + return {reject}; |
| 52 | + } |
| 53 | + } |
| 54 | + catch (err) { |
| 55 | + // ignore errors which are caused by navigating away; that is what we are expecting |
| 56 | + if (err.name !== CONTENT_SCRIPT_ABORTED_ERROR) { |
| 57 | + throw err; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // the timeout does not start counting until the content script has completed its execution; this is by design |
| 62 | + await Promise.race([ |
| 63 | + waitForNewContentPromise, |
| 64 | + delay(timeoutMs).then(() => Promise.reject( |
| 65 | + newPageWaitTimeoutError(`Waiting for a new page timed out after ${timeoutMs / 1000} seconds`) |
| 66 | + )), |
| 67 | + ]); |
| 68 | + |
| 69 | + return {reject: null}; |
| 70 | +}; |
| 71 | + |
| 72 | +const wait = async ({tabManager, tabId, frameId, code, arg}) => { |
| 73 | + validateTabId('wait', tabManager, tabId); |
| 74 | + validateFrameId('wait', tabManager, tabId, frameId); |
| 75 | + if (typeof code !== 'string') { |
| 76 | + throw illegalArgumentError('tabs.wait(): invalid argument `code`'); |
| 77 | + } |
| 78 | + |
| 79 | + const waitMetadata = Object.freeze({ |
| 80 | + waitBeginTime: Date.now(), |
| 81 | + }); |
| 82 | + |
| 83 | + const attempt = async (attemptNumber) => { |
| 84 | + try { |
| 85 | + const metadata = Object.assign({ |
| 86 | + attemptNumber, |
| 87 | + runBeginTime: Date.now(), |
| 88 | + }, waitMetadata); |
| 89 | + return await tabManager.runContentScript(tabId, frameId, code, {arg, metadata}); |
| 90 | + } |
| 91 | + catch (err) { |
| 92 | + if (err.name === CONTENT_SCRIPT_ABORTED_ERROR) { |
| 93 | + // runContentScript wait for a new tab to initialize |
| 94 | + return await attempt(attemptNumber + 1); |
| 95 | + } |
| 96 | + |
| 97 | + throw err; |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + return await attempt(0); |
| 102 | +}; |
| 103 | + |
| 104 | +module.exports = {run, waitForNewPage, wait}; |
0 commit comments