Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close a Chrome extension popup by clicking the browser action icon again

I'm developing a Chrome extension and I wanted to know if it is possible to close a popup by simply clicking again the icon that lets you open the popup: I tried anything but it looks like you must click elsewhere to close it. The docs states the onClicked event is:

Fired when a browser action icon is clicked. This event will not fire if the browser action has a popup (http://developer.chrome.com/extensions/browserAction.html#popups).

Thanks in advance.

[UPDATE] I tried the following and it half (!) works: 1. in popup.html I link popup.js; 2. popup.js reads the value of a variable contained in background.js;

var currentStatus = chrome.extension.getBackgroundPage().open;
if(currentStatus==0){
  chrome.extension.getBackgroundPage().open=1;
}else{
  chrome.extension.getBackgroundPage().open=0;
  window.close();
}

What happens: the first click opens the app, the second closes it, BUT it remains a micro popup with no content upon the icon. If I remove that, I reached my goal.

like image 916
ContentiousMaximus Avatar asked Nov 03 '12 07:11

ContentiousMaximus


2 Answers

The onClicked event is called if your extension's browser action does not define default_popup in the manifest. That note from the documentation isn't about whether the popup is currently open.

If the manifest defines default_popup then clicking the button again closes and reopens the plugin. The mousedown closes and the mouseup opens. (So clicking on the button and dragging away and releasing the mouse does close the popup, not that anyone should do this.)

I recommend setting default_popup and making a button in the html for the popup that closes the popup with window.close;, or find a point in your popup's use case where closing makes sense.

like image 56
user2573802 Avatar answered Sep 21 '22 15:09

user2573802


Well It has been a long time, and the issue/bug still persists on Chrome browsers. I've found a workaround, it's not great, but it does what I need - closes the window on a second icon click. Here's what I did in the popup javascript file:

if(localStorage.getItem('firstClick')==='true'){
    localStorage.setItem('firstClick', 'false');
    window.close();
}
else {
    localStorage.removeItem('firstClick');
    localStorage.set('firstClick', 'true')
}
like image 22
foobars Avatar answered Sep 19 '22 15:09

foobars