-
Notifications
You must be signed in to change notification settings - Fork 16
/
content.js
141 lines (123 loc) · 3.88 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
//
// (c) Lauritz Holtmann
//
function isHttpUrl(url) {
return url.startsWith('http://') || url.startsWith('https://');
}
function scanPage() {
const resources = {
iframes: [],
scripts: [],
styles: [],
anchors: [],
objects: [],
images: [],
videos: [],
audios: []
};
const pageUrl = window.location.href;
// Collect iframes
document.querySelectorAll('iframe').forEach(iframe => {
const src = iframe.src;
if (src && isHttpUrl(src)) {
resources.iframes.push({ url: src, element: 'iframe' });
}
});
// Collect scripts
document.querySelectorAll('script').forEach(script => {
const src = script.src;
if (src && isHttpUrl(src)) {
resources.scripts.push({ url: src, element: 'script' });
}
});
// Collect styles
document.querySelectorAll('link[rel="stylesheet"]').forEach(link => {
const href = link.href;
if (href && isHttpUrl(href)) {
resources.styles.push({ url: href, element: 'link[rel="stylesheet"]' });
}
});
// Collect anchor tags
document.querySelectorAll('a').forEach(anchor => {
const href = anchor.href;
if (href && isHttpUrl(href)) {
resources.anchors.push({ url: href, element: 'a' });
} else if (href && href.startsWith('mailto:')) {
resources.anchors.push({ url: href.replace('mailto:','mailto://'), element: 'a > mailto' });
}
});
// Collect object tags
document.querySelectorAll('object').forEach(object => {
const data = object.data;
if (data && isHttpUrl(data)) {
resources.objects.push({ url: data, element: 'object' });
}
});
// Collect image tags
document.querySelectorAll('img').forEach(img => {
const src = img.src;
if (src && isHttpUrl(src)) {
resources.images.push({ url: src, element: 'img' });
}
});
// Collect video tags
document.querySelectorAll('video').forEach(video => {
const src = video.src;
if (src && isHttpUrl(src)) {
resources.videos.push({ url: src, element: 'video' });
}
// Check for sources within <source> tags
video.querySelectorAll('source').forEach(source => {
const src = source.src;
if (src && isHttpUrl(src)) {
resources.videos.push({ url: src, element: 'video > source' });
}
});
});
// Collect audio tags
document.querySelectorAll('audio').forEach(audio => {
const src = audio.src;
if (src && isHttpUrl(src)) {
resources.audios.push({ url: src, element: 'audio' });
}
// Check for sources within <source> tags
audio.querySelectorAll('source').forEach(source => {
const src = source.src;
if (src && isHttpUrl(src)) {
resources.audios.push({ url: src, element: 'audio > source' });
}
});
});
console.log('External resources:', resources);
const domains = [
...resources.iframes,
...resources.scripts,
...resources.styles,
...resources.anchors,
...resources.objects,
...resources.images,
...resources.videos,
...resources.audios
].map(resource => ({
domain: (new URL(resource.url)).hostname,
pageUrl: pageUrl,
sinkElement: resource.element
})).filter(domainObj => !isIpAddress(domainObj.domain));
chrome.runtime.sendMessage({ type: 'checkDomains', domains: domains });
}
function isIpAddress(domain) {
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(domain);
}
function isIgnoredDomain(pageUrl, ignoredDomains) {
const hostname = (new URL(pageUrl)).hostname;
return ignoredDomains.includes(hostname);
}
chrome.storage.sync.get({ ignoredDomains: [] }, (items) => {
const ignoredDomains = items.ignoredDomains;
const pageUrl = window.location.href;
if (!isIgnoredDomain(pageUrl, ignoredDomains)) {
scanPage();
} else {
console.log(`Skipping scan for ignored domain: ${pageUrl}`);
}
});