Possible Duplicate:
Building a Chrome Extension - Inject code in a page using a Content script
Below is my initial attempt. First I created a test webpage:
- test.html -
<HTML>
<SCRIPT src="script.js"></SCRIPT>
</HTML>
- script.js -
function testFunction() {
console("function successfully run!");
}
Then I created a very simple extension to see if I could run testFunction() from the content script:
- manifest.json -
{
"name": "Function Test",
"manifest_version": 2,
"version": "1",
"description": "An extension to experiment with running the javascript functions of the website being browsed.",
"permissions": ["<all_urls>"],
"content_scripts": [
{
"all_frames": true,
"matches": ["<all_urls>"],
"js": ["cs.js"],
"run_at": "document_end"
}
]
}
- cs.js -
scriptNodes = document.getElementsByTagName("script");
script = scriptNodes[0];
console.log(script.src);
script.testFunction();
Here is the console output:
file:///C:/.../script.js
Uncaught TypeError: Object #<HTMLScriptElement> has no method 'testFunction'
So, is it possible to run a function on a website you are browsing using a Chrome Extension?