Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

javascript - Closing popup window created by Google Chrome extension

I'm trying to create a Chrome extension that is a replacement for the Delicious bookmarklet. I know there's already an extension that does it, but the problem with that extension is that after you bookmark a site, the popup window stays open (as opposed to using the bookmarklet, where the popup closes itself after submitting the form. I recreated the extension and ran into the same problem.

Here's my code:

manifest.json:

{
  "name": "Delicious",
  "version": "1.0",
  "description": "Bookmark a site on Delicious",
  "background_page": "background.html",
  "permissions": [ 
    "tabs" 
  ],
  "browser_action": {
    "default_icon": "delicious.png"
  },
  "content_scripts": [
    {
      "matches": ["http://www.delicious.com/save*"],
      "js": ["contentscript.js"]
    }
  ]
}

background.html:

<html><script>
chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.getSelected(null, function(tab) {
    w = window.open('http://delicious.com/save?url='+
          encodeURIComponent(tab.url)+
          '&title='+encodeURIComponent(tab.title)+
          '&v=5&noui=1&jump=close',
        'deliciousuiv5',
        'location=yes,links=no,scrollbars=no,toolbar=no,width=550,height=550');
  });
});
</script></html>

contentscript.js:

if (document.URL == 'http://www.delicious.com/save')
{
  alert('closing...');
  self.close();
  alert('should have closed by now');
}

When I click the Delicious button, the popup comes up fine and I can save the bookmark but after I click "Save", the popup does not close. Both alerts show up, but self.close() doesn't seem to do anything. When I remove the URL check in contentscript.js, the popup comes up as normal, the first alert fires right away, and then the popup closes itself (as it should).

Why doesn't this work? It doesn't seem like Chrome is preventing me from doing self.close(). Is Delicious doing something? Is it something else?

The files are here if you want them: [link removed because drop.io went out of business]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try window.close(), but that probably wouldn't work either.

As you are creating regular window (rather than browser action popup), then you can close it using chrome.tabs.remove() from a background page. You can also detect this window from a background page. Something like:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(changeInfo.status == "loading") {
        if(tab.url == "http://www.delicious.com/save") {
            chrome.tabs.remove(tabId);
        }
    }
});

I am not sure how Chrome treats created windows though - as tabs or windows. If as windows then above code will be a little different.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...