Firefox WebExtension 通知 API:单击通知时如何调用函数

Firefox WebExtension notifications API: How to call a function when the notification is clicked

我正在尝试使用 WebExtensions 开发一个 Firefox 插件。我想做的是在用户单击通知时打开一个新的 Firefox 选项卡或 window。但是没用。

当我点击通知时,没有任何反应。

我正在创建如下通知:

var q = chrome.notifications.create({
  "type": "basic",
  "iconUrl": chrome.extension.getURL("128-128q.png"),
  "title": 'title',
  "message": 'content'
 });
 
chrome.notifications.onClicked.addListener(function(notificationId) {
    window.open('http://www.google.com');
});
browser.notifications.onClicked.addListener(function(notificationId) {
    window.open('http://www.google.com');
});
 
q.onClicked.addListener(function(notificationId) {
    window.open('http://www.google.com');
});
 
var audio = new Audio('message.mp3');
audio.play();

我怎样才能使这个工作?

看来问题是您试图以一种不起作用的方式打开新的 URL。

以下应该有效,使用 chrome.tabs.create():

var q = chrome.notifications.create("MyExtensionNotificationId", {
        "type": "basic",
        "iconUrl": chrome.extension.getURL("128-128q.png"),
        "title": 'title',
        "message": 'content'
    });

chrome.notifications.onClicked.addListener(function(notificationId) {
    chrome.tabs.create({url:"http://www.google.com"});
});

但是,您需要在 Firefox 47.0+ 中测试它作为 chrome.notifications.onClicked() was only added recently. My statement of Firefox 47.0+ is based on the compatibility table. However, the compatibility table has at least one definite error. Thus, a higher version of Firefox may be required. The code I tested worked in Firefox Nightly, version 50.0a1, but did not work properly in Firefox Developer Edition, version 48.0a2. In general, given that the WebExtensions API is in active development, you should be testing against Firefox Nightly if you have questions/issues which are not functioning as you expect. You can also check the source code 对 API 的支持,以查看真正实现的内容以及添加的时间。

注意: 这适用于 chrome.notifications.onClickedbrowser.notifications.onClicked。但是,不要同时使用两者来添加两个单独的匿名函数,因为这样做会导致您正在做的事情发生两次。

我实际上并没有用你的代码测试它,但我确实用 notify-link-clicks-i18n WebExtensions example 的修改版本测试了它。我将该示例中的 background-script.js 文件修改为:

/*
Log that we received the message.
Then display a notification. The notification contains the URL,
which we read from the message.
*/
function notify(message) {
    console.log("notify-link-clicks-i18n: background script received message");
    var title = chrome.i18n.getMessage("notificationTitle");
    var content = chrome.i18n.getMessage("notificationContent", message.url);
    let id = "notify-link-clicks-i18n::" + title + "::" + message.url;
    chrome.notifications.create(id,{
        "type": "basic",
        "iconUrl": chrome.extension.getURL("icons/link-48.png"),
        "title": title,
        "message": content
    });
}

/*
Assign `notify()` as a listener to messages from the content script.
*/
chrome.runtime.onMessage.addListener(notify);

//Add the listener for clicks on a notification:
chrome.notifications.onClicked.addListener(function(notificationId) {
    console.log("Caught notification onClicked with ID:" + notificationId);
    //Open a new tab with the desired URL:
    browser.tabs.create({url:"http://www.google.com"});
});

我喜欢这样的东西,其中唯一的 ID is possible 是提供一个唯一的 ID,它既可以识别通知是由我的加载项显示的,也可以识别通知是什么。这允许侦听器功能选择仅对我的加载项显示的通知采取行动 and/or 仅我显示的通知的子集,或者根据我显示通知的原因采取不同的行动。

您在这里无法确定什么不起作用的原因可能是因为您没有将问题减少到证明该问题所需的最低限度。换句话说,尝试从通知 onClicked 监听器中单独打开一个新的 URL 并在监听器中尝试一个 console.log() 是个好主意。