From 336cac52d056504911bf181030082c86d612b40a Mon Sep 17 00:00:00 2001 From: Martin Kalcher Date: Wed, 24 Jan 2024 12:56:47 +0100 Subject: [PATCH 1/2] replace promise with sendResponse in background script --- scripts/background.js | 21 +++++++++++---------- scripts/content_script.js | 33 +++++++++++++-------------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/scripts/background.js b/scripts/background.js index 843598c..97d4ac2 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(true)) + .catch((e) => sendResponse(e.message)); + break; case "addTask": - await this.addTask(...msg.args); - return; + this.addTask(...msg.args) + .then(() => sendResponse(true)) + .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..178d620 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 !== true) 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 !== true) window.alert(response); }); } From 9992529bd0bd27c16069577445673607152f2a1d Mon Sep 17 00:00:00 2001 From: Martin Kalcher Date: Wed, 24 Jan 2024 13:02:10 +0100 Subject: [PATCH 2/2] cleanup --- scripts/background.js | 4 ++-- scripts/content_script.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/background.js b/scripts/background.js index 97d4ac2..c3af5bf 100644 --- a/scripts/background.js +++ b/scripts/background.js @@ -4,12 +4,12 @@ class BackgroundWorker { switch (msg.action) { case "track": this.track(...msg.args) - .then(() => sendResponse(true)) + .then(() => sendResponse()) .catch((e) => sendResponse(e.message)); break; case "addTask": this.addTask(...msg.args) - .then(() => sendResponse(true)) + .then(() => sendResponse()) .catch((e) => sendResponse(e.message)); break; default: diff --git a/scripts/content_script.js b/scripts/content_script.js index 178d620..7d2e8aa 100644 --- a/scripts/content_script.js +++ b/scripts/content_script.js @@ -39,7 +39,7 @@ async function addButton() { }); trackButtonIcon.classList.replace("icon-clock", "icon-check-circle"); window.setTimeout(() => trackButtonIcon.classList.replace("icon-check-circle", "icon-clock"), 2000); - if (response !== true) window.alert(response); + if (response !== null) window.alert(response); trackButtonIcon.classList.remove("trektor-state-loading"); }); @@ -48,7 +48,7 @@ async function addButton() { action: "addTask", args: [window.location.pathname.split("/", 3)[2]], }); - if (response !== true) window.alert(response); + if (response !== null) window.alert(response); }); }