Sometimes tabs.getSelected callback doesn't run
I've written a Google Chrome extension (https://chrome.google.com/webstore/detail/fhmcfamnddgoloojehbeokifhaiiebfm), and I'm noticing that the extension works on my Linux desktop, but not my Linux laptop (both running the Chromium 13.0.782.107~r94237-1 in Debian unstable)
It seems that the callback I'm passing to chrome.tabs.getSelected
doesn't run on my laptop, except when I have this popup page open in the debugger. (But it works perfectly on my desktop) Any idea what's going on?
Here's the code in question:
<html>
<head>
<script type="text/javascript">
function goTo(url){
if (url.search("://") == -1 &&
url.search("@") != -1 &&
url.search("mailto:") == -1 ) url = "mailto:"+url;
else if (url.search("://") == -1 ) url = "http://" + url;
url = url.replace(/\s/g,"");
console.log(url);
chrome.tabs.getSelected(null,function(tab){
chrome.tabs.update(tab.id,{"url":url});
});
window.close();
}
开发者_运维知识库
function pasteHandler(e) {
var t = e.target.type;
if (t == "textarea" || t == "text" || t == "password"
|| e.target.isContentEditable) {
return;
}
var url = e.clipboardData.getData("text/plain").replace(/^\s+|\s+$/g, '');
goTo(url)
}
function textBoxEnterPressed(e){
if(e.keyCode == 13){
goTo(document.getElementById('edit').value);
return false;
}
return true;
}
</script>
</head>
<body onpaste="pasteHandler(event)">
<table border="0" cellpadding="0" cellspacing="0" style="float:left">
<tr><td>
Paste in the text box to edit the URL first,
or paste outside the box to go straight there.
</td></tr>
<tr><td>
<input type="text" name="edit" id="edit" size="100"
onkeypress="return textBoxEnterPressed(event)"/>
</td></tr>
</table>
</body>
</html>
I think you are closing window before callback finishes running. Try:
function goTo(url){
...
chrome.tabs.getSelected(null,function(tab){
chrome.tabs.update(tab.id,{"url":url});
window.close();
});
}
精彩评论