Is it possible to create a Chrome extension that can perform javascript functions that belong to the page you're viewing?

Possible duplicate:
Creating a Chrome extension - entering code on a page using script content

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 can run testFunction () from the contents of the 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 the function on the website you are viewing using the Chrome extension?

+4
source share
1 answer

It seems to be impossible.

See # 4658143 and http://code.google.com/chrome/extensions/content_scripts.html

However, content scripts have some limitations. They can not:

  • Use chrome. * API (excluding chrome.extension parts)
  • Use variables or functions defined by their extension pages
  • Use variables or functions defined by web pages or other content scripts.
+1
source

Source: https://habr.com/ru/post/1444196/


All Articles