0

这将验证页面是否为 https 以及 localStorage 项目的评估结果是真还是假,并基于此显示通知。代码放在popup.js中:

chrome.tabs.query({
 active: true,               
 lastFocusedWindow: true    
    }, function(array_of_Tabs) {

    var tab = array_of_Tabs[0];
    var url = tab.url;

 if (url.indexOf('https:') === 0 && localStorage.getItem("secureRead").value !== true) {
           chrome.extension.getBackgroundPage().showNotify();
       }
    });

实际的通知代码放在 background.js 文件中:

var notification = webkitNotifications.createNotification(
  'icon48.png',
  'Secure page detected.',
  'Checking "Enable HTTPS reading" in the setting is required.'
);

function showNotify()
{
  notification.show();
}

问题是这只在全球范围内有效。然后不会检测、评估其他页面并且不会显示任何通知。我究竟做错了什么?

我也没有任何错误。

4

1 回答 1

1

首先,您应该知道当前的通知系统(即 webkitNotifications.createNotification)已被弃用,并且已从 Chrome 中删除,至少在 Windows 和 ChromeOS 上是这样。有关详细信息,请参阅http://developer.chrome.com/extensions/desktop_notifications.html

其次,如果通知被用户关闭,它可能为空;我会试试这个:

function showNotify()
{
    if (notification == null)
    {
        notification = webkitNotifications.createNotification(
                         'icon48.png',
                         'Secure page detected.',
                         'Checking "Enable HTTPS reading" in the setting is required.'
                       );
    }
    notification.show();
}
于 2013-08-20T09:58:43.263 回答