Skip to content

Instantly share code, notes, and snippets.

@minhoryang
Created December 22, 2018 04:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save minhoryang/f67551c854b313d3664cf35422aa3eba to your computer and use it in GitHub Desktop.
Save minhoryang/f67551c854b313d3664cf35422aa3eba to your computer and use it in GitHub Desktop.
Inject custom script to main page's document.head, used for bypassing Chrome Extension's Content-Scripts Sandbox.
'Drink Responsibly, Blackmagic lives here.';
/**
* Inject custom script to main page's document.head.
* Used for bypassing Chrome Extension's Content-Scripts Sandbox.
*/
/**
* @param {{type?: string, src?: string, text?: string}} content
* @see Microsoft/vscode#22980, "JSDoc comments are ignored for destructured parameters".
* @see Microsoft/typescript#11597, "Intellisense for object properties defined in multi-line JSDOC comments".
*/
function __inject__({type, src, text}) {
if (src === undefined && text === undefined) return;
const _ = document.createElement('script');
_.async = true;
if (type !== undefined) _.type = type;
if (src !== undefined) _.src = src;
if (text !== undefined) _.appendChild(document.createTextNode(text));
if (document.head) {
document.head.appendChild(_);
} else { // XXX: Used for `run_at: document_start`.
document.documentElement.appendChild(_);
}
}
// HACK: BREAKING ContentScripts Sandbox.
__inject__({
type: 'text/javascript',
text: `(${ String(__inject__) })({
type: 'module',
src: '${ chrome.extension.getURL("wanted_content_script.js") }',
})`,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment