gpt4 book ai didi

javascript - 让 Chrome 扩展程序刷新已打开的页面而不是打开新页面

转载 作者:行者123 更新时间:2023-11-28 04:57:04 25 4
gpt4 key购买 nike

收到消息时扩展程序会发送通知。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com