25

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]

Alex Grin
  • 8,121
  • 6
  • 33
  • 57
  • The window closes for me automatically. I'm running a fairly default version of Chrome if that makes any difference. – Darryl Hein Oct 16 '10 at 22:21

4 Answers4

20

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.

serg
  • 109,619
  • 77
  • 317
  • 330
  • 9
    window.close() works. If you want to close the popup after sending a request to another tab then make sure you close the popup in the callback and the tab must be sent at least an empty response. – Martins Balodis Feb 08 '12 at 21:47
  • 1
    is it possible to close the window from a content script? – chovy Mar 03 '19 at 02:41
9

I found a very easy work around to this. You just set the selected tab to True and the Popup disappears, like this...

// remove popup by selecting the tab
chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.update(tab.id, { selected: true } )
});
Hlung
  • 13,850
  • 6
  • 71
  • 90
1

getSelected not working for me, so i found this solution

chrome.tabs.create({url: 'https://www.google.com', active: false});

in background.js you need just

window.close();
0

I've found this solution: chrome.tabs.update({ active: true }); This single line of code closes the browser action's popup window. You even don't need to pass tab.id there because by default it is set to id of the current tab. I run it in background page but seems it can be run everywhere in the extension.

Konstantin Smolyanin
  • 17,579
  • 12
  • 56
  • 56
  • This unfortunately did not work for me. I am updating from similar getSelected() code posted on this page and I wrote this code exactly expecting it to work, from what the docs say. – offthat Sep 16 '13 at 01:40
  • It may depend on many circumstances. Where do you put the call of this method? Maybe inside some callback handler that don't called and so on... – Konstantin Smolyanin Sep 16 '13 at 10:47
  • I ended up just storing a reference to the popup, so that I could just call popupRef.Close() and that did the trick. – offthat Sep 17 '13 at 18:33
  • @offthat - Where did you store the reference? I want to try this too. – Sridhar Sarnobat Aug 21 '16 at 21:48