2012-03-06 77 views
0

我试图在我的工具栏上添加一个Chrome扩展,当点击它时,会将当前页面转换为该页面的Google缓存版本。如果我已经在一个页面的Google缓存中,我想打开一个小小的弹出窗口,说:“你已经在这个页面的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> 

谢谢您的阅读和帮助!

+0

您可以使用内容脚本来显示JS'alert()' - 我认为使用桌面通知是用户更好的用户体验。 – 2012-03-07 03:55:33

+0

@JasonHall我不会使用JS alert(),因为它很烦人。我想使用Chrome扩展的弹出功能。但是,它开始看起来像我想要做的功能不存在 – Dbz 2012-03-07 03:58:46

+0

它确实存在,我只是不确定有时候只显示弹出窗口是一种好方法。你可以尝试让弹出窗口总是显示,然后立即关闭,它可以做得足够快,它可以工作,但我有点怀疑它。或者,您可以将您的扩展程序设为页面操作而不是浏览器操作,因此图标将不会显示在这些页面上。 – 2012-03-07 04:07:24

回答

0

您不能混合和匹配browserActiononClick事件与打开弹出。您可能需要使用Desktop Notifications API

+0

我不认为桌面通知API在这里很有用。是否有一个原因,我不能有“browser_action”:{“default_popup”:}也启用? – Dbz 2012-03-07 01:24:42

+0

你可以在五秒钟后自动隐藏通知或类似的东西。 Google这样设计的:'点击浏览器动作图标时触发。如果浏览器操作弹出窗口,此事件不会触发。 https://code.google.com/chrome/extensions/browserAction.html#event-onClicked – abraham 2012-03-07 05:54:47

相关问题