diff --git a/scripts/background.js b/scripts/background.js index 843598c..c3af5bf 100644 --- a/scripts/background.js +++ b/scripts/background.js @@ -1,20 +1,21 @@ class BackgroundWorker { - - constructor() { - } - run() { - trektor.browser.runtime.onMessage.addListener(async (msg) => { + trektor.browser.runtime.onMessage.addListener((msg, _, sendResponse) => { switch (msg.action) { case "track": - await this.track(...msg.args); - return; + this.track(...msg.args) + .then(() => sendResponse()) + .catch((e) => sendResponse(e.message)); + break; case "addTask": - await this.addTask(...msg.args); - return; + this.addTask(...msg.args) + .then(() => sendResponse()) + .catch((e) => sendResponse(e.message)); + break; default: - throw new Error(`unknown action: ${msg.action}`); + new Error(`unknown action: ${msg.action}`); } + return true; }); } diff --git a/scripts/content_script.js b/scripts/content_script.js index cdd789b..7d2e8aa 100644 --- a/scripts/content_script.js +++ b/scripts/content_script.js @@ -33,29 +33,22 @@ async function addButton() { trackButton.addEventListener("click", async () => { trackButtonIcon.classList.add("trektor-state-loading"); - try { - await trektor.browser.runtime.sendMessage({ - action: "track", - args: [window.location.pathname.split("/", 3)[2]], - }); - trackButtonIcon.classList.replace("icon-clock", "icon-check-circle"); - window.setTimeout(() => trackButtonIcon.classList.replace("icon-check-circle", "icon-clock"), 2000); - } catch (err) { - window.alert(err); - } finally { - trackButtonIcon.classList.remove("trektor-state-loading"); - } + const response = await trektor.browser.runtime.sendMessage({ + action: "track", + args: [window.location.pathname.split("/", 3)[2]], + }); + trackButtonIcon.classList.replace("icon-clock", "icon-check-circle"); + window.setTimeout(() => trackButtonIcon.classList.replace("icon-check-circle", "icon-clock"), 2000); + if (response !== null) window.alert(response); + trackButtonIcon.classList.remove("trektor-state-loading"); }); addButton.addEventListener("click", async () => { - try { - await trektor.browser.runtime.sendMessage({ - action: "addTask", - args: [window.location.pathname.split("/", 3)[2]], - }); - } catch (err) { - window.alert(err); - } + const response = await trektor.browser.runtime.sendMessage({ + action: "addTask", + args: [window.location.pathname.split("/", 3)[2]], + }); + if (response !== null) window.alert(response); }); }