I am trying to create a firefox addon using their sdk, but I do not know how to make my js scripts shared. The goal is to make a panel with a form in 3 flags, which, when selected, can hide / show certain elements on the active tab.
Here are the scripts: 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"); })
;
painel.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"); } });
clear.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); });
Why do I keep getting self.port not defined in painel.js?