Referenceerror: Worker Is Not Defined In Simple Firefox Extension
Solution 1:
It's probably a good idea to read some intro docs before starting to code.
The Firefox add-on SDK uses modules that need to be imported into main.js
using the require
function. There is no global Worker
object, as the error is pretty explicit about. A content script needs to be attached to an HTML page somewhere; it can't exist alone. The three most common ways to get a worker are by attaching a content script to
var tabs = require("sdk/tabs");
tabs.on('ready', function(tab) {
var worker = tab.attach({
contentScript:
'document.body.style.border = "5px solid red";'
});
});
any page that matches an array of URLs or regex, using a PageMod
var tag = "p";
var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "*.mozilla.org",
contentScriptFile: data.url("element-getter.js"),
onAttach: function(worker) {
worker.port.emit("getElements", tag);
worker.port.on("gotElement", function(elementContent) {
console.log(elementContent);
});
}
});
an invisible background page with PageWorker
, which is probably what you're going for
pageWorker = require("sdk/page-worker").Page({
contentScript: "console.log(document.body.innerHTML);",
contentURL: "http://en.wikipedia.org/wiki/Internet"
});
Note that panel
s work very similarly to worker
s in that they have onMessage
and port
attributes.
Note also that onMessage
is spelt with a capital M
Solution 2:
In Firefox Addon, use ChromeWorker instead of worker.
Add the following lines in main.js
var { ChromeWorker } = require("chrome");
varself = require("sdk/self");
var data = self.data;
var localWorker = new ChromeWorker(data.url("my_worker.js"));
worker.onmessage = function(e) {
console.log(e.data);
};
worker.postMessage("Bobby");
my_worker.js
self.onmessage = function(e) {
self.postMessage("Hello " + e.data);
};
put your my_worker.js file in data/ directory.
Post a Comment for "Referenceerror: Worker Is Not Defined In Simple Firefox Extension"