有时tabs.getSelected回调不会运行

时间:2011-08-05 17:58:16

标签: google-chrome-extension

我已经编写了Google Chrome扩展程序(https://chrome.google.com/webstore/detail/fhmcfamnddgoloojehbeokifhaiiebfm),我注意到该扩展程序可以在我的Linux桌面上运行,但不是我的Linux笔记本电脑(两者都有)在Debian unstable中运行Chromium 13.0.782.107~r94237-1)

似乎我传递给chrome.tabs.getSelected的回调不能在我的笔记本电脑上运行,除非我在调试器中打开了这个弹出页面。 (但它在我的桌面上完美运行)任何想法发生了什么?

以下是相关代码:

<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>

1 个答案:

答案 0 :(得分:1)

我认为你在回调完成运行之前正在关闭窗口。尝试:

function goTo(url){
    ...
    chrome.tabs.getSelected(null,function(tab){
        chrome.tabs.update(tab.id,{"url":url});
        window.close();
    });
}