-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
182 lines (159 loc) · 5.98 KB
/
background.js
File metadata and controls
182 lines (159 loc) · 5.98 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
/**********************************************************************
* ShyHurricane – Chrome MV-3 (Debugger-API implementation)
**********************************************************************/
/* ---------- Config & persisted settings ---------- */
let DOMAINS_IN_SCOPE = [];
let MCP_SERVER_URL = "http://localhost:8000";
const INDEX_PATH = "/index";
chrome.storage.local.get(["mcpServerUrl", "domainsInScope"]).then(items => {
if (items.mcpServerUrl) MCP_SERVER_URL = items.mcpServerUrl;
if (items.domainsInScope) DOMAINS_IN_SCOPE = items.domainsInScope;
});
chrome.storage.local.onChanged.addListener(changes => {
if (changes.mcpServerUrl) MCP_SERVER_URL = changes.mcpServerUrl.newValue || MCP_SERVER_URL;
if (changes.domainsInScope) DOMAINS_IN_SCOPE = changes.domainsInScope.newValue || [];
});
/* ---------- Helpers ---------- */
const SKIP_PREFIXES = ["audio/", "video/", "font/", "binary/"];
const SKIP_TYPES = new Set([
"application/octet-stream", "application/pdf", "application/x-pdf",
"application/zip", "application/x-zip-compressed", "application/x-protobuf",
"application/font-woff", "application/font-woff2", "application/vnd.ms-fontobject"
]);
const urlInScope = url => {
if (url.startsWith(MCP_SERVER_URL)) return false;
if (DOMAINS_IN_SCOPE.length === 0) return true;
try {
const host = new URL(url).hostname;
return DOMAINS_IN_SCOPE.some(d => host.endsWith(d));
} catch {
return false;
}
};
const shouldSkip = ct => {
if (!ct) return false;
const lower = ct.toLowerCase();
if (lower.includes("+json") || lower.includes("+xml")) return false;
if (SKIP_PREFIXES.some(p => lower.startsWith(p))) return true;
if (lower.startsWith("image/") && !lower.includes("svg")) return true;
return SKIP_TYPES.has(lower);
};
const toKatanaHeaders = obj => {
const out = {};
for (const [name, value] of Object.entries(obj || {})) {
const k = name.toLowerCase();
out[k] = out[k] ? `${out[k]};${value}` : value;
}
return out;
};
/* ---------- Target management ---------- */
const PROTOCOL_VERSION = "1.3";
const targets = new Map(); // tabId → {attached, requests: Map()}
function attachToTab(tabId) {
const target = {tabId};
chrome.debugger.attach(target, PROTOCOL_VERSION, () => {
if (chrome.runtime.lastError) return; // tab might have closed meanwhile
chrome.debugger.sendCommand(target, "Network.enable");
targets.set(tabId, {target, requests: new Map()});
});
}
function detachFromTab(tabId) {
const entry = targets.get(tabId);
if (entry) {
chrome.debugger.detach(entry.target, () => {
});
targets.delete(tabId);
}
}
/* attach to all existing + future tabs */
chrome.tabs.query({}, tabs => tabs.forEach(t => attachToTab(t.id)));
chrome.tabs.onCreated.addListener(tab => attachToTab(tab.id));
chrome.tabs.onRemoved.addListener((tabId) => detachFromTab(tabId));
/* ---------- Debugger-event handler ---------- */
chrome.debugger.onEvent.addListener((source, method, params) => {
const entry = targets.get(source.tabId);
if (!entry) return;
const reqs = entry.requests;
switch (method) {
/* ---- REQUEST ---- */
case "Network.requestWillBeSent": {
const {requestId, request} = params;
if (!urlInScope(request.url)) return;
reqs.set(requestId, {
method: request.method,
endpoint: request.url,
reqHeaders: {}, // filled later
reqBody: request.postData,
resHeaders: {},
status: undefined
});
break;
}
case "Network.requestWillBeSentExtraInfo": {
const r = reqs.get(params.requestId);
if (r) r.reqHeaders = params.headers;
break;
}
/* ---- RESPONSE ---- */
case "Network.responseReceived": {
const r = reqs.get(params.requestId);
if (r) {
r.status = params.response.status;
r.resHeaders = params.response.headers;
}
break;
}
case "Network.loadingFinished": {
const r = reqs.get(params.requestId);
if (!r) break;
const ct = r.resHeaders["content-type"] || r.resHeaders["Content-Type"];
if (shouldSkip(ct)) {
reqs.delete(params.requestId);
break;
}
chrome.debugger.sendCommand(
source,
"Network.getResponseBody",
{requestId: params.requestId},
(bodyObj) => {
const responseBody = bodyObj
? (bodyObj.base64Encoded
? atob(bodyObj.body) // decode if base64
: bodyObj.body)
: undefined;
flushEntry(r, responseBody);
reqs.delete(params.requestId);
}
);
break;
}
/* ---- Error / redirect cleanup ---- */
case "Network.loadingFailed":
case "Network.webSocketClosed":
case "Network.webSocketHandshakeResponseReceived":
reqs.delete(params.requestId);
break;
}
});
/* ---------- Output helper ---------- */
function flushEntry(r, responseBody) {
const entry = {
timestamp: new Date().toISOString(),
request: {
method: r.method,
endpoint: r.endpoint,
headers: toKatanaHeaders(r.reqHeaders),
body: r.reqBody
},
response: {
status_code: r.status,
headers: toKatanaHeaders(r.resHeaders),
body: responseBody
}
};
fetch(MCP_SERVER_URL + INDEX_PATH, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(entry)
}).catch(err => console.error("[ShyHurricane]", err));
}