收到消息时扩展程序会发送通知。
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
页面已经打开,我有办法刷新它而不是打开新页面吗?
最佳答案
当您的事件被触发时,您可以尝试通过 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
}
});
};
关于javascript - 让 Chrome 扩展程序刷新已打开的页面而不是打开新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42464270/