0

I got task to write an add-on for Firefox which will add an div element to existing page. I downloaded Add-on SDK and wrote a main.js file that looks like this:

var data = require("sdk/self").data;
require("sdk/tabs").on("ready", ExecuteAd);

function ExecuteAd(tab) {
  if ( tab.url.indexOf("some url checking") > -1 ) {
    var image = "http://www.lavasoft.com/img/product_icons/aaw11/free.png";
    var link = "http://www.google.me";

    tab.attach({
        contentScriptFile: data.url("myscript.js"),
        contentScript: "appendFunc('"+image+"', '"+link+"');"
        //contentScript: "alert('Works');"
    });
  }
}

When I execute command cfx run it starts Firefox and if I go to specific web pages this script works. But when I create XPI file with cfx xpi and then click on Firefox and open that file it installs my add-on but now when I go to same web pages I gave been before add-on does not work. I have this external Javascript file which is stored in folder 'data'.

appendFunc is in myscript.js file.

How to make my extension work in production environment not just testing environment? I think that main problem is that it does not find this data/myscript.js (does it include in .xpi file?)

Noitidart
  • 35,443
  • 37
  • 154
  • 323
clzola
  • 1,925
  • 3
  • 30
  • 49
  • If you wnt to try something other than add-on sdk. Here's a template to help you with bootstrap style: [GitHubGIST :: Noitidart / watchPage...](https://gist.github.com/Noitidart/9287185) – Noitidart Jul 04 '14 at 18:21

1 Answers1

2

Don't mix contentScript and contentScriptFile. Also, you cannot know what of both is loaded first.

Instead load your script, and communicate using port.

main.js

var data = require("sdk/self").data;
require("sdk/tabs").on("ready", ExecuteAd);

function ExecuteAd(tab) {
  var image = "http://www.lavasoft.com/img/product_icons/aaw11/free.png";
  var link = "http://www.google.me";
  var worker = tab.attach({
      contentScriptFile: data.url("myscript.js")
  });
  worker.port.emit("showAd", {image: image, link: link});
}

myscript.js

self.port.on("showAd", function(data) {
  console.log("showing ad", data.link, data.image);
});

Also, it sounds like PageMod would be a better choice for what you're doing.

PS: Also consult the Add-on Policies if you're planning to host on the addons.mozilla.org website. The policies e.g. prohibit injecting ads that a) aren't clearly marked as such and b) where the user did not opt-in prior to that.

nmaier
  • 32,336
  • 5
  • 63
  • 78
  • It does not work using port communication in development environment. I have also tried with PageMod same method and it does not work in development environment. – clzola Jul 04 '14 at 13:52
  • I cannot guess what is broken in your code. I tested mine, and it works using `cfx run` and `cfx xpi`. So please post a follow up question detailing your code, what exactly does not work and error messages (or if it still does fit within the scope of this question, then update your question and let me know). – nmaier Jul 04 '14 at 15:07