Created
March 7, 2018 00:49
-
-
Save Macil/fa9c1f150369dcd3e3a2550922771081 to your computer and use it in GitHub Desktop.
This function allows you to load javascript from a remote url with an extension content script, and have the loaded javascript execute within the content script's isolated world.
This file contains hidden or 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
async function loadInContentScript(url) { | |
// Note that this extension must either have permission to the domain of this | |
// URL, or the URL must be served with the correct CORS HTTP headers to allow | |
// this script to fetch it (ie. "Access-Control-Allow-Origin: *"). | |
const response = await fetch(url); | |
const scriptText = await response.text(); | |
// Let Chrome devtools know where this code came from. | |
const codeToRun = scriptText + "\n//# sourceURL=" + url + "\n"; | |
// Put eval into a variable first and then call it so we get "indirect eval" | |
// behavior. A direct eval call is actually syntactically-special and has | |
// effects that we don't want. Code passed to a direct eval call has access | |
// to all of the local variables of the call site, and this prevents the | |
// javascript engine from many optimizations. | |
const indirectEval = eval; | |
indirectEval(codeToRun); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment