我正在尝试使用他们的 sdk 制作一个 firefox 插件,但我不知道如何让我的 js 脚本进行通信。目标是制作一个带有表单的面板,其中有 3 个复选框,选中时可以隐藏/显示活动选项卡上的某些元素。
以下是脚本: main.js:
exports.main = function() {};
var data = require("sdk/self").data;
var panel1 = require("sdk/panel").Panel({
width: 215,
height: 160,
contentURL: data.url("panelDestroyer.html"),
contentScriptFile: [data.url("jquery.js"),data.url("panel.js")]
});
require("sdk/widget").Widget({
id: "open-form-btn",
label: "Clear",
contentURL: data.url("mozico.png"),
contentScriptFile: data.url("jquery.js"),
panel: panel1
});
var tabs = require("sdk/tabs");
var tabWorkers = {};
tabs.on("ready", function(tab) {
attachTabWorker(tab);
});
for each (var tab in tabs) {
attachTabWorker(tab);
}
function attachTabWorker(tab) {
// Attach and store
var tabWorker = tabWorkers[tab.id] = tab.attach({
contentScriptFile: [data.url("clear.js"),data.url("jquery.js")]
});
// Clean up
tabWorker.on("detach", function() {
if (getTabWorker(tab) === tabWorker) {
delete tabWorkers[tab.id];
}
});
}
function getTabWorker(tab) {
return tabWorkers[tab.id];
}
function getActiveTabWorker() {
return getTabWorker(tabs.activeTab);
}
panel1.port.on("show-tag",function(tag){
getActiveTabWorker().port.emit("show-tag", tag);
console.log("worker emited");
});
panel1.port.on("clear-tag",function(tag){
getActiveTabWorker().port.emit("clear-tag", tag);
console.log("worker emited");
})
;
痛苦.js:
$("#imgD").click(function() {
if ($(this).is(":checked")) {
self.port.emit("clear-tag","img");
console.log("panel emited");
} else {
self.port.emit("show-tag","img");
console.log("panel emited");
}
});
$("#aD").click(function() {
if ($(this).is(":checked")) {
self.port.emit("clear-tag","a");
console.log("panel emited");
} else {
self.port.emit("show-tag","a");
console.log("panel emited");
}
});
$("#iframeD").click(function() {
if ($(this).is(":checked")) {
self.port.emit("clear-tag","iframe");
console.log("panel emited");
} else {
self.port.emit("show-tag","iframe");
console.log("panel emited");
}
});
清除.js:
function tagHide (tag, hide) {
$(tag).each(function() {
if (hide === false) {
$(this).css({
"visibility": "visible"
});
} else {
$(this).css({
"visibility": "hidden"
});
}
});
}
self.port.on('show-tag', function(tag) {
tagHide(tag, false);
});
self.port.on('clear-tag', function(tag) {
tagHide(tag, true);
});
为什么我不断得到 self.port 没有在 painel.js 中定义?