端口错误:无法建立连接。 '未定义'的事件处理程序中出错。 (铬扩展) [英] Port error: Could not establish connection. Error in event handler for 'undefined'. (chrome extension)
问题描述
我基本上正在尝试执行Chrome扩展程序文档中的代码。这就是我卡住的地方。
我试图从内容脚本>>背景页面(用于XHR)传递值。
并且在控制台中输入的错误是:
lockquote
端口错误:无法建立连接。接收结束不存在。 miscellaneous_bindings:236
未定义的事件处理程序中的错误:无法读取未定义的属性'告别'TypeError:无法读取未定义的
属性'告别'扩展名://bccajmddlghglocgmkpkpbiankmhlfkc/js/content_script.js:23:23
在miscellaneous_bindings:281:11
chrome.Event.dispatchToListener(event_bindings:387:21)
$ at chrome.Event.dispatch_(event_bindings:373:27)
在chrome.Event.dispatch(event_bindings:393:17)
在Object.chromeHidden。 Port.dispatchOnDisconnect(miscellaneous_bindings:239:27)
content_script.js 看起来像这样:
函数func1(){
$(function(){
$ '.genericStreamStory')。each(function(){
var link = $(this).find('。uiStreamSource a')。attr('href');
$(这一点).find( 'uiStreamFooter')。找到( '.a1')。remove();
$(this).find('。uiStreamFooter')。append(< span class ='a1'style ='color:red!important;'> (示例< / span>);
$(this).find('。a1')。click(function(){
alert('hi') ;
chrome.extension.sendMessage({greeting:hello},function(response){
console.log(response.farewell);
});
console.log('testing');
}); //单击处理程序结束
}); //每个函数结束
});
$ b func1();
window.setInterval(func1(),1000);
发送消息语句 取自 Chrome扩展程序文档。 background.js : onMessage Listener的代码取自Chrome文档。 ,最后是 manifest.json : 看起来好像侦听器对于处理conrent_script.js发送的消息似乎没有'可用'。 可能是什么错误? 打开后台页面的控制台( ) )在哪里可以从Chrome扩展中的background.js中读取控制台消息?)。 您会看到像ReferenceError:$未定义的错误,因为您使用的是 通常,端口错误表示您正在尝试发送消息,而没有人正在侦听消息。在这种情况下,监听器不会因为错误而受到束缚,但通常您会发现序列(事件绑定,事件触发)的定时不准确,需要修复。 I'm basically trying to execute the code found in Chrome's Extension documentation. And that's where I'm stuck. I'm trying to pass values from Content script >> background page (for XHR). And the error im getting in the console is : Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:236
content_script.js looks like this : The send message statement is taken from the Chrome Extension Documentation. background.js : The code for onMessage Listener is taken from the Chrome documentation. and finally, manifest.json : Looks like the listener doesn't seem to be 'available' for handling the message sent by conrent_script.js. What could be the error? Open the background page's console (Where to read console messages from background.js in a Chrome extension?). You would see an error like "ReferenceError: $ is not defined", because you're using Generally, a "Port error" indicates that you're trying to send a message while no-one is listening for messages. In this case, the listener is not bound because of an error, but often you'll find that the sequence (event binding, event triggering) is badly timed and needs to be fixed. 这篇关于端口错误:无法建立连接。 '未定义'的事件处理程序中出错。 (铬扩展)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! chrome.extension.sendMessage({greeting:hello},function(response){
console.log response.farewell);
}); $ b
$ b
$(function(){
chrome.extension.onMe ssage.addListener(
函数(request,sender,sendResponse){
console.log(background);
console.log(sender.tab?来自内容脚本:+ sender.tab.url:来自扩展名);
if(request.greeting ==hello)
sendResponse({farewell:goodbye});
}); //结束onMessage监听器
});
{
name:Facebook123,
version:0.1,
description:Sample,
content_scripts:[
{
匹配:[https://*.facebook.com/*],
js:[
/js/external/jquery.js,
/js/content_script.js
}
],
背景:{
scripts:[/ js /background.js]
},
manifest_version:2
}
content_script.js中的Click Handler是加工。当我点击跨度'a1'时,弹出'hi'提示框并在控制台打印'测试'信息。
$(function(){});
没有加载jQuery。
您不必在后台页面中等待domready事件,因此删除 $(function(){
和});
会解决问题。另一种解决方法是包含jQuery:
background:{
scripts:[
/js/external/jquery.js,
/js/background.js
]
},
Error in event handler for 'undefined': Cannot read property 'farewell' of undefined TypeError: Cannot read property 'farewell' of undefined
at chrome-extension://bccajmddlghglocgmkpkpbiankmhlfkc/js/content_script.js:23:23
at miscellaneous_bindings:281:11
at chrome.Event.dispatchToListener (event_bindings:387:21)
at chrome.Event.dispatch_ (event_bindings:373:27)
at chrome.Event.dispatch (event_bindings:393:17)
at Object.chromeHidden.Port.dispatchOnDisconnect (miscellaneous_bindings:239:27)function func1(){
$(function() {
$('.genericStreamStory').each(function(){
var link = $(this).find('.uiStreamSource a').attr('href');
$(this).find('.uiStreamFooter').find('.a1').remove();
$(this).find('.uiStreamFooter').append("<span class='a1' style='color:red !important;'> · Sample</span>");
$(this).find('.a1').click(function(){
alert('hi');
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
console.log('testing');
}); //end of click handler
}); //end of 'each' function
});
}
func1();
window.setInterval("func1()", 1000);
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
$(function() {
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
console.log("background");
console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});// end of onMessage listener
});
{
"name": "Facebook123",
"version": "0.1",
"description": "Sample",
"content_scripts": [
{
"matches": ["https://*.facebook.com/*"],
"js": [
"/js/external/jquery.js",
"/js/content_script.js"
]
}
],
"background": {
"scripts": ["/js/background.js"]
},
"manifest_version": 2
}
The Click Handler in content_script.js is working. The 'hi' alert box pops up and the message 'testing' gets printed in the console when I click on the span 'a1'. $(function() { });
without having loaded jQuery.
You don't have to wait for the domready event in the background page, so removing $(function(){
and });
would solve the problem. Another way to get around is to include jQuery:"background": {
"scripts": [
"/js/external/jquery.js",
"/js/background.js"
]
},