ReferenceError:require没有定义 [英] ReferenceError: require is not defined

查看:226
本文介绍了ReferenceError:require没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Mozilla Firefox插件。

我已经建立了一个面板,并附加了一个内容脚本。我需要在内容脚本和main.js之间进行通信。我正在使用addon-sdk的端口api。但是,由于某种原因,我甚至无法通过这两者之间得到一个简单的消息。



当我使用cfx测试我的插件时,我不断收到以下错误: ReferenceError:require是没有定义的



任何想法有什么不对?

popup.js

  var self = require(sdk / self); 
self.port.on(dataToPopup,function(data){
$(p.test).text(data);
});

第一行本身就是错误。



main.js

  var {ToggleButton} = require('sdk / ui / button / toggle'); 
var self = require(sdk / self);

var button = ToggleButton({
id:my-button,
label:my button,
icon:{
16 :./images/tsfm16px.png
},
onChange:handleChange
});
$ b var panel = require(sdk / panel)。Panel({
contentURL:self.data.url(html / popup.html),
contentScriptFile: self.data.url(scripts / jquery-1.9.1.min.js),self.data.url(scripts / jquery-ui.js),self.data.url(scripts / popup。 js)],
onHide:handleHide
});

函数handleChange(状态){
if(state.checked){
panel.show({
position:button
});
console.log(panel opened);



function handleHide(){
button.state('window',{checked:false});
console.log(panel closed);

$ b $ panel.on(show,function(){
panel.port.emit(dataToPopup,flow);
console。日志(数据发送);
});

不会为main.js抛出同样的错误



以前有人遇到过这个问题吗?

解决方案

内容脚本不能访问 require 。相反, self 已被声明



只要移除 popup.js (但不是 main.js )需要行。



请参阅使用港口


I'm currently working on a Mozilla Firefox addon.

I have set up a panel and attached a content script to it. I need to communicate between the content scripts and main.js. I'm using the port api of the addon-sdk for this. However for some reason, I can't even get a simple message through between the two.

I am constantly receiving the following error when I test my addon using cfx: "ReferenceError: require is not defined"

Any idea what's wrong?

popup.js

var self = require("sdk/self");
self.port.on("dataToPopup", function (data) { 
$("p.test").text(data);
});

The error is thrown for the first line itself.

main.js

var { ToggleButton } = require('sdk/ui/button/toggle');
var self = require("sdk/self");

var button = ToggleButton({
    id: "my-button",
    label: "my button",
    icon: {
        "16": "./images/tsfm16px.png"
    },
    onChange: handleChange
});

var panel = require("sdk/panel").Panel({
    contentURL: self.data.url("html/popup.html"),
    contentScriptFile: [self.data.url("scripts/jquery-1.9.1.min.js"), self.data.url("scripts/jquery-ui.js"), self.data.url("scripts/popup.js")],    
    onHide: handleHide
});

function handleChange(state) {
    if (state.checked) {
        panel.show({
            position: button
        });
    console.log("panel opened");    
    }
}

function handleHide() {
    button.state('window', {checked: false});
    console.log("panel closed");
}

panel.on("show", function() {
    panel.port.emit("dataToPopup", "flow");
    console.log("data sent");
});

The same error is not being thrown for main.js

Anybody experienced this before?

解决方案

Content scripts do not have access to require. Instead self is already declared.

Just remove the require line from popup.js (but not main.js).

See Communicating using "port".

这篇关于ReferenceError:require没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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