-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
67 lines (61 loc) · 1.9 KB
/
service-worker.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
import { options } from "./util.js";
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true });
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
title: "Send to Multi Notes",
contexts: ["selection"],
id: "parent",
});
chrome.storage.local.get(options, (items) => {
for (let i = 1; i <= parseInt(items.noteNum); i++) {
chrome.contextMenus.create({
title: i.toString(),
contexts: ["selection"],
parentId: "parent",
id: "note" + i,
});
}
});
});
chrome.storage.onChanged.addListener((data) => {
if (typeof data.noteNum === "undefined") return;
let newValue = parseInt(data.noteNum.newValue);
let oldValue = parseInt(data.noteNum.oldValue ?? options.noteNum);
if (newValue > oldValue) {
for (let i = oldValue + 1; i <= newValue; i++) {
chrome.contextMenus.create({
title: i.toString(),
contexts: ["selection"],
parentId: "parent",
id: "note" + i,
});
}
} else if (newValue < oldValue) {
for (let i = newValue + 1; i <= oldValue; i++) {
chrome.contextMenus.remove("note" + i);
}
}
});
chrome.contextMenus.onClicked.addListener((data) => {
let menuItemId = data.menuItemId;
let pageUrl = data.pageUrl;
let selectionText = data.selectionText;
chrome.storage.local.get({ ...options, [menuItemId]: null }, (items) => {
let newLine = items.addEmptyLine ? "\n\n" : "\n";
let newText = items.appendUrl
? selectionText + " (" + pageUrl + ")"
: selectionText;
let oldText = items[menuItemId];
let newNote = oldText
? items.sendToTop
? newText + newLine + oldText
: oldText + newLine + newText
: newText;
chrome.storage.local.set({
[menuItemId]: newNote,
});
chrome.runtime.sendMessage({ refresh: [menuItemId] }, () => {
if (chrome.runtime.lastError) return;
});
});
});