-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
62 lines (52 loc) · 1.81 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
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getJSFiles") {
findJavaScriptFiles().then(jsFiles => sendResponse({ jsFiles }));
return true; // Keep message channel open for async
}
});
async function findJavaScriptFiles() {
let jsLinks = new Set();
// 1) From <script> tags
document.querySelectorAll("script[src]").forEach(script => {
jsLinks.add(script.src);
});
// 2) From Performance API
performance.getEntriesByType("resource").forEach(entry => {
if (entry.name.endsWith(".js")) {
jsLinks.add(entry.name);
}
});
// 3) From inline scripts
document.querySelectorAll("script:not([src])").forEach(script => {
const matches = script.innerHTML.match(/https?:\/\/[^"'\s]+\.js/g);
if (matches) {
matches.forEach(url => jsLinks.add(url));
}
});
// 4) Possibly dynamic detection
await getNetworkJSFiles(jsLinks);
return Array.from(jsLinks);
}
// Example: hooking into network requests or DOM changes
async function getNetworkJSFiles(jsLinks) {
if (window.performance && window.performance.getEntriesByType) {
let resources = performance.getEntriesByType("resource");
resources.forEach(entry => {
if (entry.name.endsWith(".js")) {
jsLinks.add(entry.name);
}
});
}
// Observe new script insertions in the DOM
let observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.tagName === "SCRIPT" && node.src) {
jsLinks.add(node.src);
}
});
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
return jsLinks;
}