关闭由 Google Chrome 扩展程序创建的弹出窗口 [英] Closing popup window created by Google Chrome extension

查看:43
本文介绍了关闭由 Google Chrome 扩展程序创建的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码:

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 检查时,弹出窗口正常出现,第一个警报立即触发,然后弹出窗口自行关闭(应该如此).

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).

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

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?

文件在这里,如果你想要它们:[链接被删除,因为 drop.io 停业了]

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

推荐答案

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

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

当您创建常规窗口(而不是浏览器操作弹出窗口)时,您可以使用 chrome.tabs.remove() 从后台页面关闭它.您还可以从背景页面检测此窗口.类似的东西:

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);
        }
    }
});

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

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.

这篇关于关闭由 Google Chrome 扩展程序创建的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆