[英]Chrome Extension: Can't send message to tab with id of -1
I've checked numerous questions regarding message passing in a Chrome extension but I haven't found much specifically relating to this. 我检查了许多有关在Chrome扩展程序中传递消息的问题,但没有发现与此相关的很多问题。
I'm using the chrome.devtools* APIs and I'm having trouble sending messages between content scripts when the developer toolbar is docked. 我使用的是chrome.devtools * API,并且停靠了开发人员工具栏时,无法在内容脚本之间发送消息。 Everything works fine when it is not docked ie floating.
不停靠(即浮动)时,一切正常。
Here's a brief example of what I'm doing. 这是我正在做的简短示例。
devtools.js devtools.js
chrome.devtools.panels.create("myExtension", "img/icon.png",
"/panel.html", function(extensionPanel) {
var myData; //this variable gets manipulated before a user
//clicks on the panel
extensionPanel.onShown.addListener(function(panelWindow) {
chrome.extension.sendMessage({greeting: "update my data", data: myData}, function(response) {});
});
});
Then in my background script (eventPage.js) I listen for this message and pass it on to panel.js 然后在我的后台脚本(eventPage.js)中,我收听此消息并将其传递给panel.js
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.greeting == "update my data"){
chrome.tabs.sendMessage(sender.tab.id, {greeting: "show data", showResource: request.data}, function(response) {});
}
});
And then finally I listen for the 'show data' call in my panel.js (which is loaded from panel.html) 最后,我在panel.js(从panel.html加载)中监听“显示数据”调用
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.greeting == "show data") {
//do stuff with my data here
});
Essentially, I need to pass messages between devtools.js to panel.js but the only way to do it is by using the background script as an intermediary. 本质上,我需要在devtools.js之间将消息传递到panel.js,但是唯一的方法是使用后台脚本作为中介。
devtools.js -> background.js -> panel.js. devtools.js-> background.js-> panel.js。
This actually works fine as long as the dev tools panel is not docked to the window. 只要未将开发工具面板停靠在窗口中,这实际上就可以正常工作。 When it is docked I get an error because sendMessage() won't except a tab id of -1, which is what sender.tab.id equals when the dev tools are docked to the window.
当它停靠时,我会收到一个错误消息,因为sendMessage()除了制表符ID -1以外不会其他,这是将开发工具停靠在窗口中时sender.tab.id所等于的值。 I also tried using chrome.tabs.connect - long lasting connections - but ran into the same problem.
我也尝试使用chrome.tabs.connect-持久连接-但遇到了同样的问题。
I also just found out recently how to do this. 我最近还发现了如何执行此操作。
A technique used by "Google Pagespeed" is to get the tab id of the inspected window and pass it back and forth between the extension and background using a port. “ Google Pagespeed”使用的一种技术是获取被检查窗口的标签ID,并使用端口在扩展名和背景之间来回传递。
Whenever you want to send the extension a message, you look for the tab id and get its port. 每当您想发送扩展名消息时,都将查找选项卡ID并获取其端口。
// get the inspected windows tab id from the panel
var tabId = chrome.devtools.inspectedWindow.tabId;
// create the port
var port = chrome.extension.connect({name: 'connection'});
// keep track of the port and tab id on the background by
// sending the inspected windows tab id
port.postMessage(['connect', tabId])
var activeListeners = {};
chrome.extension.onConnect.addListener(function(port) {
port.onMessage.addListener(function(message) {
if (message[0] === 'connect') {
// get the tab id
var tabId = message[1];
// save the reference to the port
activeListeners[tabId] = port;
// make sure we clean up after disconnecting (closing the panel)
activeListeners[tabId].onDisconnect.addListener(function() {
delete activeListeners[tabId];
});
}
});
});
This is not a very thorough explanation but I hope you get the point. 这不是一个很彻底的解释,但我希望您明白这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.