Keep connection from content script to background alive

This commit is contained in:
Nathan Bierema 2024-08-10 17:23:29 -04:00
parent 8a332528a9
commit 35b98d82f7
2 changed files with 6 additions and 1 deletions

View File

@ -601,7 +601,8 @@ function onConnect<S, A extends Action<string>>(port: chrome.runtime.Port) {
id = getId(port.sender!); id = getId(port.sender!);
if (port.sender!.frameId) id = `${id}-${port.sender!.frameId}`; if (port.sender!.frameId) id = `${id}-${port.sender!.frameId}`;
connections.tab[id] = port; connections.tab[id] = port;
listener = (msg: ContentScriptToBackgroundMessage<S, A>) => { listener = (msg: ContentScriptToBackgroundMessage<S, A> | 'heartbeat') => {
if (msg === 'heartbeat') return;
if (msg.name === 'INIT_INSTANCE') { if (msg.name === 'INIT_INSTANCE') {
if (typeof id === 'number') { if (typeof id === 'number') {
chrome.pageAction.show(id); chrome.pageAction.show(id);

View File

@ -318,3 +318,7 @@ function handleMessages<S, A extends Action<string>>(
} }
window.addEventListener('message', handleMessages, false); window.addEventListener('message', handleMessages, false);
setInterval(() => {
bg?.postMessage('heartbeat');
}, 15000);