-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.ts
95 lines (86 loc) · 3.02 KB
/
background.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Chrome manifest v3 migrated from a background worker to service worker
import { logger } from "src/logger";
import { addOnClickedListener, setBadgeText } from "src/providers/action";
import {
addInstalledListener,
addMessageListener,
} from "src/providers/runtime";
import { loadStorageValues } from "src/providers/storage";
import { createTab } from "src/providers/tabs";
import { EMPTY_OR_INVALID_HUNT } from "src/types/errors";
import { SomeProgress } from "src/types/progress";
import { nonNull } from "src/utils/helpers";
const CHROME_INSTALL_REASON = "install";
const openClueCallback = (items: SomeProgress) => {
if (
items.huntConfig &&
items.huntConfig.clues &&
nonNull(items.currentProgress)
) {
if (items.currentProgress == 0) {
return;
}
const { clues } = items.huntConfig;
const foundClue = clues[items.currentProgress! - 1];
// Check for deprecated fields
// trunk-ignore(eslint/@typescript-eslint/no-unsafe-member-access)
if ((foundClue as any).html) {
logger.error(
"HTML functionality is deprecated. Please adjust your hunt.",
);
return;
}
if (items.userConfig && items.userConfig.displayMode === "Overlay") {
// Do nothing since it was registered on save.
} else {
// Open the clue page for the most recently found clue.
// This should actually be awaited, but the wiring here is nontrivial so leaving as is for now.
// trunk-ignore(eslint/@typescript-eslint/no-floating-promises)
createTab("popup.html");
}
} else {
logger.warn(EMPTY_OR_INVALID_HUNT);
// trunk-ignore(eslint/@typescript-eslint/no-floating-promises)
createTab("landing_page.html");
}
};
export const setupMessageListener = () =>
addMessageListener((request, _sender, _sendResponse) => {
if (request) {
// trunk-ignore-begin(eslint/@typescript-eslint/no-unsafe-member-access,eslint/@typescript-eslint/no-floating-promises)
if (request.status == "Found") {
setBadgeText("1");
return;
} else if (request.status == "Not Found") {
setBadgeText("");
return;
} else {
// Invalid
setBadgeText("");
return;
}
// trunk-ignore-end(eslint/@typescript-eslint/no-unsafe-member-access,eslint/@typescript-eslint/no-floating-promises)
}
logger.warn("Invalid request", request);
});
export const setupOnClickedListener = () =>
addOnClickedListener(() => {
// trunk-ignore(eslint/@typescript-eslint/no-floating-promises)
setBadgeText("");
loadStorageValues(
["huntConfig", "currentProgress", "userConfig"],
openClueCallback,
);
});
export const setupOnInstalledListener = () => {
addInstalledListener((details) => {
logger.debug(`Received install event ${details.reason}`);
if (details.reason === CHROME_INSTALL_REASON) {
// trunk-ignore(eslint/@typescript-eslint/no-floating-promises)
createTab("landing_page.html");
}
});
};
setupOnInstalledListener();
setupMessageListener();
setupOnClickedListener();