7

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.

4

2 回答 2

3

onClicked如果您的扩展程序的浏览器操作未default_popup在清单中定义,则调用该事件。文档中的该注释与弹出窗口当前是否打开无关。

如果清单定义default_popup,则再次单击该按钮将关闭并重新打开插件。mousedown 关闭, mouseup 打开。(因此,单击按钮并拖动并释放鼠标确实会关闭弹出窗口,并不是任何人都应该这样做。)

我建议default_popup在 html 中为关闭弹出窗口的弹出窗口设置并制作一个按钮window.close;,或者在弹出窗口的用例中找到一个有意义的关闭点。

于 2013-08-14T21:46:47.093 回答
2

好吧,已经很久了,问题/错误仍然存​​在于 Chrome 浏览器上。我找到了一种解决方法,它不是很好,但它可以满足我的需要 - 在第二次点击图标时关闭窗口。这是我在弹出的 javascript 文件中所做的:

if(localStorage.getItem('firstClick')==='true'){
    localStorage.setItem('firstClick', 'false');
    window.close();
}
else {
    localStorage.removeItem('firstClick');
    localStorage.set('firstClick', 'true')
}
于 2015-05-20T17:25:35.160 回答