-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for running user scripts in cross origin iframes
Introduces a new content method `frame = await tabs.frame(iframeElement)` which returns a new object upon which scripts can be run using `frame.run(() => {...})` and `frame.wait(() => {...})` and `frame.waitForNewPage(() => {...})`
- Loading branch information
1 parent
b170728
commit 1ca8613
Showing
11 changed files
with
388 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
const {assert} = require('chai'); | ||
|
||
const scriptFrameCommands = require('./scriptFrameCommands'); | ||
const {mergeCoverageReports} = require('../../../../lib/mergeCoverage'); | ||
|
||
module.exports = (tabManager, frame) => { | ||
const {tab, browserFrameId} = frame; | ||
const {id: tabId, browserTabId} = tab; | ||
|
||
const submitCodeCoverage = contentCoverage => { | ||
// eslint-disable-next-line camelcase, no-undef | ||
const myCoverage = typeof __runner_coverage__ === 'object' && __runner_coverage__; | ||
if (myCoverage) { | ||
mergeCoverageReports(myCoverage, contentCoverage); | ||
} | ||
}; | ||
|
||
const waitForChildFrameToken = async (token) => { | ||
return await frame.waitForChildFrameToken(String(token)); // returns the frameId | ||
}; | ||
|
||
const receivedFrameToken = async (token) => { | ||
frame.parentFrame.resolveChildFrameToken(token, frame); | ||
}; | ||
|
||
const validateFrameId = frameId => { | ||
// only allowed to execute commands on child frames (not ancestors, children of children, etc) | ||
const childFrame = tab.getFrame(frameId); | ||
assert.isTrue(frame.isChild(childFrame), 'Invalid frame id'); | ||
}; | ||
|
||
const run = async ({frameId, code, arg}) => { | ||
validateFrameId(frameId); | ||
return await scriptFrameCommands.run({tabManager, tabId, frameId, code, arg}); | ||
}; | ||
|
||
const waitForNewPage = async ({frameId, code, arg, timeoutMs}) => { | ||
validateFrameId(frameId); | ||
return await scriptFrameCommands.waitForNewPage({tabManager, tabId, frameId, code, arg, timeoutMs}); | ||
}; | ||
|
||
const wait = async ({frameId, code, arg}) => { | ||
validateFrameId(frameId); | ||
return await scriptFrameCommands.wait({tabManager, tabId, frameId, code, arg}); | ||
}; | ||
|
||
return new Map([ | ||
['tabs.mainContentInit', () => tabManager.handleTabMainContentInitialized(browserTabId, browserFrameId)], | ||
['tabs.contentInit', ({moduleName}) => tabManager.handleTabModuleInitialized(browserTabId, browserFrameId, moduleName)], | ||
['core.submitCodeCoverage', submitCodeCoverage], | ||
['tabs.waitForChildFrameToken', waitForChildFrameToken], | ||
['tabs.receivedFrameToken', receivedFrameToken], | ||
['tabs.frameRun', run], | ||
['tabs.frameWait', wait], | ||
['tabs.frameWaitForNewPage', waitForNewPage], | ||
]); | ||
}; |
104 changes: 104 additions & 0 deletions
104
runner-modules/tabs/lib/background/scriptFrameCommands.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict'; | ||
const {illegalArgumentError, newPageWaitTimeoutError, CONTENT_SCRIPT_ABORTED_ERROR} = require('../../../../lib/scriptErrors'); | ||
const delay = require('../../../../lib/delay'); | ||
|
||
const validateTabId = (method, tabManager, tabId) => { | ||
if (typeof tabId !== 'string' || !tabManager.hasTab(tabId)) { | ||
throw illegalArgumentError(`tabs.${method}(): invalid argument \`tabId\``); | ||
} | ||
}; | ||
|
||
const validateFrameId = (method, tabManager, tabId, frameId) => { | ||
if (typeof frameId !== 'number' || | ||
frameId < 0 || | ||
!Number.isFinite(frameId) || | ||
!tabManager.getTab(tabId).hasFrame(frameId) | ||
) { | ||
throw illegalArgumentError(`tabs.${method}(): invalid argument \`frameId\` (${tabId} : ${frameId})`); | ||
} | ||
}; | ||
|
||
const run = async ({tabManager, tabId, frameId, code, arg}) => { | ||
validateTabId('run', tabManager, tabId); | ||
validateFrameId('run', tabManager, tabId, frameId); | ||
if (typeof code !== 'string') { | ||
throw illegalArgumentError('tabs.run(): invalid argument `code`'); | ||
} | ||
|
||
const metadata = Object.freeze({ | ||
runBeginTime: Date.now(), | ||
}); | ||
|
||
return await tabManager.runContentScript(tabId, frameId, code, {arg, metadata}); | ||
}; | ||
|
||
const waitForNewPage = async ({tabManager, tabId, frameId, code, arg, timeoutMs}) => { | ||
validateTabId('waitForNewPage', tabManager, tabId); | ||
validateFrameId('waitForNewPage', tabManager, tabId, frameId); | ||
if (typeof code !== 'string') { | ||
throw illegalArgumentError('tabs.waitForNewPage(): invalid argument `code`'); | ||
} | ||
|
||
const metadata = Object.freeze({ | ||
runBeginTime: Date.now(), | ||
}); | ||
|
||
const waitForNewContentPromise = tabManager.waitForNewContent(tabId, frameId); | ||
try { | ||
const {reject} = await tabManager.runContentScript(tabId, frameId, code, {arg, metadata}); | ||
// do not return `resolve` to avoid timing inconsistencies (e.g. the script may have been canceled because of the navigation) | ||
if (reject) { | ||
return {reject}; | ||
} | ||
} | ||
catch (err) { | ||
// ignore errors which are caused by navigating away; that is what we are expecting | ||
if (err.name !== CONTENT_SCRIPT_ABORTED_ERROR) { | ||
throw err; | ||
} | ||
} | ||
|
||
// the timeout does not start counting until the content script has completed its execution; this is by design | ||
await Promise.race([ | ||
waitForNewContentPromise, | ||
delay(timeoutMs).then(() => Promise.reject( | ||
newPageWaitTimeoutError(`Waiting for a new page timed out after ${timeoutMs / 1000} seconds`) | ||
)), | ||
]); | ||
|
||
return {reject: null}; | ||
}; | ||
|
||
const wait = async ({tabManager, tabId, frameId, code, arg}) => { | ||
validateTabId('wait', tabManager, tabId); | ||
validateFrameId('wait', tabManager, tabId, frameId); | ||
if (typeof code !== 'string') { | ||
throw illegalArgumentError('tabs.wait(): invalid argument `code`'); | ||
} | ||
|
||
const waitMetadata = Object.freeze({ | ||
waitBeginTime: Date.now(), | ||
}); | ||
|
||
const attempt = async (attemptNumber) => { | ||
try { | ||
const metadata = Object.assign({ | ||
attemptNumber, | ||
runBeginTime: Date.now(), | ||
}, waitMetadata); | ||
return await tabManager.runContentScript(tabId, frameId, code, {arg, metadata}); | ||
} | ||
catch (err) { | ||
if (err.name === CONTENT_SCRIPT_ABORTED_ERROR) { | ||
// runContentScript wait for a new tab to initialize | ||
return await attempt(attemptNumber + 1); | ||
} | ||
|
||
throw err; | ||
} | ||
}; | ||
|
||
return await attempt(0); | ||
}; | ||
|
||
module.exports = {run, waitForNewPage, wait}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.