-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
471 lines (415 loc) · 13.3 KB
/
background.js
File metadata and controls
471 lines (415 loc) · 13.3 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
const DEBUG = false; // Toggle this to enable/disable all logging
/**
* Centralized logging function
* @param {string} category - Category of the log (e.g., 'Storage', 'Menu', 'Network')
* @param {string} type - Type of log ('info', 'warn', 'error')
* @param {string} message - Main log message
* @param {any} data - Optional data to log
*/
function logInfo(category, type = 'info', message, data = null) {
if (!DEBUG) return;
const timestamp = new Date().toISOString();
const prefix = `[${timestamp}] [${category}]`;
switch (type.toLowerCase()) {
case 'error':
if (data) {
console.error(prefix, message, data);
} else {
console.error(prefix, message);
}
break;
case 'warn':
if (data) {
console.warn(prefix, message, data);
} else {
console.warn(prefix, message);
}
break;
default:
if (data) {
console.log(prefix, message, data);
} else {
console.log(prefix, message);
}
}
}
logInfo('Background script loaded');
function isChromeBrowser() {
var objAgent = navigator.userAgent
if (objAgent.indexOf("Chrome") != -1) {
return true;
} else {
return false;
}
}
const isChrome = isChromeBrowser();
const browserAPI = isChrome ? chrome : browser;
const menus = isChrome ? browserAPI.contextMenus : browserAPI.menus;
const runtime = browserAPI.runtime;
const storage = browserAPI.storage;
const action = browserAPI.action;
const i18n = browserAPI.i18n;
const contextList = isChrome ? ["link", "selection", "page", "image", "video", "audio",]
: ["link", "selection", "page", "tab", "image", "video", "audio",];
const URL_SEPARATOR = ";";
const urlMapKey = 'url_map';
//this is prefix string for menu item title
const menuTitlePrefix = getMessage("menuTitlePrefix");
//this is the storage for urls
let globalUrlMap = null; // Define at top level
/**
*
* Methods for accessing browser APIs
*
*/
//get data from the storage
function getDataFromStorage(key, callBack) {
logInfo('Storage', 'info', `Getting data for key: ${key}`);
if (isChrome) {
storage.sync.get(null,
function (storagePrefs) {
try {
var value = storagePrefs[key];
logInfo('Storage', 'info', 'Raw storage value:', value);
if (typeof (value) == 'undefined') {
value = '{}';
}
const map = jsonToMap(value);
logInfo('Storage', 'info', 'Parsed map:', map);
callBack(map);
} catch (error) {
logInfo('Storage', 'error', 'Error parsing storage data:', error);
callBack(new Map());
}
});
} else {
storage.sync.get()
.then((storagePrefs) => {
try {
var value = storagePrefs[key];
logInfo('Storage', 'info', 'Raw storage value:', value);
if (typeof (value) == 'undefined') {
value = '{}';
}
const map = jsonToMap(value);
logInfo('Storage', 'info', 'Parsed map:', map);
callBack(map);
} catch (error) {
logInfo('Storage', 'error', 'Error parsing storage data:', error);
callBack(new Map());
}
});
}
}
//set data to storage
function setDataInStorage(key, value, callBack) {
logInfo('Storage', 'info', 'mapKey/value: ' + key + "/" + value);
var storagePrefs = {};
storagePrefs[key] = mapToJson(value);
if (isChrome) {
storage.sync.set(storagePrefs, callBack);
} else {
storage.sync.set(storagePrefs)
.then(function (item) {
callBack();
});
}
}
// get message string from the message json
function getMessage(messageKey) {
return i18n.getMessage(messageKey);
}
//add a context menu item
function createMenuItem(itemProperties, callBack) {
logInfo('Menu', 'info', 'Creating menu item', itemProperties);
menus.create(itemProperties, callBack);
}
//add click listener for context menu items
function addMenuItemClickListener(callBack) {
menus.onClicked.addListener(callBack);
}
// add click listener for extension icon on tool bar
function addIconClickListener(callBack) {
action.onClicked.addListener(callBack);
}
/**
* This method will open the options page of the addon
*/
function openOptionsPage() {
runtime.openOptionsPage();
}
/**
* Utility methods with pure javascript code
*/
function strMapToObj(strMap) {
let obj = Object.create(null);
for (let [k, v] of strMap) {
obj[k] = v;
}
return obj;
}
function objToStrMap(obj) {
let strMap = new Map();
for (let k of Object.keys(obj)) {
strMap.set(k, obj[k]);
}
return strMap;
}
function mapToJson(strMap) {
return JSON.stringify(strMapToObj(strMap));
}
function jsonToMap(jsonStr) {
return objToStrMap(JSON.parse(jsonStr));
}
/**
* Call back methods for logging
*/
/**
* Called when the item has been created, or when creation failed due to an error.
*
* We'll just log success/failure here.
*
*/
function onCreated() {
if (runtime.lastError) {
logInfo('System', 'error', 'Error:', runtime.lastError);
} else {
logInfo('System', 'info', 'Item created successfully');
}
}
/**
* Called when the url has been set in storage.
*
* We'll just log success here.
*/
function onSuccess() {
logInfo('System', 'info', 'Url set successfully');
}
/**
* Called when there was an error.
*
* We'll just log the error here.
*/
function onError(error) {
logInfo('System', 'error', 'Error:', error);
}
/**
* Print the message provided in the browser console
* @param {String} message
*/
function print(message) {
logInfo('System', 'info', message);
}
/**
* Add this function to create menu items
*/
function addMenuItem(name, url) {
createMenuItem({
id: name,
title: menuTitlePrefix + " " + name,
contexts: contextList,
}, onCreated);
}
/**
* Add this function to create the options menu item
*/
function addOptionsMenuItem() {
createMenuItem({
id: "options",
title: getMessage("menuTitleOptions"),
contexts: ["all"]
}, onCreated);
}
/**
* Modify initContextMenu to initialize urlMap
*/
function initContextMenu() {
logInfo('System', 'info', 'Initializing context menu');
getDataFromStorage(urlMapKey, function (map) {
logInfo('Storage', 'info', 'Retrieved map from storage:', map);
// Ensure map is valid
if (!map || !(map instanceof Map)) {
logInfo('System', 'info', 'Creating new Map as retrieved map was invalid');
map = new Map();
}
// Set the global urlMap
globalUrlMap = map; // Use global variable instead of window
// Clear existing menus first
menus.removeAll(() => {
logInfo('Menu', 'info', 'Creating menu items for map:', Array.from(map.entries()));
if (map.size > 0) {
for (const [name, url] of map) {
addMenuItem(name, url);
}
}
addOptionsMenuItem();
});
});
}
// Remove the DOMContentLoaded event listener and replace with direct initialization
function initializeExtension() {
logInfo('System', 'info', 'Extension initialized');
// Clear existing menus first
menus.removeAll(() => {
// Then initialize context menus
initContextMenu();
});
}
// For Chrome's service worker
if (isChrome) {
// Service workers use the 'install' event
globalThis.addEventListener('install', (event) => {
logInfo('System', 'info', 'Service worker installed');
globalThis.skipWaiting(); // Use globalThis instead of self
});
globalThis.addEventListener('activate', (event) => {
logInfo('System', 'info', 'Service worker activated');
initializeExtension();
});
} else {
// Firefox can use direct initialization
initializeExtension();
}
//redirect user to options page on click of the extension icon.
addIconClickListener(openOptionsPage);
// Add to storage initialization
browserAPI.storage.sync.get(null, function(items) {
logInfo('Storage', 'info', 'Initial storage state:', items);
logInfo('Storage', 'info', 'urlMapKey value:', items[urlMapKey]);
});
// Add back the click handler and send functions with image support
addMenuItemClickListener((info, tab) => {
logInfo('System', 'info', 'Menu item clicked:', info.menuItemId);
logInfo('Storage', 'info', 'Current urlMap:', globalUrlMap);
logInfo('System', 'info', 'Click info:', info);
ensureUrlMap((urlMap) => {
var menuItemId = "" + info.menuItemId;
if (urlMap && urlMap.size > 0) {
var url = urlMap.get(menuItemId);
logInfo('Storage', 'info', 'Found URL for menuId:', url);
if (url != null && typeof url != 'undefined') {
logInfo('System', 'info', 'Sending to URL:', url);
logInfo('System', 'info', 'Info object:', info);
logInfo('System', 'info', 'Tab object:', tab);
send(url, info, tab);
} else {
logInfo('System', 'warn', 'No URL found for menuId:', menuItemId);
}
} else {
logInfo('System', 'warn', 'urlMap is empty or null:', urlMap);
}
if (menuItemId === "options") {
openOptionsPage();
}
});
});
/**
* This method sends given content(tab url, link or selected text)
* to given url.
*
* Note: The url must be of the discord webhook only.
* @param {String} url
* @param {TabInfo} info
* @param {Tab} tab
*/
function send(url, info, tab) {
logInfo('Network', 'info', 'Send function called', { url, info, tab });
let content = '';
let embeds = [];
if (info.mediaType === "image" || (info.srcUrl && info.srcUrl.match(/\.(jpeg|jpg|gif|png)$/i))) {
logInfo('Network', 'info', 'Processing image content');
embeds.push({
image: {
url: info.srcUrl
}
});
content = info.linkUrl || info.srcUrl;
} else {
logInfo('Network', 'info', 'Processing regular content');
content = info.linkUrl || info.selectionText || tab.url;
}
const payload = {
content: content,
embeds: embeds,
ttl: false
};
logInfo('Network', 'info', 'Formatted payload:', payload);
const jsonData = JSON.stringify(payload);
if (url.includes(URL_SEPARATOR)) {
logInfo('Network', 'info', 'Broadcasting to multiple channels');
const channels = url.split(URL_SEPARATOR);
logInfo('Network', 'info', 'Target channels:', channels);
for (const channel of channels) {
executeRequest(channel, jsonData);
}
} else {
logInfo('Network', 'info', 'Sending to single channel:', url);
executeRequest(url, jsonData);
}
}
/**
* This method sends given platform formatted json data
* to given url.
*
* @param {String} url
* @param {String} jsonData
*/
async function executeRequest(url, jsonData) {
logInfo('Network', 'info', 'Executing request', { url, jsonData });
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
body: jsonData
});
const data = await response.text();
logInfo('Network', 'info', 'Discord Response:', data);
return data;
} catch (error) {
logInfo('Network', 'error', 'Request failed', {
message: error.message,
stack: error.stack,
url: url,
data: jsonData
});
}
}
// Add a function to ensure urlMap is initialized
function ensureUrlMap(callback) {
if (!globalUrlMap) {
getDataFromStorage(urlMapKey, (map) => {
globalUrlMap = map;
callback(map);
});
} else {
callback(globalUrlMap);
}
}
// Add storage change listener near the top of the file with other listeners
browserAPI.storage.onChanged.addListener((changes, areaName) => {
logInfo('Storage', 'info', 'Storage changed:', { changes, areaName });
// Check if our urlMap has changed
if (changes[urlMapKey]) {
logInfo('Storage', 'info', 'urlMap changed, reinitializing menus');
// Get the new value
const newValue = changes[urlMapKey].newValue;
try {
// Parse and update global map
const newMap = jsonToMap(newValue);
globalUrlMap = newMap;
// Reinitialize context menus
menus.removeAll(() => {
if (newMap.size > 0) {
for (const [name, url] of newMap) {
addMenuItem(name, url);
}
}
addOptionsMenuItem();
});
} catch (error) {
logInfo('Storage', 'error', 'Error handling storage change:', error);
}
}
});