self.port is undefined error [on mozilla add-on SDK 1.8]

那年仲夏 提交于 2019-12-12 05:48:24

问题


Following this guide, I did a little variation and I keep receiving this error:

self.port is undefined at base.js[my content code]

here my add-on code:

PageMod({
    include: [data.url('base.html')],
    contentScriptWhen: 'start',
    contentScriptFile: [data.url('jquery-1.7.2.min.js'),data.url('jquery-ui-1.8.21.custom.min.js'),data.url('base.js')],
    contentStyleFile: [data.url('css/mint-choc/jquery-ui-1.8.21.custom.css'),data.url('css/base.css')],
    onAttach: function(worker) {
            worker.port.on("request", function(txt) {
                console.log(txt);
                worker.port.emit("response","response for "+txt);
            });
    }
});

and my content code:

// initialized by jQuery
$(function() {
    self.port.on("response",function(txt){console.log("response "+txt);}) //this line is where the error point to.
    $('#tabs').tabs();
    testLoadLinks();
});

function testReceiveRequest(request) {
    console.log('received:',request);
    self.port.emit("request",request);
}

I'm using the browser editor with the SDK version 1.8, can someone please help me fix this?


回答1:


After some testing and debugging, I found out my two mistakes and i'm sorry i didn't put the whole code because I thought it wasn't related:

I had this in base.html

<link type="text/css" href="css/mint-choc/jquery-ui-1.8.21.custom.css" rel="stylesheet" />
<link type="text/css" href="css/base.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.21.custom.min.js"></script>
<script type="text/javascript" src="js/base.js"></script>

and this on main.js

PageMod({
include: [data.url("base.html")],
contentScriptWhen: 'start',
contentScriptFile: [data.url('jquery-1.7.2.min.js'),data.url('jquery-ui-1.8.21.custom.min.js'),data.url('base.js')],
contentStyleFile: [data.url('css/mint-choc/jquery-ui-1.8.21.custom.css'),data.url('css/base.css')],
onAttach: function(worker) {
        console.log("attaching...")
        worker.port.on("request", function(txt) {
            console.log(txt);
            worker.port.emit("response","response for "+txt);
        });
}
});

My scripts are on the /data/js/ folder, they couldn't be found thus not loading the pageMod part of script in the main.js, but they were being loaded by the content script itself inside the DOM, where self.port is undefined. I just removed the script and styles tags from the base.html because they are already in the pageMod, and fixed the path in the data.url('js/base.js') and the others. Now i learned the scripts in the add-on scope can access the DOM scope and change it, but scripts inside the DOM cannot (they can?) communicate with the add-on scope. If you want scripts to interact with the add-on, you should left them in the add-on scope. I hope to help someone who commits the same mistake. Thanks canuckistani.



来源:https://stackoverflow.com/questions/11426406/self-port-is-undefined-error-on-mozilla-add-on-sdk-1-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!