admin管理员组文章数量:1435262
here is my setup
background.js
var port = null;
function setPort() {
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
port = chrome.tabs.connect(tabs[0].id, {name: "CONTENTSCRIPT"});
});
}
// when i click on something, get the port and send a message
function clickHandler(e) {
setPort();
if (port) {
port.postMessage({key: 'message', value: true});
}
}
contentscript.js
chrome.runtime.onConnect.addListener(function (port) {
if (port.name == "CONTENTSCRIPT") {
port.onMessage.addListener(function (msg) {
console.log(msg);
});
}
});
what i am doing is clicking on a contextMenu button on random tabs and trying to send a meessage.
what happens is that first tine i click, nothing happens, no errors. the second time i click the message goes through.
if i switch to another tab, and click on the menu button, i get the Error: Attempting to use a disconnected port object
error. If i click again the message gets sent successfully
I've tried to use var port = chrome.runtime.connect({name: "CONTENTSCRIPT"});
but that errors out with a disconnected port every time
ideas?
here is my setup
background.js
var port = null;
function setPort() {
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
port = chrome.tabs.connect(tabs[0].id, {name: "CONTENTSCRIPT"});
});
}
// when i click on something, get the port and send a message
function clickHandler(e) {
setPort();
if (port) {
port.postMessage({key: 'message', value: true});
}
}
contentscript.js
chrome.runtime.onConnect.addListener(function (port) {
if (port.name == "CONTENTSCRIPT") {
port.onMessage.addListener(function (msg) {
console.log(msg);
});
}
});
what i am doing is clicking on a contextMenu button on random tabs and trying to send a meessage.
what happens is that first tine i click, nothing happens, no errors. the second time i click the message goes through.
if i switch to another tab, and click on the menu button, i get the Error: Attempting to use a disconnected port object
error. If i click again the message gets sent successfully
I've tried to use var port = chrome.runtime.connect({name: "CONTENTSCRIPT"});
but that errors out with a disconnected port every time
ideas?
Share Improve this question edited Apr 8, 2014 at 17:30 Patrioticcow asked Apr 8, 2014 at 17:20 PatrioticcowPatrioticcow 27.1k76 gold badges221 silver badges340 bronze badges1 Answer
Reset to default 4Your problem lies in the fact that chrome.tabs.query
is asynchronous.
You execute setPort()
, that immediately returns before query
executes the callback and sets up port. At this moment, port
is either null or refers to your previous tab's port.
Therefore, you either get no error, or an error because the old port is invalid.
After that happened, the callback in query
gets executed and port
is set up for the next munication.
So, to fix that, you need to send the message after the port is set up in the call chain. Example:
function setPort(callback) {
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
port = chrome.tabs.connect(tabs[0].id, {name: "CONTENTSCRIPT"});
callback(port);
});
}
function clickHandler(e) {
setPort( function (port) {
if (port) { port.postMessage({key: 'message', value: true}); }
});
}
Edit: by the way, you're supposed to reuse a port, it's kind of the point. If you're re-establishing the connection every time, you're better off with sendMessage
, though I suppose you only used the code above for testing.
本文标签:
版权声明:本文标题:javascript - one more time about: Error: Attempting to use a disconnected port object, how to? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744773195a2624466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论