How does the chrome_url_overrides page communicate with the service_worker

24 views
Skip to first unread message

菠萝吹雪

unread,
May 30, 2024, 3:23:30 AMMay 30
to Chromium Extensions
I try this code bu't cannot use..
chrome.tabs.onCreated.addListener((tab) => {
// 在新标签页创建时,发送消息到内容脚本
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0]
if (activeTab) {
chrome.tabs.sendMessage(activeTab.id, {
action: TabEvent.updateTabList,
tab,
})
}
})
})

woxxom

unread,
May 30, 2024, 6:29:21 AMMay 30
to Chromium Extensions, 菠萝吹雪
Assuming chrome_url_overrides page in your case is newtab, it can use chrome.runtime.sendMessage or navigator.serviceWorker messages to communicate with the service worker.

As for the posted code, it uses the active tab, but that's not necessarily the created tab. You should probably use tab.id of the tab parameter in onCreated listener. Note that onCreated is generated immediately after the tab container was created but before it started loading its page, so your message won't be received. The simplest solution is to initiate messages from the page. 
Reply all
Reply to author
Forward
0 new messages