Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
61 lines (48 loc) · 1.38 KB
/
Copy pathbackground.js
File metadata and controls
61 lines (48 loc) · 1.38 KB
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
import {
attemptInitialRulesUpdate,
createManualRulesUpdateHandler,
} from "./lib/rules-update.js";
import {
handlePeriodicRulesAlarm,
reconcileRulesSchedule,
} from "./lib/rules-schedule.js";
const MENU_ID = "qr-that";
let pendingTarget = null;
const handleManualRulesUpdate = createManualRulesUpdateHandler();
browser.runtime.onInstalled.addListener(async (details) => {
browser.menus.create({
id: MENU_ID,
title: "QR THAT!",
contexts: ["page", "link", "image"],
});
if (details.reason === "install") {
await attemptInitialRulesUpdate();
}
await reconcileRulesSchedule();
});
browser.runtime.onStartup.addListener(() => reconcileRulesSchedule());
browser.alarms.onAlarm.addListener((alarm) => handlePeriodicRulesAlarm(alarm));
browser.menus.onClicked.addListener(async (info) => {
if (info.menuItemId !== MENU_ID) {
return;
}
pendingTarget = info.linkUrl || info.srcUrl || info.pageUrl || null;
try {
await browser.action.openPopup();
} catch {
pendingTarget = null;
}
});
browser.runtime.onMessage.addListener((message) => {
const updateResult = handleManualRulesUpdate(message);
if (updateResult) {
return updateResult;
}
if (message !== "consume-context-target") {
return undefined;
}
const target = pendingTarget;
pendingTarget = null;
return Promise.resolve(target);
});
reconcileRulesSchedule().catch(() => {});