-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
241 lines (206 loc) · 8.04 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* eslint-disable no-unused-vars */
// NOTE: This file must be in the top-level directory of the extension according to the docs
import { executeInCurrentTab, wrapResponse } from './src/background/utils.js';
const DEFAULT_COLOR_TITLE = "yellow";
// Add option when right-clicking
browser.runtime.onInstalled.addListener(async () => {
// remove existing menu items
browser.contextMenus.removeAll();
browser.contextMenus.create({ title: 'Highlight', id: 'highlight', contexts: ['selection'] });
browser.contextMenus.create({ title: 'Toggle Cursor', id: 'toggle-cursor' });
browser.contextMenus.create({ title: 'Highlighter color', id: 'highlight-colors' });
browser.contextMenus.create({ title: 'Yellow', id: 'yellow', parentId: 'highlight-colors', type: 'radio' });
browser.contextMenus.create({ title: 'Blue', id: 'blue', parentId: 'highlight-colors', type: 'radio' });
browser.contextMenus.create({ title: 'Green', id: 'green', parentId: 'highlight-colors', type: 'radio' });
browser.contextMenus.create({ title: 'Pink', id: 'pink', parentId: 'highlight-colors', type: 'radio' });
browser.contextMenus.create({ title: "Dark", id: "dark", parentId: "highlight-colors", type: "radio" });
// Get the initial selected color value
const { title: colorTitle } = await getCurrentColor();
browser.contextMenus.update(colorTitle, { checked: true });
});
browser.contextMenus.onClicked.addListener(({ menuItemId, parentMenuItemId }) => {
if (parentMenuItemId === 'highlight-color') {
changeColorFromContext(menuItemId);
return;
}
switch (menuItemId) {
case 'highlight':
highlightTextFromContext();
break;
case 'toggle-cursor':
toggleHighlighterCursorFromContext();
break;
}
});
// Analytics (non-interactive events)
browser.runtime.onInstalled.addListener(() => {
});
browser.runtime.onStartup.addListener(() => {
});
// If the URL changes, try again to highlight
// This is done to support javascript Single-page applications
// which often change the URL without reloading the page
browser.tabs.onUpdated.addListener((tabId, changeInfo, _tab) => {
if (changeInfo.url) {
browser.scripting.executeScript({
target: { tabId, allFrames: true },
files: ['src/contentScripts/loadHighlights.js'],
});
}
});
// Add Keyboard shortcuts
browser.commands.onCommand.addListener((command) => {
switch (command) {
case 'execute-highlight':
highlightText();
toggleHighlighterCursor();
break;
}
});
// Listen to messages from content scripts
/* eslint-disable consistent-return */
browser.runtime.onMessage.addListener((request, _sender, sendResponse) => {
if (!request.action) return;
switch (request.action) {
case 'highlight':
highlightText();
return;
case 'track-event':
return;
case 'remove-highlights':
removeHighlights();
return;
case 'remove-highlight':
removeHighlight(request.highlightId);
return;
case 'change-color':
changeColor(request.color);
return;
case 'edit-color':
editColor(request.colorTitle, request.color, request.textColor);
return;
case 'toggle-highlighter-cursor':
toggleHighlighterCursor();
return;
case 'get-highlights':
wrapResponse(getHighlights(), sendResponse);
return true; // return asynchronously
case 'get-lost-highlights':
wrapResponse(getLostHighlights(), sendResponse);
return true; // return asynchronously
case 'show-highlight':
return showHighlight(request.highlightId);
case 'get-current-color':
wrapResponse(getCurrentColor(), sendResponse);
return true; // return asynchronously
case 'get-color-options':
wrapResponse(getColorOptions(), sendResponse);
return true; // return asynchronously
}
});
/* eslint-enable consistent-return */
async function getCurrentColor() {
const { color } = await browser.storage.sync.get("color");
const colorTitle = color || DEFAULT_COLOR_TITLE;
const colorOptions = await getColorOptions();
return colorOptions.find((colorOption) => colorOption.title === colorTitle) || colorOptions[0];
}
function highlightTextFromContext() {
highlightText();
}
function toggleHighlighterCursorFromContext() {
toggleHighlighterCursor();
}
function changeColorFromContext(menuItemId) {
changeColor(menuItemId);
}
function highlightText() {
executeInCurrentTab({ file: 'src/contentScripts/highlight.js' });
}
function toggleHighlighterCursor() {
executeInCurrentTab({ file: 'src/contentScripts/toggleHighlighterCursor.js' });
}
function removeHighlights() {
executeInCurrentTab({ file: 'src/contentScripts/removeHighlights.js' });
}
function removeHighlight(highlightId) {
function contentScriptRemoveHighlight(highlightIndex) {
const highlightError = window.highlighter_lostHighlights.get(highlightIndex);
clearTimeout(highlightError?.timeout);
window.highlighter_lostHighlights.delete(highlightIndex);
removeHighlight(highlightIndex, window.location.hostname + window.location.pathname, window.location.pathname);
}
executeInCurrentTab({ func: contentScriptRemoveHighlight, args: [highlightId] });
}
function showHighlight(highlightId) {
console.log("hihi ruka chan")
function contentScriptShowHighlight(highlightId) { // eslint-disable-line no-shadow
const highlightEl = document.querySelector(`[data-highlight-id="${highlightId}"]`);
if (highlightEl) {
highlightEl.scrollIntoViewIfNeeded(true);
const boundingRect = highlightEl.getBoundingClientRect();
onHighlightMouseEnterOrClick({
'type': 'click',
'target': highlightEl,
'clientX': boundingRect.left + (boundingRect.width / 2),
});
}
}
executeInCurrentTab({ func: contentScriptShowHighlight, args: [highlightId] });
}
function getHighlights() {
return executeInCurrentTab({ file: 'src/contentScripts/getHighlights.js' });
}
function getLostHighlights() {
function contentScriptGetLostHighlights() {
const lostHighlights = [];
window.highlighter_lostHighlights.forEach((highlight, index) => lostHighlights.push({ string: highlight?.string, index }));
return lostHighlights;
}
return executeInCurrentTab({ func: contentScriptGetLostHighlights });
}
function changeColor(colorTitle) {
if (!colorTitle) return;
browser.storage.sync.set({ color: colorTitle });
// Also update the context menu
browser.contextMenus.update(colorTitle, { checked: true });
}
async function editColor(colorTitle, color, textColor) {
const colorOptions = await getColorOptions();
const colorOption = colorOptions.find((option) => option.title === colorTitle);
colorOption.color = color;
colorOption.textColor = textColor;
if (!textColor) {
delete colorOption.textColor;
}
browser.storage.sync.set({ colors: colorOptions });
}
function getColorOptions() {
return new Promise((resolve, _reject) => {
browser.storage.sync.get({
colors: [ // Default value
{
title: 'yellow',
color: 'rgb(255, 255, 102)',
},
{
title: 'green',
color: 'rgb(153, 255, 102)',
},
{
title: 'blue',
color: 'rgb(51, 204, 204)',
},
{
title: 'pink',
color: 'rgb(255, 153, 204)',
},
{
title: 'dark',
color: 'rgb(52, 73, 94)',
textColor: 'rgb(255, 255, 255)',
},
],
}, ({ colors }) => resolve(colors));
});
}