Google chrome extension Chrome ext选项卡:端口错误:无法建立连接。接收端不存在
我尝试连接到刚刚创建的新选项卡,我的最终任务是更新所有打开选项卡上的html小部件 不管怎样,每次我尝试通过id连接到选项卡时,我都会出错。im使用版本23.0.1271.64 mGoogle chrome extension Chrome ext选项卡:端口错误:无法建立连接。接收端不存在,google-chrome-extension,communication,Google Chrome Extension,Communication,我尝试连接到刚刚创建的新选项卡,我的最终任务是更新所有打开选项卡上的html小部件 不管怎样,每次我尝试通过id连接到选项卡时,我都会出错。im使用版本23.0.1271.64 m var channelTabs = []; function isInclude(arr,obj) { return (arr.indexOf(obj) != -1); } chrome.tabs.onCreated.addListener(function(tab) { // ad
var channelTabs = [];
function isInclude(arr,obj) {
return (arr.indexOf(obj) != -1);
}
chrome.tabs.onCreated.addListener(function(tab) {
// add tab when opened
if (channelTabs.indexOf(tab.id) == -1) {
channelTabs.push(tab.id);
}
chrome.windows.getCurrent(function(win)
{
// get an array of the tabs in the window
chrome.tabs.getAllInWindow(win.id, function(tabs)
{
for (i in tabs) // loop over the tabs
{
// if the tab is not the selected one
if(isInclude(channelTabs,tabs[i].id))
{
if(/^(https?|file):/.test(tabs[i].url))
{
console.log('Debug Background sending update to open tab id:'+tabs[i].id);
var port = chrome.tabs.connect(tabs[i].id,{name: "content_tab_request"});
port.postMessage({resp: "tab_update",data:"some string song1"});
}
}
}
});
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo,tab) {
// Example: allow http:, https: and file:
if (/^(https?|file):/.test(tab.url)) {
console.log('Debug Background sending onUpdated to open tab id:'+tabId);
var port = chrome.tabs.connect(tabId,{name: "content_tab_request"});
port.postMessage({resp: "tab_update",data:"some string song1"});
}
});
});
但每次尝试chrome.tabs.connect时,它都会给我:
Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:235
chromeHidden.Port.dispatchOnDisconnect miscellaneous_bindings:235
chrome.Event.dispatch_ event_bindings:371
dispatchArgs event_bindings:249
chromeHidden.Event.dispatchEvent
以及内容脚本:
// Register this tab to the background script
var port = chrome.extension.connect({name: "content_request"});
port.postMessage({req: "Hello"});
port.onMessage.addListener(function(msg) {
if (msg.resp == "World")
{
port.postMessage({answer: "good"});
}
else if(msg.answer == "bye")
{
console.log('Debug contentscript.js reciving answer from background msg.answer:'+msg.answer);
}
else
{
console.log('Debug contentscript.js reciving answer from background is wrong:'+msg);
}
if(port.name == "content_tab_request")
{
console.log('Debug contentscript.js reciving request from background Tab function:'+msg);
if(msg.resp=="tab_update ")
{
var data_recive = msg.data;
console.log('Debug contentscript.js reciving request data from background Tab to update page data_recive:'+data_recive);
}
}
});
json文件:
{
"background": {
"page": "background.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["contentscript.js"],
"run_at": "document_start",
"all_frames": true
}
],
"web_accessible_resources": [
"script_inpage.js"
],
"browser_action": {
"default_icon": "icon19.png",
"default_popup": "popup.html",
"default_title": "Simple test"
},
"content_security_policy": "script-src 'self'; media-src *; object-src 'self'",
"description": "Simple test.",
"icons": {
"128": "icon128.png",
"16": "icon16.png",
"32": "icon32.png",
"48": "icon48.png"
},
"manifest_version": 2,
"minimum_chrome_version": "20",
"name": "Simple test",
"permissions": [
"unlimitedStorage",
"http://*/",
"<all_urls>",
"tabs"
],
"version": "2.6"
}
{
“背景”:{
“页面”:“background.html”
},
“内容脚本”:[
{
“匹配项”:[“”],
“js”:[“contentscript.js”],
“运行时间”:“文档开始时间”,
“所有帧”:正确
}
],
“网络可访问资源”:[
“script_inpage.js”
],
“浏览器操作”:{
“默认图标”:“icon19.png”,
“默认弹出窗口”:“popup.html”,
“默认标题”:“简单测试”
},
“内容安全策略”:“脚本src'self';媒体src*;对象src'self',
“说明”:“简单测试”,
“图标”:{
“128”:“icon128.png”,
“16”:“icon16.png”,
“32”:“icon32.png”,
“48”:“icon48.png”
},
“清单版本”:2,
“最低chrome版本”:“20”,
“名称”:“简单测试”,
“权限”:[
“无限制存储”,
“http://*/”,
"",
“选项卡”
],
“版本”:“2.6”
}
顺便说一下,从内容脚本到后台的连接工作得很好 好的,似乎您正在尝试同步异步事件,在简化代码后,我对其进行了修改,实现了创建新选项卡时向所有选项卡发布消息的功能要求
Output:
我可以在所有选项卡上看到此消息
manifest.json
好的,似乎您正在尝试同步异步事件,在简化代码后,我对其进行了修改,并实现了创建新选项卡时向所有选项卡发布消息的功能要求
Output:
我可以在所有选项卡上看到此消息
manifest.json
此外,一些chrome扩展可能会产生此错误。我有这个问题,问题是扩展(为了把物品放进口袋)。 错误是: 端口错误:无法建立连接。接收端不存在。杂项_绑定:235 chromeHidden.Port.dispatchOnDisconnect
此外,一些chrome扩展可能会产生此错误。我有这个问题,问题是扩展(为了把物品放进口袋)。 错误是: 端口错误:无法建立连接。接收端不存在。杂项_绑定:235 chromeHidden.Port.dispatchOnDisconnect
感谢您的回答,在后台脚本中它没有给出任何错误,但是在内容脚本中,它没有触发onMessage,这也是为什么我不能使用端口对象的持久连接?@user63898:您可以使用持久连接,但在
postMessage()之前,您应该确保receiver存在
在您的场景中缺少该选项,因此我建议sendMessage()
api如何确保这一点?我确实检查了标签是否存在。另外,我正在测试,我看到了标签(:ok,我用我的json文件更新了,mybe有问题吗?我只是无法从后台得到任何内容的信息script@user63898:在本例中,使用sendMessage()时,我相信您没有从选项卡向background.js发送确认
将解决您的问题;为避免无法连接错误
对于后台->选项卡的长期连接,场景是等待新选项卡完全加载并准备好与端口侦听器注入内容脚本,chrome.tabs.onCreated.addListener不保证新选项卡完全加载谢谢回答,在后台脚本中,它没有给出任何错误,但在内容脚本中,它没有触发onMessage,这也是为什么我不能使用端口对象的持久连接?@user63898:可以使用持久连接,但在postMessage()之前,应该确保receiver存在
在您的场景中缺少该选项卡,因此我建议使用sendMessage()
ap如何确保这一点?我确实检查了选项卡是否存在。此外,我正在测试并看到了该选项卡(:好的,我用我的json文件更新了,mybe init有问题吗?我只是不能从后台得到任何信息script@user63898:在本例中,使用sendMessage()时,我相信您没有从选项卡向background.js发送确认
将解决您的问题;为避免无法连接错误
对于后台->选项卡的长期连接,场景是等待新选项卡完全加载并准备好使用端口侦听器注入内容脚本,chrome.tabs.onCreated.addListener不保证新选项卡完全加载
chrome.tabs.onCreated.addListener(function(tab) {
chrome.windows.getCurrent(function(win){
chrome.tabs.getAllInWindow(win.id, function(tabs){
for (i=0;i<tabs.length;i++){
console.log(tabs[i]);
chrome.tabs.sendMessage(tabs[i].id,{name: "content_tab_request"});
}
});
});
});
chrome.extension.onMessage.addListener(function(msg) {
console.log("Message Recieved "+ msg);
});