javascript - 为什么 chrome.notifications.onButtonClicked.addListener 函数运行多次?

标签 javascript google-chrome google-chrome-extension

我想同时显示多个通知。如果我在循环中多次运行NotificationBasic函数。我毫无问题地收到屏幕右下角的通知。但是,如果我单击通知按钮一次,它的作用就好像我单击了几次并在控制台上记录了一堆单击的按钮。另一方面,IDS ARE EQUAL 没有显示在任何地方。

如果我点击通知按钮,我想打开index.html页面。

function NotificationBasic(NotificationTitle, NotificationMessage, href){
    var options = {
        type: "basic",
        title: NotificationTitle,
        message: NotificationMessage,
        iconUrl: "icon.png",
        contextMessage: "Notification",
        buttons: [{
            title: "Go to the topic-->"
        }]
    };
    chrome.notifications.onButtonClicked.addListener(function(notifId){
        console.log("BUTTON CLİCKED");
        console.log("notifId: "+notifId);
        console.log("myNotificationID: "+myNotificationID);
        if(notifId == myNotificationID){
            console.log("IDS ARE EQUAL");
            window.open("/index.html"); 
        }      
    });
    chrome.notifications.create(options, function(id){
        myNotificationID = id;
    });
}

    for(let i = 0; i<new_ids.length; i++){
        let topic_id = ids.indexOf(new_ids[i]);
        new_topics[i] = messages[topic_id];
        if((messages[topic_id][1].includes("?day=")==false)){
        NotificationBasic(messages[topic_id][0], 'New topic', messages[topic_id][1]);
        }
    }

enter image description here

最佳答案

在您的函数中,chrome.notifications.onButtonClicked.addListener(...) 被执行多次,因此会记录很多行。

关于javascript - 为什么 chrome.notifications.onButtonClicked.addListener 函数运行多次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52928463/

相关文章:

javascript - 如何防止更改复选框的父单击事件

javascript - 上传-onComplete || onAllComplete 不起作用

google-chrome - Chrome 中 Jupyter 笔记本的 emacs 键绑定(bind)中的 Ctrl-N

css - 使 CSS 焦点轮廓在 Chrome 中更容易看到

javascript - 试图通过扩展程序阻止 Chrome 关闭

jquery - Chrome 扩展程序 将消息从后台发送到内容脚本

javascript - 正则表达式:如何返回不在括号中的匹配项

javascript - 如何跟踪图像和 iframe 的盗链?

android - 使用 Android 版 Chrome 移动版的路线加载 Google map

css - 样式文本区域和按钮