Javascript 关闭由 Google Chrome 扩展程序创建的弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3950981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:48:48  来源:igfitidea点击:

Closing popup window created by Google Chrome extension

javascriptgoogle-chromegoogle-chrome-extensiondelicious-api

提问by Alex Grin

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.

我正在尝试创建一个 Chrome 扩展程序来替代 Delicious 书签。我知道已经有一个扩展程序可以做到这一点,但该扩展程序的问题在于,在您为网站添加书签后,弹出窗口保持打开状态(而不是使用书签,在提交表单后弹出窗口会自行关闭。我重新创建了扩展并遇到了同样的问题。

Here's my code:

这是我的代码:

manifest.json:

清单.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:

<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:

内容脚本.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).

当我单击 Delicious 按钮时,弹出窗口很好,我可以保存书签,但是在单击“保存”后,弹出窗口没有关闭。两个警报都出现了,但self.close()似乎没有做任何事情。当我删除 contentscript.js 中的 URL 检查时,弹出窗口正常出现,第一个警报立即触发,然后弹出窗口自行关闭(应该如此)。

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?

为什么这不起作用?Chrome 似乎并没有阻止我执行 self.close()。美味在做什么?是别的吗?

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

如果您需要这些文件,请点击此处:[链接已删除,因为 drop.io 停业了]

采纳答案by serg

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

尝试window.close(),但这可能也行不通。

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.remove()后台页面关闭它。您还可以从背景页面检测此窗口。就像是:

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.

我不确定 Chrome 如何处理创建的窗口 - 作为选项卡或窗口。如果是windows,那么上面的代码会有点不同。

回答by Hlung

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

我找到了一个非常简单的解决方法。您只需将选定的选项卡设置为 True,弹出窗口就会消失,就像这样...

// remove popup by selecting the tab
chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.update(tab.id, { selected: true } )
});

回答by Konstantin Smolyanin

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.

我找到了这个解决方案:chrome.tabs.update({ active: true });这行代码关闭了浏览器操作的弹出窗口。你甚至不需要在那里传递 tab.id 因为默认情况下它被设置为当前选项卡的 id。我在后台页面中运行它,但似乎它可以在扩展中的任何地方运行。

回答by Andrey Andrey

getSelected not working for me, so i found this solution

getSelected 对我不起作用,所以我找到了这个解决方案

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

in background.js you need just

在 background.js 你只需要

window.close();