用附加SDK创建XPI包? [英] Create XPI package with the Add-on SDK?

查看:836
本文介绍了用附加SDK创建XPI包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任务为Firefox编写一个附加组件,它将在现有页面中添加一个div元素。我下载了附加SDK,写了一个如下所示的 main.js 文件:

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

函数Exec​​uteAd(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( ' +图像+', + +链接');
// contentScript:alert('Works');
});




$ b当我执行命令 cfx运行它启动Firefox,如果我去特定的网页这个脚本的作品。但是,当我用 cfx xpi 创建XPI文件,然后点击Firefox并打开该文件时,它会安装我的插件,但现在当我转到之前提供的相同网页时加载项不起作用。我有这个外部的JavaScript文件,存储在文件夹'数据'。
$ b

appendFunc myscript.js 文件。



如何让我的扩展工作在生产环境中而不仅仅是测试环境?我认为主要的问题是它没有找到这个数据/ myscript.js(包括在.xpi文件中吗?)

解决方案

不要混合 contentScript contentScriptFile 。另外,你不能知道两者是先加载的。



改为加载你的脚本, port 进行通信。



main.js

  var data = require ( SDK /自)数据; 
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( ,data.link,data.image);
});

另外,这听起来像 PageMod 将会是更好的选择,重新做。



PS:另请参阅如果您打算在addons.mozilla.org网站上托管,则可以使用附加政策。政策例如禁止注射广告:a)没有明确标记,b)用户在此之前没有选择加入的广告。

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

解决方案

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.

这篇关于用附加SDK创建XPI包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆