Google chrome extension 我能';无法在同一选项卡中打开chrome扩展的桌面推送通知

Google chrome extension 我能';无法在同一选项卡中打开chrome扩展的桌面推送通知,google-chrome-extension,notifications,push-notification,desktop,Google Chrome Extension,Notifications,Push Notification,Desktop,单击我在通知上创建的“查看产品”按钮后,链接将在一个新选项卡中打开,但对于第二个通知,链接将打开两个类似的选项卡并继续。 随着通知的增加,选项卡不断增加。 这是一个电子商务网站 这是我的密码。我不知道我哪里出错了 var messages = [] ; var ids = [] ; var latestItem; $(function(){ engine(); setInterval(engine, 60000); }); function engine(){ var

单击我在通知上创建的“查看产品”按钮后,链接将在一个新选项卡中打开,但对于第二个通知,链接将打开两个类似的选项卡并继续。 随着通知的增加,选项卡不断增加。 这是一个电子商务网站

这是我的密码。我不知道我哪里出错了

var messages = [] ;
var ids = [] ;

var latestItem;


$(function(){
engine();
    setInterval(engine, 60000);
}); 

function engine(){
    var newItems = [];
        $.get('https://www.mysite.or/electronics-video/', function(data){
            var htmlData = data;

            $data = ($(htmlData).find('.offer').eq(0));
            $data.find('.fleft').remove();

            $data.find('.rel.observelinkinfo.inlblk.zi3').remove();
            $data.find('.suggesttitleright.small.top.abs.zi2.br4.hidden').remove();
            $data.find('.thumb.vtop.inlblk.rel.tdnone.linkWithHash.scale4.detailsLink').remove();
            $data.find('.color-9.lheight16.margintop5').remove();
            $data.find('.breadcrumb.x-normal').remove();
            $data.find('.normal.inlblk.pdingtop5.lheight16.color.2').remove();


        $('body').append($data);

        for(i = 0; i<$data.find('h3.x-large.lheight20.margintop5').length; i++){

            ids[i]=($($data).find('td.wwnormal.tright.td-price').eq(i).find('p.price').text()).replace(/\n\r/g, '').trim();
            messages[i]= ($($data).find('h3.x-large.lheight20.margintop5').eq(i).find('a.marginright5.link.linkWithHash.detailsLink').text()).replace(/\n\r/g, '').trim();

        }


if (latestItem == ids[0]) {

    }else if(latestItem === undefined) {
        var firstRun = {
            type: "basic",
            title: "Site Notifier",
            message: 'Visit the website for updates on new products',
            iconUrl: "origi.png"
        }


        chrome.notifications.create(firstRun);
        latestItem = ids[0];

    }else if(latestItem != ids[0]) {
        for(j = 0; j<ids.length; j++){
            if(latestItem == ids[j]){
                break;
            }else{
                if (messages[j] != " "){
                newItems[j]= messages[j].concat(" - ").concat(ids[j]);
            }
        }

    }
        latestItem = ids[0];
    }
        if (newItems.length == 0){


        }else{
            for(i=0;i<newItems.length; i++){
        var myItem = {
            type: "basic",
            title: "New Product Alert!",
            message: newItems[i],
            contextMessage: "Site Notifier",
            buttons: [{
                title: "View Product"

            }],
            iconUrl: "origi.png"
        };

        chrome.notifications.onButtonClicked.addListener(function(){
            window.open('https://www.mysite.or/electronics-video/');


        });

        chrome.notifications.create(myItem);

            }
        }
    });
}
var消息=[];
var-id=[];
晚生变种;
$(函数(){
引擎();
设定间隔(发动机,60000);
}); 
函数引擎(){
var newItems=[];
$.get('https://www.mysite.or/electronics-video/,函数(数据){
var htmlData=数据;
$data=($(htmlData.find('.offer').eq(0));
$data.find('.fleft').remove();
$data.find('.rel.observelinkinfo.inlblk.zi3').remove();
$data.find('.suggestTitlerRight.small.top.abs.zi2.br4.hidden').remove();
$data.find('.thumb.vtop.inblk.rel.tdnone.linkWithHash.scale4.detailsLink').remove();
$data.find('.color-9.lheight16.margintop5').remove();
$data.find('.breadcrumb.x-normal').remove();
$data.find('.normal.inblk.pdingtop5.lheight16.color.2').remove();
$('body')。追加($data);
对于(i=0;i请尝试以下代码

 chrome.notifications.onButtonClicked.addListener(function (notificationId, buttonIndex) {
    if (notificationId == '1234567' && buttonIndex == 0) {
        var $ismysite = false;
        chrome.windows.getAll({ populate: true }, function (windows) {
            windows.forEach(function (window) {
                window.tabs.forEach(function (tab) {
                    if (tab.url.toString().lastIndexOf('mysite') > -1) {
                        $ismysite= true
                    }
                });
            });
            if (!$ismysite) {
                $ismysite = false;
                chrome.tabs.create({ url: "YourSire" }, function () {
                });
            }
        });
    }
});

在代码的末尾,如果(newItems.length==0)

else
语句将为newItems数组的每个元素创建通知,并在单击
侦听器时添加

你需要退出

chrome.notifications.onButtonClicked.addListener(function(){
  window.open('https://www.mysite.or/electronics-video/');
});
在这个循环之外的某个地方。

如果您需要知道单击了哪个通知,您可以通过回调中的ID找到。为此,您可能需要在全局对象中维护一些ID-url连接。

这确实是个问题,不知道您通过拉来告诉我什么out@CollinKiprono每次通过newItems数组循环时,您都在添加onButtonClicked侦听器,它们是乘法。你只需要在循环之外的某个地方添加一次侦听器。我真的明白你的意思,但尝试将侦听器从循环之外添加,但是选项卡不断增加。肯定有什么地方我做错了。我就是想不出来