Skip to content
This repository was archived by the owner on Nov 11, 2025. It is now read-only.

Commit cc27458

Browse files
authored
replace promise with sendResponse in background script (#21)
1 parent 3db87d1 commit cc27458

2 files changed

Lines changed: 24 additions & 30 deletions

File tree

scripts/background.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
class BackgroundWorker {
2-
3-
constructor() {
4-
}
5-
62
run() {
7-
trektor.browser.runtime.onMessage.addListener(async (msg) => {
3+
trektor.browser.runtime.onMessage.addListener((msg, _, sendResponse) => {
84
switch (msg.action) {
95
case "track":
10-
await this.track(...msg.args);
11-
return;
6+
this.track(...msg.args)
7+
.then(() => sendResponse())
8+
.catch((e) => sendResponse(e.message));
9+
break;
1210
case "addTask":
13-
await this.addTask(...msg.args);
14-
return;
11+
this.addTask(...msg.args)
12+
.then(() => sendResponse())
13+
.catch((e) => sendResponse(e.message));
14+
break;
1515
default:
16-
throw new Error(`unknown action: ${msg.action}`);
16+
new Error(`unknown action: ${msg.action}`);
1717
}
18+
return true;
1819
});
1920
}
2021

scripts/content_script.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,22 @@ async function addButton() {
3333
trackButton.addEventListener("click", async () => {
3434
trackButtonIcon.classList.add("trektor-state-loading");
3535

36-
try {
37-
await trektor.browser.runtime.sendMessage({
38-
action: "track",
39-
args: [window.location.pathname.split("/", 3)[2]],
40-
});
41-
trackButtonIcon.classList.replace("icon-clock", "icon-check-circle");
42-
window.setTimeout(() => trackButtonIcon.classList.replace("icon-check-circle", "icon-clock"), 2000);
43-
} catch (err) {
44-
window.alert(err);
45-
} finally {
46-
trackButtonIcon.classList.remove("trektor-state-loading");
47-
}
36+
const response = await trektor.browser.runtime.sendMessage({
37+
action: "track",
38+
args: [window.location.pathname.split("/", 3)[2]],
39+
});
40+
trackButtonIcon.classList.replace("icon-clock", "icon-check-circle");
41+
window.setTimeout(() => trackButtonIcon.classList.replace("icon-check-circle", "icon-clock"), 2000);
42+
if (response !== null) window.alert(response);
43+
trackButtonIcon.classList.remove("trektor-state-loading");
4844
});
4945

5046
addButton.addEventListener("click", async () => {
51-
try {
52-
await trektor.browser.runtime.sendMessage({
53-
action: "addTask",
54-
args: [window.location.pathname.split("/", 3)[2]],
55-
});
56-
} catch (err) {
57-
window.alert(err);
58-
}
47+
const response = await trektor.browser.runtime.sendMessage({
48+
action: "addTask",
49+
args: [window.location.pathname.split("/", 3)[2]],
50+
});
51+
if (response !== null) window.alert(response);
5952
});
6053
}
6154

0 commit comments

Comments
 (0)