How to inject script locally in firefox add-on

我是研究僧i 提交于 2019-12-24 15:42:31

问题


I have published a add-on in mozilla add-on website, it got rejected because of remote scripts.

lib/Main.js:

 var data = require("sdk/self").data;
    var pageMod = require("sdk/page-mod");
    var _workers = [];
    pageMod.PageMod({
      include: "some url",
      contentScriptWhen: "ready",
      contentScriptFile: data.url("./inject.js"),
      contentStyleFile:  data.url("style.css"),
      attachTo: 'top'   
      });

data/inject.js:

var script = document.createElement('script');
script.type = "text/javascript";
script1.src =    "https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
document.body.appendChild(script);

var script2 = document.createElement('script');
script2.type = "text/javascript";
script2.src = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js";
document.body.appendChild(script2);

var script3 = document.createElement('script');
script3.type = "text/javascript";
script3.src = "resource://addon-id/addon-name/data/popup.js";
document.body.appendChild(script3);

Please help me to insert these scripts to the web content or insert these scripts locally from my addon. And what is remote script?


回答1:


All the code you use should be reviewable and immutable, google.com being controlled by a suspicious company, is not trustworthy, and can replace jquery.min.js with password/credit card collector, without Mozilla noticing.

To avoid suspicion, copy files required to your plugin's directory, and use them as:

var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");

pageMod.PageMod({
  include: "*.mozilla.org",
  contentScriptFile: [data.url("jquery.min.js"),
                      data.url("jquery-ui.js"),
                      data.url("popup.js")]
});

See page-mod manual, it has all needed instructions.



来源:https://stackoverflow.com/questions/30504534/how-to-inject-script-locally-in-firefox-add-on

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