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

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

问题描述

我正在尝试创建一个替代Delicious小书签的Chrome扩展程序。我知道这里有一个扩展名,但扩展名的问题在于,在为网站添加书签后,弹出窗口保持打开状态(与使用小书签相反,弹出窗口在提交表单后自动关闭),我重新创建了


$ b manifest.json:

/ strong>

  {
name:Delicious,
version:1.0 ,
description:为Delicious上的网站添加书签,
background_page:background.html,
权限:[
标签
$ bbrowser_action:{
default_icon:delicious.png
},
content_scripts:[
{
matches:[http://www.delicious.com/save*],
js:[contentscript.js]
}
]
}

background.html:

 < html>< script> 
chrome.bro wserAction.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('现在应该关闭');
}

当我点击Delicious按钮时,弹出窗口显示正常,我可以保存书签,但我点击保存后,弹出窗口不关闭。两个警报都显示出来,但 self.close()似乎没有任何作用。当我删除contentscript.js中的URL检查时,弹出窗口会正常显示,第一个警报会立即触发,然后弹出窗口自动关闭(应该如此)。



为什么这不起作用?它似乎并不像Chrome阻止我做self.close()。美味做什么?还有其他什么?



这些文件在这里,如果你想要的话:[链接被删除,因为drop.io歇业]

解决方案

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

在创建常规窗口(而不是浏览器动作弹出窗口)时,可以使用 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,那么上面的代码会有些不同。


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]

解决方案

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.

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

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