Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何仅在单一情况下打开chrome扩展弹出窗口?_Javascript_Json_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 如何仅在单一情况下打开chrome扩展弹出窗口?

Javascript 如何仅在单一情况下打开chrome扩展弹出窗口?,javascript,json,google-chrome,google-chrome-extension,Javascript,Json,Google Chrome,Google Chrome Extension,我试图在我的工具栏上做一个chrome扩展,当点击时,它会将当前页面转换为该页面的google缓存版本。如果我已经在一个页面的谷歌缓存上,我想打开一个小弹出窗口说,“你已经在这个页面的谷歌缓存版本上了!” 以下是我得到的: manifest.json: { "name": "gCache", "version": "1.1.5", "description": "View the Google Cache of a page", "background_page": "redir

我试图在我的工具栏上做一个chrome扩展,当点击时,它会将当前页面转换为该页面的google缓存版本。如果我已经在一个页面的谷歌缓存上,我想打开一个小弹出窗口说,“你已经在这个页面的谷歌缓存版本上了!”

以下是我得到的:

manifest.json:

{
  "name": "gCache",
  "version": "1.1.5",
  "description": "View the Google Cache of a page",
  "background_page": "redirect.html",

  "browser_action": {
    "default_icon": "icon.png",
    "default_text": "Google Cache version of this page"
  },

  "permissions": [
    "tabs"
  ]
}
redirect.html:

<script>

chrome.browserAction.onClicked.addListener(function(tab) {
    if (tab.url.substring(0, 38) == "http://webcache.googleusercontent.com/")
        //Popup saying user is already on a webcache page
    else if(tab.url.substring(0, 5) == "http:")
        chrome.tabs.update(tab.id, { url: 'http://webcache.googleusercontent.com/search?q=cache:' + tab.url.substr(7) });
    else if(tab.url.substring(0,6) == "https:")
        chrome.tabs.update(tab.id, { url: 'http://webcache.googleusercontent.com/search?q=cache:' + tab.url.substr(8) });

});

</script>

chrome.browserAction.onClicked.addListener(函数(选项卡){
如果(tab.url.substring(0,38)=“http://webcache.googleusercontent.com/")
//弹出窗口显示用户已在网络缓存页面上
else if(tab.url.substring(0,5)=“http:”)
更新(tab.id,{url:'http://webcache.googleusercontent.com/search?q=cache:'+tab.url.substr(7)});
else if(tab.url.substring(0,6)=“https:)
更新(tab.id,{url:'http://webcache.googleusercontent.com/search?q=cache:'+tab.url.substr(8)});
});

感谢您的阅读和帮助

您不能在打开弹出窗口时将
browserAction
onClick
事件混合匹配。您可能想改用。

我不认为桌面通知API在这里有用。我不能启用“browser_action”:{“default_popup”:}有什么原因吗?您可以在五秒钟后自动隐藏通知或类似的内容。谷歌是这样设计的:
在点击浏览器动作图标时触发的。如果浏览器操作有弹出窗口,则不会触发此事件。
。您可以使用内容脚本显示JS
alert()
——我认为使用桌面通知对用户来说是更好的用户体验。@JasonHall我不会使用JS
alert()
,因为这很烦人。我想使用chrome扩展的弹出功能。然而,我想做的功能似乎已经不存在了,我只是不确定是否有一种好的方式可以偶尔显示弹出窗口。您可以尝试让弹出窗口始终显示,然后立即关闭,它可能运行得足够快,但我有点怀疑它。或者,您可以将扩展设置为页面操作,而不是浏览器操作,这样图标就不会出现在这些页面上。