Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
392 lines (345 loc) · 13.2 KB
/
Copy pathbackground.js
File metadata and controls
392 lines (345 loc) · 13.2 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
// Webhook queue management
const webhookQueues = new Map(); // Map of webhookUrl -> { queue: [], lastSent: timestamp, timer: timeoutId }
const queueNotifications = new Map(); // Map of notificationId -> { webhookUrl, intervalId }
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "sendToWebhook",
title: "Send to Webhook",
contexts: ["page", "link", "image", "selection"]
}, () => {
if (chrome.runtime.lastError) {
console.error(`Error creating parent menu: ${chrome.runtime.lastError.message}`);
}
updateWebhookMenus();
});
initializeQueues();
});
chrome.runtime.onStartup.addListener(() => {
updateWebhookMenus();
initializeQueues();
});
function initializeQueues() {
chrome.storage.local.get('webhooks', function (data) {
if (data.webhooks) {
data.webhooks.forEach(webhook => {
if (!webhookQueues.has(webhook.url)) {
webhookQueues.set(webhook.url, {
queue: [],
lastSent: 0,
timer: null,
rateLimit: webhook.rateLimit || 0
});
} else {
// Update rate limit if it changed
const queueData = webhookQueues.get(webhook.url);
queueData.rateLimit = webhook.rateLimit || 0;
}
});
}
});
}
function addToQueue(webhookUrl, payload, webhookName, rateLimit = 0) {
if (!webhookQueues.has(webhookUrl)) {
webhookQueues.set(webhookUrl, {
queue: [],
lastSent: 0,
timer: null,
rateLimit: rateLimit
});
}
const queueData = webhookQueues.get(webhookUrl);
queueData.rateLimit = rateLimit; // Update rate limit
queueData.queue.push({ payload, webhookName, timestamp: Date.now() });
// Check if this item will be queued (not sent immediately)
const now = Date.now();
const timeSinceLastSent = now - queueData.lastSent;
const rateLimitMs = queueData.rateLimit * 1000;
const willBeQueued = queueData.rateLimit > 0 && (queueData.queue.length > 1 || timeSinceLastSent < rateLimitMs);
if (willBeQueued) {
showQueueNotification(webhookUrl, webhookName);
}
processQueue(webhookUrl);
}
function processQueue(webhookUrl) {
const queueData = webhookQueues.get(webhookUrl);
if (!queueData || queueData.queue.length === 0) return;
const now = Date.now();
const timeSinceLastSent = now - queueData.lastSent;
const rateLimitMs = queueData.rateLimit * 1000;
if (queueData.rateLimit > 0 && timeSinceLastSent < rateLimitMs) {
// Need to wait before sending
const waitTime = rateLimitMs - timeSinceLastSent;
if (queueData.timer) {
clearTimeout(queueData.timer);
}
queueData.timer = setTimeout(() => {
processQueue(webhookUrl);
}, waitTime);
return;
}
// Send the next item in queue
const item = queueData.queue.shift();
queueData.lastSent = now;
// Clear any existing queue notification for this webhook
clearQueueNotification(webhookUrl);
postToWebhookDirect(webhookUrl, item.payload, 3, item.webhookName);
// Schedule next item if queue has more items
if (queueData.queue.length > 0) {
if (queueData.rateLimit > 0) {
queueData.timer = setTimeout(() => {
processQueue(webhookUrl);
}, rateLimitMs);
} else {
// No rate limit, process immediately
processQueue(webhookUrl);
}
}
}
function sanitizeMenuId(name) {
return name.replace(/[^a-zA-Z0-9_-]/g, '_').substring(0, 50);
}
let updateWebhookMenusTimeout;
function updateWebhookMenus() {
// Debounce to avoid excessive rebuilds
clearTimeout(updateWebhookMenusTimeout);
updateWebhookMenusTimeout = setTimeout(() => {
// First remove all existing child items (if any)
chrome.contextMenus.removeAll(() => {
// Recreate parent menu to avoid reference errors
chrome.contextMenus.create({
id: "sendToWebhook",
title: "Send to Webhook",
contexts: ["page", "link", "image", "selection"]
}, () => {
// Check for errors
if (chrome.runtime.lastError) {
console.error(`Error recreating parent menu: ${chrome.runtime.lastError.message}`);
return;
}
// Now add child items
chrome.storage.local.get('webhooks', function (data) {
if (data.webhooks && data.webhooks.length > 0) {
data.webhooks.forEach((webhook, index) => {
const sanitizedId = sanitizeMenuId(webhook.name);
// Create menu item for page, link, and image contexts
chrome.contextMenus.create({
id: `sendTo_${sanitizedId}_${index}_normal`,
parentId: "sendToWebhook",
title: webhook.name,
contexts: ["page", "link", "image"]
});
// Create separate menu item for selection context
chrome.contextMenus.create({
id: `sendTo_${sanitizedId}_${index}_selection`,
parentId: "sendToWebhook",
title: webhook.name,
contexts: ["selection"]
});
});
}
});
});
});
}, 100);
}
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId.startsWith("sendTo_")) {
chrome.storage.local.get('webhooks', function (data) {
// Extract index from menu ID (sendTo_sanitizedName_index_type)
const parts = info.menuItemId.split('_');
const indexPart = parts[parts.length - 2]; // Second to last part is the index
const index = parseInt(indexPart);
const webhook = data.webhooks[index];
if (webhook) {
sendToWebhook(webhook.url, info, tab.id);
}
});
}
});
function sendToWebhook(webhookUrl, info, tabId) {
if (info.linkUrl) {
extractDataAndSend(webhookUrl, info.linkUrl, 'link', tabId, null);
} else if (info.srcUrl) {
extractDataAndSend(webhookUrl, info.srcUrl, 'image', tabId, null);
} else {
// Page context: check if text is selected
const type = info.selectionText ? 'selection' : 'page';
const urlToSend = info.pageUrl;
extractDataAndSend(webhookUrl, urlToSend, type, tabId, info.selectionText);
}
}
function extractDataAndSend(webhookUrl, urlToSend, type, tabId, selectionText) {
let codeToExecute;
if (type === 'page') {
codeToExecute = function () {
return {
title: document.title,
description: document.querySelector('meta[name="description"]')?.getAttribute('content') || null,
keywords: document.querySelector('meta[name="keywords"]')?.getAttribute('content') || null,
favicon: document.querySelector('link[rel="icon"]')?.href || document.querySelector('link[rel="shortcut icon"]')?.href || null
};
};
} else if (type === 'selection') {
codeToExecute = function () {
return {
title: document.title,
description: document.querySelector('meta[name="description"]')?.getAttribute('content') || null,
keywords: document.querySelector('meta[name="keywords"]')?.getAttribute('content') || null,
favicon: document.querySelector('link[rel="icon"]')?.href || document.querySelector('link[rel="shortcut icon"]')?.href || null
};
};
} else if (type === 'link') {
codeToExecute = function () {
const links = document.querySelectorAll('a');
for (let link of links) {
if (link.href === arguments[0]) {
return link.title || null;
}
}
return null;
};
} else if (type === 'image') {
codeToExecute = function () {
const images = document.querySelectorAll('img');
for (let img of images) {
if (img.src === arguments[0]) {
return img.alt || null;
}
}
return null;
};
}
chrome.scripting.executeScript({
target: { tabId: tabId },
func: codeToExecute,
args: [urlToSend]
}, (injectionResults) => {
if (chrome.runtime.lastError) {
console.error('Script injection failed:', chrome.runtime.lastError.message);
return;
}
const extractedData = injectionResults[0]?.result;
// Find webhook name for notification
chrome.storage.local.get('webhooks', function (data) {
const webhook = data.webhooks?.find(wh => wh.url === webhookUrl);
const webhookName = webhook ? webhook.name : 'Webhook';
// Build enhanced payload
const payload = {
url: urlToSend,
timestamp: new Date().toISOString(),
type: type
};
if (type === 'page') {
payload.title = extractedData?.title || null;
payload.description = extractedData?.description || null;
payload.keywords = extractedData?.keywords || null;
payload.favicon = extractedData?.favicon || null;
} else if (type === 'selection') {
payload.title = extractedData?.title || null;
payload.description = extractedData?.description || null;
payload.keywords = extractedData?.keywords || null;
payload.favicon = extractedData?.favicon || null;
payload.selectedText = selectionText;
} else if (type === 'link') {
payload.title = extractedData;
payload.linkTitle = extractedData;
} else if (type === 'image') {
payload.title = extractedData;
payload.altText = extractedData;
}
// Find webhook to get rate limit
chrome.storage.local.get('webhooks', function (webhooksData) {
const webhook = webhooksData.webhooks?.find(wh => wh.url === webhookUrl);
const rateLimit = webhook ? webhook.rateLimit || 0 : 0;
addToQueue(webhookUrl, payload, webhookName, rateLimit);
});
});
});
}
function showNotification(title, message, isSuccess = true) {
const iconPath = isSuccess ? 'images/icon48.png' : 'images/icon48.png';
chrome.notifications.create({
type: 'basic',
iconUrl: iconPath,
title: title,
message: message
});
}
function showQueueNotification(webhookUrl, webhookName) {
const notificationId = `queue_${webhookUrl}_${Date.now()}`;
// Clear any existing notification for this webhook
clearQueueNotification(webhookUrl);
// Get notification interval from settings
chrome.storage.local.get({ settings: { notificationInterval: 5 } }, function (data) {
const updateIntervalMs = data.settings.notificationInterval * 1000;
function updateNotification() {
const queueData = webhookQueues.get(webhookUrl);
if (!queueData || queueData.queue.length === 0) {
clearQueueNotification(webhookUrl);
return;
}
const now = Date.now();
const timeSinceLastSent = now - queueData.lastSent;
const rateLimitMs = queueData.rateLimit * 1000;
const waitTime = Math.max(0, rateLimitMs - timeSinceLastSent);
const queuePosition = queueData.queue.length;
const totalWait = Math.ceil((waitTime + (queuePosition - 1) * rateLimitMs) / 1000);
chrome.notifications.create(notificationId, {
type: 'basic',
iconUrl: 'images/icon48.png',
title: `⏳ ${webhookName} - Queued`,
message: `${queuePosition} in queue, ~${totalWait}s remaining`
});
}
// Initial notification
updateNotification();
// Update at configured interval
const intervalId = setInterval(updateNotification, updateIntervalMs);
queueNotifications.set(webhookUrl, { notificationId, intervalId });
// Auto-clear after 60 seconds to prevent indefinite notifications
setTimeout(() => clearQueueNotification(webhookUrl), 60000);
});
}
function clearQueueNotification(webhookUrl) {
const notification = queueNotifications.get(webhookUrl);
if (notification) {
clearInterval(notification.intervalId);
chrome.notifications.clear(notification.notificationId);
queueNotifications.delete(webhookUrl);
}
}
function postToWebhookDirect(webhookUrl, payload, retryCount = 3, webhookName = 'Webhook') {
fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
}).then(response => {
if (!response.ok && retryCount > 0) {
console.log(`Webhook failed with status ${response.status}, retrying... (${retryCount} attempts left)`);
setTimeout(() => postToWebhookDirect(webhookUrl, payload, retryCount - 1, webhookName), 1000);
} else if (response.ok) {
console.log('Webhook sent with response status:', response.status);
showNotification(`✅ ${webhookName} - Success`, `Data sent successfully to ${webhookName}`, true);
} else {
console.log('Webhook failed after all retries');
showNotification(`❌ ${webhookName} - Failed`, `Failed to send data after 3 attempts`, false);
}
}).catch(error => {
console.error('Error sending webhook:', error);
if (retryCount > 0) {
console.log(`Retrying webhook in 2 seconds... (${retryCount} attempts left)`);
setTimeout(() => postToWebhookDirect(webhookUrl, payload, retryCount - 1, webhookName), 2000);
} else {
showNotification(`❌ ${webhookName} - Error`, `Network error: ${error.message}`, false);
}
});
}
// Listen for changes in the webhooks data to update context menus and queues
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'local' && changes.webhooks) {
updateWebhookMenus();
initializeQueues();
}
});