-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
367 lines (329 loc) · 12.1 KB
/
Copy pathcontent.js
File metadata and controls
367 lines (329 loc) · 12.1 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
// ScriptVault v2.3.0 - Content Script Bridge
// Bridges messages between userscripts (USER_SCRIPT world) and background service worker
(function() {
'use strict';
// Prevent double initialization (use extension ID to avoid page-level spoofing)
const bridgeKey = '__ScriptVault_Bridge_' + chrome.runtime.id + '__';
if (window[bridgeKey]) {
return;
}
Object.defineProperty(window, bridgeKey, { value: true, writable: false, configurable: false });
// Unique channel ID to prevent conflicts with other extensions
const CHANNEL_ID = 'ScriptVault_' + chrome.runtime.id;
// Security allowlist — the bridge receives window.postMessage events that
// any page script can forge (channel ID is derived from the public extension
// ID). Keep this bridge telemetry-only; privileged GM APIs must use
// chrome.runtime messaging from the USER_SCRIPT world.
const ALLOWED_BRIDGE_ACTIONS = new Set([
'netlog_record',
'reportExecError',
'reportExecTime'
]);
function isPublicApiPageMessage(msg) {
if (!msg || typeof msg !== 'object') return false;
if (typeof msg.type !== 'string') return false;
if (!msg.type.startsWith('scriptvault:')) return false;
// Responses posted back into the page are visible to this listener too.
// Do not bounce them back to the service worker.
if (msg.type.endsWith(':response')) return false;
return true;
}
function isAllowedBridgeAction(action) {
if (typeof action !== 'string') return false;
return ALLOWED_BRIDGE_ACTIONS.has(action);
}
const BRIDGE_RATE_WINDOW_MS = 10000;
const BRIDGE_RATE_LIMIT = 60;
let bridgeRateWindowStartedAt = Date.now();
let bridgeRateCount = 0;
function consumeBridgeRateLimit() {
const now = Date.now();
if (now - bridgeRateWindowStartedAt >= BRIDGE_RATE_WINDOW_MS) {
bridgeRateWindowStartedAt = now;
bridgeRateCount = 0;
}
if (bridgeRateCount >= BRIDGE_RATE_LIMIT) return false;
bridgeRateCount += 1;
return true;
}
function boundedNumber(value, minimum, maximum, integer = false) {
const number = Number(value);
if (!Number.isFinite(number) || number < minimum || number > maximum) return undefined;
if (integer && !Number.isInteger(number)) return undefined;
return number;
}
function normalizeBridgeTelemetry(action, value) {
const data = value && typeof value === 'object' && !Array.isArray(value) ? value : {};
if (action === 'reportExecError') {
const error = typeof data.error === 'string' ? data.error.slice(0, 500) : '';
return error ? { kind: 'execution-error', error } : null;
}
if (action === 'reportExecTime') {
const duration = boundedNumber(data.time, 0, 86400000);
return duration === undefined ? null : { kind: 'execution-time', duration };
}
if (action !== 'netlog_record') return null;
const url = typeof data.url === 'string' ? data.url.slice(0, 4096) : '';
if (!url) return null;
const telemetry = {
kind: 'network',
url,
method: typeof data.method === 'string' ? data.method.slice(0, 16).toUpperCase() : 'GET',
type: typeof data.type === 'string' ? data.type.slice(0, 32) : 'fetch'
};
const status = boundedNumber(data.status, 0, 999, true);
const duration = boundedNumber(data.duration, 0, 86400000);
const responseSize = boundedNumber(data.responseSize, 0, 1073741824, true);
if (status !== undefined) telemetry.status = status;
if (duration !== undefined) telemetry.duration = duration;
if (responseSize !== undefined) telemetry.responseSize = responseSize;
if (typeof data.statusText === 'string') telemetry.statusText = data.statusText.slice(0, 256);
if (typeof data.error === 'string') telemetry.error = data.error.slice(0, 500);
return telemetry;
}
function redactBridgeEventData(data) {
if (!data || typeof data !== 'object') return data;
const safe = { ...data };
delete safe.response;
delete safe.responseText;
delete safe.responseXML;
delete safe.responseHeaders;
delete safe.payload;
delete safe.streamChunk;
return safe;
}
const CHAIN_DOM_EVENT_LIMIT = 20;
const chainDomEventListeners = new Map();
function normalizeChainDomEventTypes(eventTypes) {
const seen = new Set();
const normalized = [];
for (const eventType of Array.isArray(eventTypes) ? eventTypes : []) {
const value = String(eventType || '').trim();
if (!/^[A-Za-z][\w:.-]{0,63}$/.test(value) || seen.has(value)) continue;
seen.add(value);
normalized.push(value);
if (normalized.length >= CHAIN_DOM_EVENT_LIMIT) break;
}
return normalized;
}
function notifyChainDomEvent(event) {
chrome.runtime.sendMessage({
action: 'chainDomEvent',
eventType: event.type,
url: location.href
}).catch(() => {});
}
function installChainDomEventListeners(eventTypes) {
for (const [eventType, listener] of chainDomEventListeners) {
document.removeEventListener(eventType, listener, true);
}
chainDomEventListeners.clear();
for (const eventType of normalizeChainDomEventTypes(eventTypes)) {
const listener = (event) => notifyChainDomEvent(event);
chainDomEventListeners.set(eventType, listener);
document.addEventListener(eventType, listener, { capture: true, passive: true });
}
}
async function refreshChainDomEventTriggers() {
try {
const result = await chrome.runtime.sendMessage({ action: 'getChainDomEventTriggers' });
installChainDomEventListeners(result?.eventTypes || []);
} catch (_) {
installChainDomEventListeners([]);
}
}
// Listen for messages from userscript world (USER_SCRIPT or page context)
window.addEventListener('message', async (event) => {
// Security: Only accept messages from same window
if (event.source !== window) return;
const msg = event.data;
// Check for our message type
if (!msg || typeof msg !== 'object') return;
if (isPublicApiPageMessage(msg)) {
try {
const result = await chrome.runtime.sendMessage({
action: 'publicApi_handleWebMessage',
origin: event.origin,
message: msg
});
if (result?.response) {
window.postMessage(result.response, event.origin);
}
} catch (_) {
// Public API relay failures are intentionally silent to avoid noisy pages.
}
return;
}
if (msg.channel !== CHANNEL_ID) return;
if (msg.direction !== 'to-background') return;
const msgId = msg.id;
const action = msg.action;
// Security: reject any action that isn't a known userscript-safe telemetry action.
// Do not forward GM_* calls here: page scripts can post to this window too.
if (!isAllowedBridgeAction(action)) {
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
id: msgId,
error: 'Action not permitted via page-visible bridge',
success: false
}, '*');
return;
}
try {
const safeData = normalizeBridgeTelemetry(action, msg.data);
if (!safeData) throw new Error('Invalid page telemetry payload');
if (!consumeBridgeRateLimit()) throw new Error('Page telemetry rate limit exceeded');
const result = await chrome.runtime.sendMessage({
action: 'recordBridgeTelemetry',
data: safeData
});
// Send response back to userscript world
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
id: msgId,
result: result,
success: true
}, '*');
} catch (e) {
// Silently handle errors - no console output to avoid chrome://extensions error spam
const errorMsg = e.message || 'Unknown error';
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
id: msgId,
error: errorMsg,
success: false
}, '*');
}
});
// Listen for messages from background script (menu commands, value changes, XHR events)
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// Track if we handled the message
let handled = false;
// Menu command execution
if (message.action === 'executeMenuCommand') {
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
type: 'menuCommand',
scriptId: message.data?.scriptId,
commandId: message.data?.commandId
}, '*');
handled = true;
}
// Value change notifications
if (message.action === 'valueChanged') {
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
type: 'valueChanged',
scriptId: message.data?.scriptId,
key: message.data?.key,
remote: message.data?.remote
}, '*');
handled = true;
}
// XHR event forwarding
if (message.action === 'xhrEvent') {
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
type: 'xhrEvent',
requestId: message.data?.requestId,
scriptId: message.data?.scriptId,
eventType: message.data?.type,
data: redactBridgeEventData(message.data)
}, '*');
handled = true;
}
// GM_webSocket event forwarding (open, message, error, close)
if (message.action === 'webSocketEvent') {
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
type: 'webSocketEvent',
requestId: message.data?.requestId,
scriptId: message.data?.scriptId,
eventType: message.data?.type,
data: redactBridgeEventData(message.data)
}, '*');
handled = true;
}
// Notification event forwarding (click, done, buttonClick).
// Phase 11.11: buttonIndex is forwarded so the wrapper can fire
// onbuttonclick({ buttonClickIndex }) per ScriptCat semantics.
if (message.action === 'notificationEvent') {
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
type: 'notificationEvent',
scriptId: message.data?.scriptId,
notifTag: message.data?.notifId,
eventType: message.data?.type,
buttonIndex: message.data?.buttonIndex
}, '*');
handled = true;
}
// Download event forwarding (load, error, progress, timeout)
if (message.action === 'downloadEvent') {
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
type: 'downloadEvent',
scriptId: message.data?.scriptId,
downloadId: message.data?.downloadId,
eventType: message.data?.type,
data: message.data
}, '*');
handled = true;
}
// Audio state change event
if (message.action === 'audioStateChanged') {
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
type: 'audioStateChanged',
data: message.data
}, '*');
handled = true;
}
// Opened tab closed event
if (message.action === 'openedTabClosed') {
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
type: 'openedTabClosed',
scriptId: message.data?.scriptId,
closedTabId: message.data?.tabId
}, '*');
handled = true;
}
if (message.action === 'chainDomTriggersChanged') {
refreshChainDomEventTriggers();
handled = true;
}
// Always send response if we handled the message
// This prevents "message channel closed" errors
if (handled) {
sendResponse({ success: true });
}
// Return false for synchronous response (we never do async here)
return false;
});
// Signal readiness without exposing additional bridge material to page code.
try {
Promise.resolve(chrome.runtime.sendMessage({
action: 'reportDocumentReady',
url: location.href
})).catch(() => {});
} catch (_) { /* extension context may have restarted */ }
refreshChainDomEventTriggers();
Object.defineProperty(window, '__ScriptVault_BridgeReady__', { value: true, writable: false, configurable: false });
window.postMessage({
channel: CHANNEL_ID,
direction: 'to-userscript',
type: 'bridgeReady'
}, '*');
})();