// One writer keeps edits from separate popup/options pages from overwriting one another.
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message?.type !== 'tubefocus:settings' || sender?.id !== chrome.runtime.id) return;
// Only extension pages may use this message API for settings writes.
if (![chrome.runtime.getURL('popup.html'), chrome.runtime.getURL('settings.html')].includes(sender.url)) {
sendResponse({ ok: false, error: 'Open TubeFocus to change settings.' });
return false;
}
let command;
try { command = parseCommand(message); } catch (error) {
sendResponse({ ok: false, error: error.message });
return false;
}
if (pendingWrites >= MAX_PENDING_WRITES) {
sendResponse({ ok: false, error: 'Too many changes. Please wait a moment and try again.' });
return false;
}
pendingWrites++;
const operation = writeQueue.then(async () => {
const current = await readSettings();
// … the command, applied to the settings it just read, and saved …
});
writeQueue = operation.catch(() => {});