关闭由 Google Chrome 扩展程序创建的弹出窗口
我正在尝试创建一个 Chrome 扩展来替代 Delicious 书签。我知道已经有一个扩展可以做到这一点,但该扩展的问题是,在您为站点添加书签后,弹出窗口保持打开状态(与使用书签相反,在提交表单后弹出窗口会自行关闭。我重新创建了扩展并遇到了同样的问题。
这是我的代码:
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');
}
当我单击 Delicious 时按钮,弹出窗口显示正常,我可以保存书签,但单击“保存”后,弹出窗口没有关闭,但 self.close()
似乎没有关闭。当我删除 contentscript.js 中的 URL 检查时,弹出窗口会正常出现,第一个警报会立即触发,然后弹出窗口会自行关闭(正如它应该的那样)
。看起来 Chrome 正在阻止我执行 self.close()。Delicious 正在做其他事情吗?
如果您需要的话,这些文件就在这里:[链接已删除,因为 drop.io 停业了]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
window.close()
,但这可能也不起作用。当您创建常规窗口(而不是浏览器操作弹出窗口)时,您可以从后台页面使用 chrome.tabs.remove() 来关闭它。您还可以从背景页面检测此窗口。比如:
我不确定 Chrome 如何将创建的窗口视为选项卡或窗口。如果是 Windows 那么上面的代码会有点不同。
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: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.
我找到了一个非常简单的解决方法。您只需将选定的选项卡设置为 True,弹出窗口就会消失,就像这样......
I found a very easy work around to this. You just set the selected tab to True and the Popup disappears, like this...
getSelected 不适合我,所以我
在 background.js 中找到了这个解决方案,你只需要
getSelected not working for me, so i found this solution
in background.js you need just
我找到了这个解决方案:
chrome.tabs.update({ active: true });
这行代码关闭浏览器操作的弹出窗口。您甚至不需要在那里传递 tab.id,因为默认情况下它被设置为当前选项卡的 id。我在后台页面运行它,但似乎它可以在扩展中的任何地方运行。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.