2017-02-26 119 views
0

擴展在收到消息時發送通知。使Chrome擴展程序刷新已打開頁面而不是打開新頁面

var notification = new Notification('New message', { 
    icon: icon, 
    body: dabody, 
}); 

它還打開一個頁面,顯示單擊時的消息。

notification.onclick = function() { 
    window.open(link); 
    chrome.tabs.create({url: "https://website/messages/", selected: false, pinned: true}, function(tab) { 
     chrome.tabs.executeScript(tab.id, { 
      code: 'window.close();', 
      runAt: 'document_idle' 
     }); 
    }); 
    notification.close(); 
} 

有沒有一種方法可以讓我有/messages刷新頁面,如果它已經打開,而不是打開一個新的網頁嗎?

+3

見['tabs.update()'](https://developer.chrome.com/extensions/tabs#method-update)。 – Makyen

回答

0

當您的活動被解僱時,您可以嘗試通過chrome.tabs.query找到選項卡並重新加載,否則創建一個新的。

像這樣:

notification.onclick = function() { 
    chrome.tabs.query({url:"https://website/messages/"}, function(tabs) { 
     if(tabs.length) { 
      chrome.tabs.reload(tabs[0].tabId); 
     } 
     else { 
      // create new tab 
     } 
    }); 
}; 
相關問題