Saruman Saruman - 1 year ago 60
Javascript Question

Chrome Extension referencing/calling other script functions from a content script

I have a content script with a lot of functions in it, i would like to be able to split out those functions into other scripts

Is there any magic needed to call other scripts from the content script

my manifest contains both scripts



my content script is working fine

however if i put a function in the other.js file and step through it, anything i reference in other.js is undefined

is there anything i should know here?


This is just a simple example, the Test function should run on contentscript load




function Test(){
return true;

Google is telling me uncaught ReferenceError, Test not defined

Answer Source

According to the docs on Content Scripts:

js: The list of JavaScript files to be injected into matching pages. These are injected in the order they appear in this array.

In your case, content.js would be injected first and try to execute other's Test() function, before other.js is loaded).

Note that based on your manifest, both scripts will be loaded at "document_idle", so even if content.js has registered the call to Test() to be run after the page is loaded, it should still run immediately (since the page is already loaded.
If you want your scripts to be injected before the page's content is loaded, then modify your manifest:

"content_scripts": {
    "run_at": "document_start"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download