diff --git a/background_scripts/index.js b/background_scripts/index.js index 3a29bbf..f02e30b 100644 --- a/background_scripts/index.js +++ b/background_scripts/index.js @@ -10,7 +10,7 @@ WORKING: "w", }; - async function updateIconStatus() { + async function updateIconStatus(stopped) { return await browser.browserAction.setIcon({ path: { 16: `../icons/vaccine-${stopped ? "black" : "color"}.svg`, @@ -57,7 +57,7 @@ } async function executeNextJob() { - const { stopped } = await browser.storage.sync.get({ + const { stopped } = await browser.storage.local.get({ stopped: false, }); @@ -93,9 +93,7 @@ } browser.storage.onChanged.addListener(async (change, areaName) => { - if (areaName !== "sync") return; - - if (change.locations && change.locations.newValue) { + if (areaName === "sync" && change.locations && change.locations.newValue) { Object.keys(locations).forEach((url) => { if (!change.locations.newValue[url]) { delete locations[url]; @@ -111,7 +109,14 @@ }); } - if (change.stopped) return await updateIconStatus(stopped); + if (change.stopped) { + await updateIconStatus(change.stopped.newValue); + if (areaName === "sync") { + // Ça peut arriver de cette instance ou d'une autre instance de Firefox + // -> mettons aussi à jour la valeur locale pour arrêter les checks locaux. + await browser.storage.local.set({ stopped: true }); + } + } }); browser.runtime.onMessage.addListener(async (data) => { @@ -144,6 +149,7 @@ break; case "booked": + // Note: on met à jour la valeur locale dans onChanged au-dessus. await browser.storage.sync.set({ stopped: true }); await browser.tabs.create({ @@ -170,11 +176,17 @@ } }); - const { locations, stopped } = await browser.storage.sync.get({ + // Le booléan "stopped" est à la fois stocké localement et synchronisé. En + // effet, lorsqu'on arrive à booker un rdv dans un Firefox on veut arrêter les + // checks dans toutes les instances. Mais un simple clic sur le bouton ne doit + // déclencher d'arrêt que localement. + const { locations, stopped: stoppedFromSync } = await browser.storage.sync.get({ locations: {}, stopped: false, }); + const { stopped } = await browser.storage.local.get({ stopped: stoppedFromSync }); + await updateIconStatus(stopped); // On ajoute les iframes pour charger les centres à surveiller en arrière plan diff --git a/browser_action/index.js b/browser_action/index.js index f22ab59..f7f381f 100644 --- a/browser_action/index.js +++ b/browser_action/index.js @@ -62,12 +62,13 @@ document.getElementById(stopped ? "stop" : "start").style = "display: none"; } - let { locations, stopped, autoBook } = await browser.storage.sync.get({ + let { locations, autoBook } = await browser.storage.sync.get({ locations: {}, autoBook: false, - stopped: false, }); + let { stopped } = await browser.storage.local.get({ stopped: false }); + let { localLocations } = await browser.storage.local.get({ locations: {} }); browser.storage.onChanged.addListener(async (change, areaName) => { @@ -79,6 +80,8 @@ localLocations = change.locations.newValue; displayLocations(locations, localLocations); } + + if (change.stopped) displayStopStart(change.stopped.newValue || false); } if (areaName === "sync") { @@ -87,8 +90,6 @@ displayLocations(locations, localLocations); } - if (change.stopped) displayStopStart(change.stopped.newValue || false); - if (change.autoBook) document.getElementById( change.autoBook.newValue || false @@ -99,13 +100,11 @@ }); document.getElementById("stop").onclick = async () => { - await browser.storage.sync.set({ stopped: true }); - displayStopStart(true); + await browser.storage.local.set({ stopped: true }); }; document.getElementById("start").onclick = async () => { - await browser.storage.sync.set({ stopped: false }); - displayStopStart(false); + await browser.storage.local.set({ stopped: false }); }; document.getElementById("reset").onclick = async () => { @@ -117,6 +116,7 @@ return; await browser.storage.sync.clear(); + await browser.storage.local.clear(); }; document.getElementById("disableAutoBook").onclick = async () =>