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

发布于 2024-09-28 10:33:21 字数 1610 浏览 4 评论 0原文

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

这是我的代码:

ma​​nifest.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰葑 2024-10-05 10:33:22

getSelected 不适合我,所以我

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

在 background.js 中找到了这个解决方案,你只需要

window.close();

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();
听闻余生 2024-10-05 10:33:22

我找到了这个解决方案: 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.

浮世清欢 2024-10-05 10:33:21

尝试window.close(),但这可能也不起作用。

当您创建常规窗口(而不是浏览器操作弹出窗口)时,您可以从后台页面使用 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);
        }
    }
});

我不确定 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:

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.

一曲琵琶半遮面シ 2024-10-05 10:33:21

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

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

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 } )
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文