Why doesn't ContentScriptFile work in PageMod?

I am trying to inject the contents of a script into a page and use

console.log("starting addon"); pageMod.PageMod({ include: "*",//tempopary contentScriptFile: self.data.url("testPreload.js"), contentScriptWhen: 'start'}); 

testPreload.js:

 console.log('testPreload'); 

I see the "starting addon" in the log, and if I use contentScript:"console.log('testPreload')" instead of contentScriptFile , I also see "testPreload".

But when I use contentScriptFile , I see a "starting addon", but not "testPreload". What am I doing wrong?

EDIT Error: Error opening input stream (invalid filename?) Resource filePath: //jid1-ktaxagdysynpew-at-jetpack/extension/data/testPreload.js

+7
firefox firefox-addon firefox-addon-sdk
source share
3 answers

You want to move the testPreload.js file to the data directory. The self.data module actually refers to this directory, so the self.data.url() function provides a valid URL to the files in this directory. FYI, these URLs tend to look like resource://[your-jetpack-id]/data/[file])

Again just move your: lib/testPreload.js to data/testPreload.js and this should fix the problem.

+4
source share

ContentScript files must be in the data directory in order to access it through self.data.url('scriptname') .
Move testPreload.js to the data directory.

See https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/Loading_Content_Scripts

+2
source share

Instead:

 contentScriptFile: self.data.url("testPreload.js"), 

Using:

 contentScriptFile: "resource://<your_extension_name>/testPreload.js", 

self.Data.url() is a function that sets the path to the file, returns the full path of the URI to the file, assuming that it is in your additional data folder.

If you prefer to put your file in the root directory of the package, you can build the URI path yourself, as in the example above. Just replace <your_extension_name> with the actual name that you specified in your add-on in the package.json file.

0
source share

All Articles