forked from kusoidev/ScriptFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-bridge.js
More file actions
85 lines (72 loc) · 3.02 KB
/
content-bridge.js
File metadata and controls
85 lines (72 loc) · 3.02 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
(async function() {
'use strict';
let shouldLog;
try {
const result = await chrome.storage.sync.get(['debugLogging']);
shouldLog = !!result.debugLogging;
} catch (error) {
console.error('[ScriptFlow Bridge] Failed to load initial debug setting:', error);
shouldLog = false;
}
function log(message, ...args) {
if (shouldLog) {
console.log('[ScriptFlow Bridge]', message, ...args);
}
}
log('Initialized - relay mode');
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === 'sync' && changes.debugLogging !== undefined) {
shouldLog = !!changes.debugLogging.newValue;
if (shouldLog) {
console.log('[ScriptFlow Bridge]', 'Debug logging dynamically enabled.');
}
}
});
window.addEventListener('message', async function(event) {
if (event.source !== window) return;
if (event.data && event.data.type === 'SF_FETCH_REQUEST') {
const { requestId, url, method, headers, credentials, body } = event.data;
log('Relaying to background:', url);
try {
const response = await chrome.runtime.sendMessage({
action: 'fetch',
url: url,
method: method || 'GET',
headers: headers || {},
credentials: credentials || 'include',
body: body
});
log('Response received from background:', {
status: response.status,
ok: response.ok,
hasBody: !!response.body,
bodyLength: response.body ? response.body.length : 0
});
const responseBody = response.body !== undefined && response.body !== null
? String(response.body)
: '';
window.postMessage({
type: 'SF_FETCH_RESPONSE',
requestId: requestId,
ok: response.ok,
status: response.status,
statusText: response.statusText,
headers: response.headers || {},
body: responseBody
}, '*');
log('Fetch success:', url, response.status);
} catch (error) {
log('Fetch failed:', url, error);
window.postMessage({
type: 'SF_FETCH_RESPONSE',
requestId: requestId,
ok: false,
status: 0,
statusText: error.message || 'Unknown error',
error: error.message || 'Unknown error',
body: ''
}, '*');
}
}
});
})();