简体   繁体   English

在Firefox扩展中的小部件上单击运行内容脚本

[英]Run content script on widget click in firefox extension

I just finishing a chrome extension which add a content script on click on the browser button. 我刚刚完成一个chrome扩展程序,该扩展程序在单击浏览器按钮时添加了内容脚本。 I'm looking for a same way to accomplish it with firefox, I find PageMod but it seem to not work inside a onclick event into my widget. 我正在寻找一种使用firefox完成此操作的方法,我发现了PageMod,但它似乎在我的小部件中的onclick事件中不起作用。

Any suggestion to make this ? 有什么建议吗?

I'm guessing that what you call "the browser button" is what Mozilla calls a widget . 我猜您所说的“浏览器按钮”就是Mozilla所谓的小部件 Activating a page-mod doesn't require you to click anything, only to "attach" (open) a matching web page. 激活page-mod不需要您单击任何东西,只需“附加”(打开)匹配的网页即可。

To interact with page content (a page's DOM) following the clicking of a widget, the value of the widget's onClick attribute should be an anonymous function of no arguments, in which a variable is set to the active tab's attach event: 要在单击窗口小部件之后与页面内容(页面的DOM)进行交互,窗口小部件的onClick属性的值应为不带参数的匿名函数,其中变量设置为活动选项卡的attach事件:

var self=require("self");
var data = self.data;
var widget = require("widget");
var tabs = require("tabs");


var erase = widget.Widget({
 id: "magated",
 label: "click this widget",
 content: "<div>?</div>",
 contentScriptWhen: "start",
 contentScriptUrl: data.url("cs.js"),
 onClick:function () {
  worker = tabs.activeTab.attach({
    contentScriptWhen: "start",
    contentScriptFile: [data.url("cs.js")],
  });
  worker.port.emit("dothing", tabs.activeTab.url);
 },
});

in cs.js one might find the following: 在cs.js中,可能会发现以下内容:

self.port.on("dothing", function (url) {
  var bigUrl = document.createElement("h1");
  bigUrl.textContent = url;
  document.body.insertBefore(bigUrl, document.body.firstElementChild);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM