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

时间:2018-10-22 11:38:16

标签: 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

1 个答案:

答案 0 :(得分:1)

在您的函数中,chrome.notifications.onButtonClicked.addListener(...)被执行了很多次,所以记录了很多行。