-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.js
More file actions
346 lines (306 loc) · 12.2 KB
/
utils.js
File metadata and controls
346 lines (306 loc) · 12.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
// Shared utility functions
// Ensure browserAPI polyfill is loaded when running in a browser environment
if (typeof window !== 'undefined' && !window.browserAPI) {
const script = document.createElement('script');
script.src = '/utils/browser-polyfill.js';
document.head.appendChild(script);
}
// Import or load the browser abstraction layer
const getBrowserAPI = () => {
if (typeof window !== 'undefined') {
if (window.browserAPI) return window.browserAPI;
if (window.browser) return window.browser;
if (window.chrome) return window.chrome;
}
if (typeof global !== 'undefined' && global.browser) {
return global.browser;
}
try {
return require('./browser-polyfill.js');
} catch (e) {
return typeof browser !== 'undefined' ? browser : typeof chrome !== 'undefined' ? chrome : {};
}
};
/**
* Replaces i18n placeholders in the HTML document.
* It targets elements with the 'data-i18n' attribute,
* text nodes containing '__MSG_...__', and the document title.
*/
function replaceI18nPlaceholders() {
const browserAPI = getBrowserAPI();
// Helper to replace __MSG_key__ tokens within a string
const replaceTokens = (value) => {
if (!value || typeof value !== 'string' || !value.includes('__MSG_')) return value;
return value.replace(/__MSG_([^_]+)__/g, (match, key) => {
const msg = browserAPI.i18n?.getMessage ? browserAPI.i18n.getMessage(key) : '';
return msg || match;
});
};
// Replace textContent of elements with data-i18n attribute
const elements = document.querySelectorAll('[data-i18n]');
elements.forEach(element => {
const key = element.getAttribute('data-i18n');
if (key) {
const message = browserAPI.i18n.getMessage(key);
if (message) {
element.textContent = message;
}
}
});
// Replace __MSG_...__ patterns in all text nodes
document.querySelectorAll('body *').forEach(element => {
if (element.childNodes && element.childNodes.length > 0) {
element.childNodes.forEach(node => {
if (node.nodeType === Node.TEXT_NODE && node.nodeValue && node.nodeValue.includes('__MSG_')) {
node.nodeValue = replaceTokens(node.nodeValue);
}
});
}
});
// Replace __MSG_...__ patterns in common attributes (e.g., placeholder, title, aria-label)
document.querySelectorAll('*').forEach(el => {
if (!el.attributes) return;
Array.from(el.attributes).forEach(attr => {
if (typeof attr.value === 'string' && attr.value.includes('__MSG_')) {
const newVal = replaceTokens(attr.value);
if (newVal !== attr.value) {
el.setAttribute(attr.name, newVal);
}
}
});
});
// Replace __MSG_...__ patterns in the document title
if (document.title && document.title.includes('__MSG_')) {
document.title = replaceTokens(document.title);
}
}
const padDatePart = (value, length = 2) => String(value).padStart(length, '0');
const toLocalIsoString = (date) => {
const year = date.getFullYear();
const month = padDatePart(date.getMonth() + 1);
const day = padDatePart(date.getDate());
const hour = padDatePart(date.getHours());
const minute = padDatePart(date.getMinutes());
const second = padDatePart(date.getSeconds());
const millisecond = padDatePart(date.getMilliseconds(), 3);
const offsetMinutes = -date.getTimezoneOffset();
const sign = offsetMinutes >= 0 ? '+' : '-';
const absoluteOffset = Math.abs(offsetMinutes);
const offsetHour = padDatePart(Math.floor(absoluteOffset / 60));
const offsetMinute = padDatePart(absoluteOffset % 60);
return `${year}-${month}-${day}T${hour}:${minute}:${second}.${millisecond}${sign}${offsetHour}:${offsetMinute}`;
};
const buildDateTimeVariables = (date = new Date()) => {
const nowIso = date.toISOString();
const timestampMs = date.getTime();
return {
nowIso,
values: {
"{{now.iso}}": nowIso,
"{{now.date}}": nowIso.slice(0, 10),
"{{now.time}}": nowIso.slice(11, 19),
"{{now.unix}}": Math.floor(timestampMs / 1000),
"{{now.unix_ms}}": timestampMs,
"{{now.year}}": date.getUTCFullYear(),
"{{now.month}}": date.getUTCMonth() + 1,
"{{now.day}}": date.getUTCDate(),
"{{now.hour}}": date.getUTCHours(),
"{{now.minute}}": date.getUTCMinutes(),
"{{now.second}}": date.getUTCSeconds(),
"{{now.millisecond}}": date.getUTCMilliseconds(),
"{{now.local.iso}}": toLocalIsoString(date),
"{{now.local.date}}": `${date.getFullYear()}-${padDatePart(date.getMonth() + 1)}-${padDatePart(date.getDate())}`,
"{{now.local.time}}": `${padDatePart(date.getHours())}:${padDatePart(date.getMinutes())}:${padDatePart(date.getSeconds())}`,
"{{triggeredAt}}": nowIso,
},
};
};
async function sendWebhook(webhook, isTest = false) {
const browserAPI = getBrowserAPI();
let selectors = Array.isArray(webhook?.selectors) ? [...webhook.selectors] : [];
let selectorContent = [];
try {
let payload;
const dateTimeVariables = buildDateTimeVariables();
if (isTest) {
payload = {
url: "https://example.com",
test: true,
triggeredAt: dateTimeVariables.nowIso,
};
} else {
// Get info about the active tab
const tabs = await browserAPI.tabs.query({
active: true,
currentWindow: true,
});
if (tabs.length === 0) {
throw new Error(browserAPI.i18n.getMessage("popupErrorNoActiveTab"));
}
const activeTab = tabs[0];
const currentUrl = activeTab.url;
if ((!selectors || selectors.length === 0) && webhook?.id) {
try {
const stored = await browserAPI.storage.sync.get("webhooks");
const storedHooks = Array.isArray(stored?.webhooks) ? stored.webhooks : [];
const storedMatch = storedHooks.find((w) => w.id === webhook.id);
if (storedMatch && Array.isArray(storedMatch.selectors)) {
selectors = storedMatch.selectors.filter((value) => typeof value === "string" && value.trim().length > 0);
}
} catch (error) {
console.debug("Failed to load stored selectors", error);
}
}
const canSendMessage =
typeof browserAPI.tabs?.sendMessage === "function" ||
(typeof browser !== "undefined" && typeof browser.tabs?.sendMessage === "function") ||
(typeof chrome !== "undefined" && typeof chrome.tabs?.sendMessage === "function");
if (selectors.length > 0 && canSendMessage) {
try {
let response;
if (browserAPI.tabs && typeof browserAPI.tabs.sendMessage === "function") {
response = await browserAPI.tabs.sendMessage(activeTab.id, {
type: "GET_SELECTOR_CONTENT",
selectors,
});
} else if (typeof browser !== "undefined" && typeof browser.tabs?.sendMessage === "function") {
response = await browser.tabs.sendMessage(activeTab.id, {
type: "GET_SELECTOR_CONTENT",
selectors,
});
} else if (typeof chrome !== "undefined" && typeof chrome.tabs?.sendMessage === "function") {
response = await new Promise((resolve, reject) => {
chrome.tabs.sendMessage(activeTab.id, {
type: "GET_SELECTOR_CONTENT",
selectors,
}, (res) => {
const err = chrome.runtime?.lastError;
if (err) {
reject(new Error(err.message));
} else {
resolve(res);
}
});
});
}
if (response && Array.isArray(response.selectorContent)) {
selectorContent = response.selectorContent.map((value) =>
typeof value === "string" ? value.trim() : ""
);
}
} catch (error) {
console.warn("Failed to retrieve selector content", error);
}
}
// Get browser and platform info
const browserInfo = await browserAPI.runtime.getBrowserInfo?.() || {};
const platformInfo = await browserAPI.runtime.getPlatformInfo?.() || {};
// Create default payload
payload = {
tab: {
title: activeTab.title,
url: currentUrl,
id: activeTab.id,
windowId: activeTab.windowId,
index: activeTab.index,
pinned: activeTab.pinned,
audible: activeTab.audible,
mutedInfo: activeTab.mutedInfo,
incognito: activeTab.incognito,
status: activeTab.status,
},
browser: browserInfo,
platform: platformInfo,
triggeredAt: dateTimeVariables.nowIso,
};
if (webhook && webhook.identifier) {
payload.identifier = webhook.identifier;
}
if (selectors.length > 0) {
payload.selectorContent = selectorContent;
}
if (webhook && webhook.customPayload) {
try {
const replacements = {
"{{tab.title}}": activeTab.title,
"{{tab.url}}": currentUrl,
"{{tab.id}}": activeTab.id,
"{{tab.windowId}}": activeTab.windowId,
"{{tab.index}}": activeTab.index,
"{{tab.pinned}}": activeTab.pinned,
"{{tab.audible}}": activeTab.audible,
"{{tab.incognito}}": activeTab.incognito,
"{{tab.status}}": activeTab.status,
"{{browser}}": JSON.stringify(browserInfo),
"{{platform.arch}}": platformInfo.arch || "unknown",
"{{platform.os}}": platformInfo.os || "unknown",
"{{platform.version}}": platformInfo.version,
"{{identifier}}": webhook.identifier || "",
"{{selectorContent}}": selectorContent,
...dateTimeVariables.values,
};
let customPayloadStr = webhook.customPayload;
Object.entries(replacements).forEach(([placeholder, value]) => {
const isPlaceholderInQuotes = customPayloadStr.match(new RegExp(`"[^"]*${placeholder.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[^"]*"`));
let replaceValue;
if (typeof value === 'string') {
replaceValue = JSON.stringify(value);
if (isPlaceholderInQuotes) {
// Remove the surrounding quotes added by JSON.stringify
replaceValue = replaceValue.slice(1, -1);
}
} else {
replaceValue = value === undefined ? 'null' : JSON.stringify(value);
}
// Escape special replacement patterns ($) to prevent them from being interpreted by String.prototype.replace
replaceValue = replaceValue.replace(/\$/g, '$$$$');
customPayloadStr = customPayloadStr.replace(
new RegExp(placeholder.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'),
replaceValue
);
});
const customPayload = JSON.parse(customPayloadStr);
payload = customPayload;
} catch (error) {
throw new Error(browserAPI.i18n.getMessage("popupErrorCustomPayloadJsonParseError", error.message));
}
}
}
let headers = { "Content-Type": "application/json" };
if (webhook && Array.isArray(webhook.headers)) {
webhook.headers.forEach(h => {
if (h.key && h.value) headers[h.key] = h.value;
});
}
const method = webhook && webhook.method ? webhook.method : "POST";
const fetchOpts = {
method,
headers,
};
const url = webhook.url;
if (method === "POST") {
fetchOpts.body = JSON.stringify(payload);
} else if (method === "GET") {
const urlObj = new URL(url);
urlObj.searchParams.set("payload", encodeURIComponent(JSON.stringify(payload)));
fetchOpts.body = undefined;
fetchOpts._url = urlObj.toString();
}
const fetchUrl = fetchOpts._url || url;
const response = await fetch(fetchUrl, fetchOpts);
if (!response.ok) {
throw new Error(browserAPI.i18n.getMessage("popupErrorHttp", response.status));
}
return response;
} catch (error) {
console.error("Error sending webhook:", error);
throw error;
}
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = { replaceI18nPlaceholders, getBrowserAPI, sendWebhook };
} else {
window.replaceI18nPlaceholders = replaceI18nPlaceholders;
window.getBrowserAPI = getBrowserAPI;
window.sendWebhook = sendWebhook;
}