Firefox Addon SDK消息传递错误

时间:2015-12-09 11:12:00

标签: javascript firefox firefox-addon firefox-addon-sdk

我试图在pageMod和内容脚本之间传递消息。

我将此代码与pageMod一起使用

var gmailPageMod = pageMod.PageMod({
            include: "https://mail.google.com/*",
            contentScriptWhen: "ready",
            contentScriptFile: [self.data.url('js/script.js')],
            contentStyleFile: [
                self.data.url("css/angular.min.css"),
                self.data.url("css/style.css")
            ],
            onAttach: function(worker) {
                try {
                    console.log("adding worker");
                    gmail_workers.push(worker);
                } catch (error) {
                    console.log("error", error);
                }

                worker.port.on("getTalentGamil_recipient_change", function(email) {
                    console.log(email);
                });
                try {
                    console.log("updating SS");
                    console.log(simpleStorage);
                    worker.port.emit("updateSimpleStorage", simpleStorage);
                } catch (error) {
                    console.log("error", error);
                }

            });

和script.js

self.port.on("updateSimpleStorage", function(simpleStorage) {
    console.log('updateSimpleStorage from script.js', simpleStorage);
    var e = new CustomEvent('updateSimpleStorage', {
        'detail': simpleStorage
    });
    window.dispatchEvent(e);
});

然而,这给了我一个错误Message: [Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: <unknown filename> :: <TOP_LEVEL> :: line 0" data: no]

我不明白为什么?因为我认为我正在完成最新工作here。我很感激有关此事的任何帮助

2 个答案:

答案 0 :(得分:0)

我在这里阅读 - https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/using_port#Accessing_port_in_the_Add-on_Script

所以你从main.js发出了这个:

worker.port.emit("updateSimpleStorage", simpleStorage);

你想知道为什么它没有在script.js中调用回调对吗?

以下是我对可能出现的问题的看法:

  1. 是否有可能没有加载contentScript script.js?
  2. 什么是simpleStorage?它是可以序列化的吗?
  3. 如果没有加载script.js,我建议你试试这个:

    main.js onAttach中监听这样的加载事件:

    var gmailPageMod = pageMod.PageMod({
                include: "https://mail.google.com/*",
                contentScriptWhen: "ready",
                contentScriptFile: [self.data.url('js/script.js')],
                contentStyleFile: [
                    self.data.url("css/angular.min.css"),
                    self.data.url("css/style.css")
                ],
                onAttach: function(worker) {
                    try {
                        console.log("adding worker");
                        gmail_workers.push(worker);
                    } catch (error) {
                        console.log("error", error);
                    }
    
                    worker.port.on("getTalentGamil_recipient_change", function(email) {
                        console.log(email);
                    });
    
                    worker.port.on('iLoaded', function() {
                        try {
                            console.log("updating SS");
                            console.log(simpleStorage);
                            worker.port.emit("updateSimpleStorage", simpleStorage);
                        } catch (error) {
                            console.log("error", error);
                        }
                    });
    
                });
    

    请注意我是如何将其包裹在iLoaded

    中的

    然后在script.js中发出iLoaded这样的消息:

    self.port.on("updateSimpleStorage", function(simpleStorage) {
        console.log('updateSimpleStorage from script.js', simpleStorage);
        var e = new CustomEvent('updateSimpleStorage', {
            'detail': simpleStorage
        });
        window.dispatchEvent(e);
    });
    
    self.port.emit('iLoaded');
    

答案 1 :(得分:0)

我遇到了this,根据它,CustomEvents不再有效,所以我使用了一个带有FF Addon SDK原生消息传递路径的环形消息来获取我的信息。