最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReferenceError: require is not defined - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Dec 22, 2015 at 19:30 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Jun 25, 2014 at 14:57 Sriram SridharanSriram Sridharan 1031 gold badge1 silver badge5 bronze badges 2
  • Are you having the script file in your HTML? Because I think its not being loaded. – Afzaal Ahmad Zeeshan Commented Jun 25, 2014 at 14:58
  • no. i added it as a content script from main.js – Sriram Sridharan Commented Jun 25, 2014 at 15:04
Add a comment  | 

1 Answer 1

Reset to default 14

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".

发布评论

评论列表(0)

  1. 暂无评论