ReferenceError:Worker不是在简单的Firefox扩展中定义的 [英] ReferenceError: Worker is not defined in simple Firefox Extension

查看:187
本文介绍了ReferenceError:Worker不是在简单的Firefox扩展中定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个令人难以置信的简单Firefox扩展。我使用 cfx init 来创建目录结构,并在 lib / main.js data / my_worker.js



main.js 如下: p>

  var worker = new Worker(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);
};

然后运行: cfx run 扩展名。结果如下:

lockquote
(addon-sdk-1.17)me:lib me $ cfx run在
' /Applications/Firefox.app/Contents/MacOS/firefox-bin'。在
'/var/folders/p1/zzdzcrrx6pq96hgsmy5xjqmh0000gp/T/tmp57OYe9.mozrunner'中使用配置文件

console.error:test:Message: ReferenceError:工人未定义
堆栈:
@resource:// jid1-zmowxggdley0aa-at-jetpack / test / lib / main.js:1:9 CuddlefishLoader / options< .load @ resource://gre/modules/commonjs/sdk/loader/cuddlefish.js:129:18
run @ resource:// gre / modules /commonjs/sdk/addon/runner.js:138:19
启动/

我试着把 my_worker。 js data lib 文件夹中,但都不起作用。这看起来像是一个超简单的扩展。有什么可能会出错?

解决方案

这可能是一个好主意,阅读一些介绍文档

add-on SDK 使用需要导入的模块 main.js ,使用 require 函数。没有全局的 Worker 对象,因为这个错误非常明确。内容脚本需要附加到某个HTML页面;它不能单独存在。获得工作人员的三种最常见的方式是将内容脚本附加到

标签

  var tabs = require(sdk / tabs); 
$ b $ tabs.on('ready',function(tab){
var worker = tab.attach({
contentScript:
'document.body.style。 border =5px solid red;'
});
});

匹配URL或regex数组的任何页面,使用 PageMod

  var tag =p; 
var data = require(sdk / self)。data;
var pageMod = require(sdk / page-mod);
$ b $ 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);
});
}
});

一个不可见的背景页面 PageWorker ,这可能就是你要去的对于

  pageWorker = require(sdk / page-worker)。Page({
contentScript:console。 log(document.body.innerHTML);,
contentURL:http://en.wikipedia.org/wiki/Internet
});

请注意 panel s worker s,因为它们有 onMessage port 属性。



还要注意 onMessage 拼写的是大写字母 M


I'm creating an incredibly simple Firefox extension. I used cfx init to create the directory structure and have code in lib/main.js and data/my_worker.js

main.js is as follows:

var worker = new Worker("my_worker.js");

worker.onmessage = function(e) {
    console.log(e.data);
};

worker.postMessage("Bobby");

and my_worker.js is as follows:

self.onmessage = function(e) {
    self.postMessage("Hello " + e.data);
};

Then I run: cfx run to run the extension. The results are as follows:

(addon-sdk-1.17)me:lib me$ cfx run Using binary at '/Applications/Firefox.app/Contents/MacOS/firefox-bin'. Using profile at '/var/folders/p1/zzdzcrrx6pq96hgsmy5xjqmh0000gp/T/tmp57OYe9.mozrunner'. console.error: test: Message: ReferenceError: Worker is not defined Stack: @resource://jid1-zmowxggdley0aa-at-jetpack/test/lib/main.js:1:9 CuddlefishLoader/options<.load@resource://gre/modules/commonjs/sdk/loader/cuddlefish.js:129:18 run@resource://gre/modules/commonjs/sdk/addon/runner.js:138:19 startup/

I tried putting my_worker.js in both the data and lib folders but neither works. This seems like a SUPER simple extension. What could be going wrong?

解决方案

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

a tab

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 panels work very similarly to workers in that they have onMessage and port attributes.

Note also that onMessage is spelt with a capital M

这篇关于ReferenceError:Worker不是在简单的Firefox扩展中定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆