self.port是未定义的错误[在Mozilla插件SDK 1.8] [英] self.port is undefined error [on mozilla add-on SDK 1.8]

查看:196
本文介绍了self.port是未定义的错误[在Mozilla插件SDK 1.8]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注指南,我做了一些变化,我一直在收到这个错误


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


这里是我的附加组件代码
$ b

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。 (css / base.css')],
onAttach:function(worker){
worker.port.on(request,function(txt ){
console.log(txt);
worker.port.emit(响应,响应+ txt);
});
}
});

和我的内容代码

//通过jQuery初始化
$(function(){
self.port.on(response ,function(txt){console.log(response+ txt);})//这行是错误指向的地方。
$('#tabs')。tabs();
testLoadLinks();
});

函数testReceiveRequest(请求){
console.log('received:',request);
self.port.emit(request,request);
}

我使用SDK版本1.8的浏览器编辑器,帮助我解决这个问题?

解决方案

经过一些测试和调试,我发现了我的两个错误, t把整个代码,因为我认为这是不相关的:

我有这在 base.html

 < link type =text / csshref =css / mint-choc / jquery-ui-1.8.21.custom.css = stylesheet/> 
< link type =text / csshref =css / base.css =stylesheet/>
< script type =text / javascriptsrc =js / jquery-1.7.2.min.js>< / script>
< script type =text / javascriptsrc =js / jquery-ui-1.8.21.custom.min.js>< / script>
< script type =text / javascriptsrc =js / base.js>< / script>

main.js $ b

  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(attach ...)
worker.port.on(request,function(txt){
console.log(txt);
worker.port.emit(response,response for+ txt);
});
}
});

我的脚本位于/ data / js /文件夹中,因此无法找到它们main.js中脚本的pageMod部分,但是它们是由DOM内部的内容脚本本身加载的,其中self.port是未定义的。我只是从base.html中删除脚本和样式标记,因为它们已经在pageMod中,并修复了data.url('js / base.js')和其他的路径。现在我学习了附加范围中的脚本可以访问DOM范围并对其进行更改,但DOM中的脚本无法(它们可以)与附加范围进行通信。如果您希望脚本与加载项进行交互,则应将其留在加载项范围中。我希望能帮助那些犯同样错误的人。
谢谢canuckistani。


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?

解决方案

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.

这篇关于self.port是未定义的错误[在Mozilla插件SDK 1.8]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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