Create XPI package with the Add-on SDK?

拟墨画扇 提交于 2019-12-25 07:29:03

问题


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?)


回答1:


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.



来源:https://stackoverflow.com/questions/24574276/create-xpi-package-with-the-add-on-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!