7

I try to inject content script on 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 "starting addon" in log and if I use contentScript:"console.log('testPreload')" instead of contentScriptFile I also see "testPreload".

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

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

urig
  • 16,016
  • 26
  • 115
  • 184
Suhan
  • 1,434
  • 2
  • 13
  • 28

3 Answers3

4

You want to move your testPreload.js file into the data directory. The self.data module is actually referencing that directory so the self.data.url() function gives you a valid URL to the files in that directory. FYI those URLs tend to look like resource://[your-jetpack-id]/data/[file])

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

Bryan Clark
  • 2,542
  • 1
  • 15
  • 19
  • What do I do if I don't want to move the script to the data directory? If I'd rather keep it in the root of the XPI? Tx. – urig Nov 20 '15 at 21:17
  • Managed to do it using a nasty, nasty hack that I cooked up: https://gist.github.com/urig/828ddc85e3fd0b79327e – urig Nov 20 '15 at 22:03
2

Your contentScript Files should reside in data directory to be able to access it through self.data.url('scriptname') .
Move your testPreload.js to data directory.

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

abhishekkannojia
  • 2,796
  • 23
  • 32
0

Instead of:

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

Use:

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

self.Data.url() is a function that given a file path will return the full URI path to the file assuming it is under your add-on's data folder.

If you prefer to put your file at the root of the package, you can instead build the URI path yourself as in the above example. Just replace <your_extension_name> with the actual name you've given to your add-on in its package.json file.

urig
  • 16,016
  • 26
  • 115
  • 184