Created
October 10, 2017 22:04
-
-
Save culttm/6f130bd067fd5d33533d3f8df9975a29 to your computer and use it in GitHub Desktop.
chrome extension using a content script to access the `window` object
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
{ | |
"content_scripts": [ | |
{ | |
"matches": ["http://*/*", "https://*/*"], | |
"js": ["inject.js"], | |
"all_frames": true | |
} | |
], | |
"web_accessible_resources": [ | |
"content.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
console.log(window); |
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
/** | |
* injectScript - Inject internal script to available access to the `window` | |
* | |
* @param {type} file_path Local path of the internal script. | |
* @param {type} tag The tag as string, where the script will be append (default: 'body'). | |
* @see {@link http://stackoverflow.com/questions/20499994/access-window-variable-from-content-script} | |
*/ | |
function injectScript(file_path, tag) { | |
var node = document.getElementsByTagName(tag)[0]; | |
var script = document.createElement('script'); | |
script.setAttribute('type', 'text/javascript'); | |
script.setAttribute('src', file_path); | |
node.appendChild(script); | |
} | |
injectScript(chrome.extension.getURL('content.js'), 'body'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment