-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
67 lines (56 loc) · 2 KB
/
background.js
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
"use strict";
/// Run a script in [tabId], with the given options.
/// Options are the same as chrome.tabs.executeScript.
/// Returns a promise.
function executeScript(tabId, options) {
return new Promise((resolve, reject) => {
chrome.tabs.executeScript(tabId, options, resolve);
});
}
/// Send [message] to the current tab
async function sendMessage(message, tabId) {
tabId = tabId || await new Promise((resolve, reject) => {
chrome.tabs.query({ currentWindow: true, active: true },
(tabs) => resolve(tabs[0].id));
});
chrome.tabs.sendMessage(
tabId,
message);
}
chrome.browserAction.onClicked.addListener(async (tab) => {
await executeScript(tab.id, {
file: "/content/annotate.js",
allFrames: true,
});
await executeScript(tab.id, {
file: "/content/controls.js",
});
let toolColor = localStorage.getItem("toolColor") ?? "#f00";
let toolThickness = localStorage.getItem("toolThickness") ?? 0.5;
let touchDraw = localStorage.getItem("touchDrawEnabled") ?? "true";
sendMessage({ command: "setToolColor", value: toolColor }, tab.id);
sendMessage({ command: "setToolThickness", value: toolThickness }, tab.id);
sendMessage({ command: "setTouchDrawEnabled", value: touchDraw == "true" }, tab.id);
});
chrome.runtime.onMessage.addListener(async (message) => {
console.log("Got message! " + message);
switch (message.command) {
case "setToolColor":
localStorage.setItem("toolColor", message.value);
break;
case "setToolThickness":
localStorage.setItem("toolThickness", message.value);
break;
case "setTouchDrawEnabled":
localStorage.setItem("touchDrawEnabled", message.value);
break;
case "setDrawingMode":
break;
default:
console.warn(`Unknown message.`, message);
}
if (message.forward) {
message.forward = false;
sendMessage(message);
}
});