You can save the status into a synchronous DOM storage: window.onbeforeunload = () => localStorage.setItem('state', '0')
...and detect it in an already open offscreen document via window.onstorage = e => console.log(e.key);
...then delete the key from localStorage and add it to IndexedDB or you can just use localStorage via offscreen document instead of IndexedDB.
Alternatively, use a port:
// sidepanel.js
const port = chrome.runtime.connect({name: 'sidePanel'});
setInterval(() => port.postMessage('sidePanelPing'), 25e3); // keeping the background script alive while the side panel is open
// background.js
chrome.runtime.onConnect.addListener(port => {
if (
port.name === 'sidePanel') {
console.log(port.sender);
port.onDisconnect.addListener(() => {
console.log('disconnected');
// update IndexedDB here, optionally using port.sender if necessary
});
}
});