-
Notifications
You must be signed in to change notification settings - Fork 2
/
content.js
280 lines (256 loc) · 8.25 KB
/
content.js
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
// Arrays to store URLs and endpoints
const urls = [];
const endpoints = [];
const newEndpoints = [];
const newURLs = [];
const excludeExt = [
".js",
".svg",
".ts",
".jpg",
".json",
".css",
".png",
".webp",
".mp4",
".woff",
".ttf",
];
const excludeDomain = [
"www.w3.org",
"reactjs.org",
"googleapis.com",
"googletagmanager.com",
"schema.org",
"wikipedia.org",
"goo.gl",
"googleadservices.com",
"doubleclick.net",
"cdnjs.cloudflare.com",
"use.typekit.net",
"react.dev",
"hubspot.com",
"hs-analytics.net",
"cloudfront.net",
"gstatic.com",
"fonts.googleapis.com",
"ajax.googleapis.com",
"cdn.jsdelivr.net",
"bootstrapcdn.com",
"jsdelivr.net",
"stackpathcdn.com",
"optimizely.com",
"adservice.google.com",
"adservice.googleadservices.com",
"akamai.net",
"akamaihd.net",
"amazonaws.com",
"google-analytics.com",
"googlesyndication.com",
"adsafeprotected.com",
"cloudflare.com",
"licdn.com",
"ytimg.com",
"pixel.wp.com",
"clickfunnels.com",
"cdn.optimizely.com",
"cdn.segment.com",
"d3js.org",
"fastly.net",
"googletagservices.com",
"ads-twitter.com",
"googleusercontent.com",
"tiktokcdn.com",
"fbcdn.net",
"twimg.com",
"fbsbx.com",
"cdn.ampproject.org",
"azureedge.net"
];
// Cross-browser support
const browserAPI = typeof browser !== "undefined" ? browser : chrome;
// Debounce to avoid excessive calls to saveDataToStorage
let debounceTimer;
function debounceSaveData(tabUrl, delay = 1000) {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
saveDataToStorage(tabUrl);
}, delay);
}
// Utility function to extract the domain from a URL
function getDomainFromUrl(url) {
try {
const domain = new URL(url).hostname; // Extract the domain part
return domain;
} catch (e) {
console.error(`Invalid URL: ${url}`, e);
return null;
}
}
function handleMatches(isNew, matchedText, array, newArray, data) {
if (isNew && !array.some((item) => item.text === matchedText)) {
newArray.push(data);
} else if (!array.some((item) => item.text === matchedText)) {
array.push(data);
}
}
let matchIdCounter = 1; // Initialize counter for match IDs
// Function to process JavaScript content and categorize matches
function processJavaScriptContent(scriptContent, filename, isNew = false) {
const regex =
/(?:`|"|'|\n|\r)(((?:[a-zA-Z]{1,10}:\/\/|\/\/)[^"'\/]{1,}\.[a-zA-Z]{2,}[^"']{0,})|((?:\/|\.\.\/|\.\/)[^"'><,;| *()(%%$^\/\\\[\]][^"'><,;|()]{1,})|([a-zA-Z0-9_\-\/]{1,}\/[a-zA-Z0-9_\-\/]{1,}\.(?:[a-zA-Z]{1,4}|action)(?:[\?|\/][^"|']{0,}|))|([a-zA-Z0-9_\-]{1,}\.(?:php|asp|aspx|cfm|jsp|json|js|action|html|htm|bak|do|txt|xml|xls|xlsx|key|env|pem|git|ovpn|log|secret|secrets|access|dat|db|sql|pwd|passwd|gitignore|properties|dtd|conf|cfg|config|configs|apk|cgi|sh|py|java|rb|rs|go|yml|yaml|toml|php4|zip|tar|tar.bz2|tar.gz|rar|7z|gz|dochtml|doc|docx|csv|odt|ts|phtml|php5|pdf)(?:\?[^"|^']{0,}|)))(?:`|"|'|\n|\r)/g;
let match;
while ((match = regex.exec(scriptContent)) !== null) {
const matchedText = match[1] || match[0];
const lineNumber = scriptContent.slice(0, match.index).split("\n").length;
const data = {
id: matchIdCounter++,
index: match.index,
text: matchedText,
line: lineNumber,
filename,
};
if (
matchedText.startsWith("http:") ||
matchedText.startsWith("https:") ||
matchedText.startsWith("ws:") ||
matchedText.startsWith("wss:") ||
/^\/\/([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/.test(matchedText)
) {
if (
!excludeDomain.some((domain) => matchedText.includes(domain)) &&
!excludeExt.some((ext) => matchedText.includes(ext))
) {
handleMatches(isNew, matchedText, urls, newURLs, data);
}
} else if (
!excludeExt.some((ext) => matchedText.endsWith(ext)) &&
!matchedText.startsWith("//")
) {
handleMatches(isNew, matchedText, endpoints, newEndpoints, data);
}
}
}
// Async function to process all JavaScript scripts on the page
async function processAllScripts() {
const scripts = Array.from(document.querySelectorAll("script"));
for (const script of scripts) {
if (script.src) {
if (!excludeDomain.some((domain) => script.src.includes(domain))) {
try {
const response = await fetch(script.src);
const scriptContent = await response.text();
processJavaScriptContent(scriptContent, script.src);
} catch (err) {
console.error("Error fetching JS file:", err);
}
}
} else {
processJavaScriptContent(script.innerHTML, document.location.href);
}
}
}
// Function to save data to storage based on the tab URL
function saveDataToStorage(tabUrl) {
const domain = getDomainFromUrl(tabUrl);
if (!domain) return;
// Prepare the data to be saved (array of objects)
const data = {
urls: urls, // Using the arrays of objects directly
endpoints: endpoints,
newEndpoints: newEndpoints,
newURLs: newURLs,
};
// Retrieve existing data for the domain, merge new data with existing, and save
browserAPI.storage.local.get(domain, (result) => {
const existingData = result[domain] || {
urls: [],
endpoints: [],
newEndpoints: [],
newURLs: [],
};
// Merge the existing data with the new data
const mergedData = {
urls: [...existingData.urls, ...data.urls],
endpoints: [...existingData.endpoints, ...data.endpoints],
newEndpoints: [...existingData.newEndpoints, ...data.newEndpoints],
newURLs: [...existingData.newURLs, ...data.newURLs],
};
// Remove duplicates based on 'text' property
mergedData.urls = mergedData.urls.filter(
(item, index, self) =>
index === self.findIndex((t) => t.text === item.text)
);
mergedData.endpoints = mergedData.endpoints.filter(
(item, index, self) =>
index === self.findIndex((t) => t.text === item.text)
);
mergedData.newEndpoints = mergedData.newEndpoints.filter(
(item, index, self) =>
index === self.findIndex((t) => t.text === item.text)
);
mergedData.newURLs = mergedData.newURLs.filter(
(item, index, self) =>
index === self.findIndex((t) => t.text === item.text)
);
// Store the merged data back to the domain
browserAPI.storage.local.set({ [domain]: mergedData }, () => {
console.log(`Data stored for domain ${domain}:`, mergedData);
});
});
}
// Initialize by processing scripts and setting up mutation observer
function init() {
browserAPI.runtime.sendMessage({ type: "getTabUrl" }, async (tabUrl) => {
await processAllScripts();
debounceSaveData(tabUrl);
});
const targetNode = document.documentElement; // Observe the entire document
const observerConfig = { childList: true, subtree: true }; // Observe additions in the DOM
const callback = async (mutationsList) => {
const newScripts = [];
for (const mutation of mutationsList) {
if (mutation.type === "childList") {
mutation.addedNodes.forEach((node) => {
if (
node.nodeType === Node.ELEMENT_NODE &&
node.tagName === "SCRIPT"
) {
newScripts.push(node);
}
});
}
}
// Process newly added scripts
if (newScripts.length > 0) {
for (const script of newScripts) {
if (script.src) {
// Skip excluded domains
if (!excludeDomain.some((domain) => script.src.includes(domain))) {
try {
const response = await fetch(script.src);
const scriptContent = await response.text();
processJavaScriptContent(scriptContent, script.src, true);
} catch (err) {
console.error("Error fetching JS file:", err);
}
}
} else {
processJavaScriptContent(
script.innerHTML,
document.location.href,
true
);
}
}
// Save data after processing
browserAPI.runtime.sendMessage({ type: "getTabUrl" }, (tabUrl) => {
debounceSaveData(tabUrl);
});
}
};
const observer = new MutationObserver(callback); // Create an observer instance
observer.observe(targetNode, observerConfig); // Start observing
}
// On window load, initialize script processing
window.onload = init;