forked from ComputerElite/ComputerElite.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.html
107 lines (96 loc) · 4.49 KB
/
loader.html
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
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
Yeah so it load Something
<script>
var url = new URL(window.location.href);
var uri = url.searchParams.get("uri");
var html = MakeTextGetRequest(uri)
var baseDir = uri
if(baseDir.endsWith("/")) baseDir = baseDir.substring(0, baseDir.length - 1)
baseDir = baseDir.substring(0, baseDir.lastIndexOf("/"))
//Check if base dir exceeds website domain
const websiteRegex = /^https?\:\/\/([^/]+(\/)?)$/g
if(uri.match(websiteRegex)) {
baseDir = uri.endsWith("/") ? uri : uri + "/"
}
// Get every match for a relative uri and replace it with an absolute one
const reg = /("|`|')(((\.\.\/)+)|\/)?[a-zA-Z0-9\-_\.\/\{\}\$]+("|`|')/g
var links = []
while ((match = reg.exec(html)) !== null) {
match[0] = match[0].substring(1, match[0].length - 1)
if(!match[0].includes(".") && !match[0].endsWith("/") && !match[0].startsWith("//") || match[0].startsWith("///") || match[0].startsWith("$")) continue;
console.log(`Found ${match[0]} start=${match.index} end=${reg.lastIndex}.`);
var replacement = GetAbsoluteLink(baseDir, match[0])
links.push({
absolute: replacement,
relative: match[0],
start: match.index,
end: reg.lastIndex
})
console.log("replacing with " + replacement)
}
//Start replacement
var length = 0
links.forEach(link => {
html = html.substring(0, link.start + 1 + length) + html.substring(link.end - 1 + length, html.length)
html = InsertString(link.absolute, link.start + 1 + length, html)
length += link.absolute.length - link.relative.length
})
const scriptReg = /<script( src=".+?")?((\/>)|(>.*?<\/script>))/gs
const headStart = html.indexOf("<head>") + 6
var scripts = []
while ((match = scriptReg.exec(html)) !== null) {
scripts.push(match[0]);
console.log("moved script " + match[0] + " into head")
}
scripts.forEach(s => {
html = html.replace(s, "")
})
document.documentElement.innerHTML = html
const srcRegex = /src=".+?"/gs
var i = 0
scripts.forEach(e => {
console.log(i)
i++
var script = document.createElement("script")
if(e.substring(e.indexOf("<"), e.indexOf(">")).match(srcRegex))
{
var found = srcRegex.exec(e.substring(0, e.indexOf(">")))[0];
script.src = found.substring(5, found.length - 1)
} else script.appendChild(document.createTextNode(e.substring(e.indexOf(">") + 1, e.lastIndexOf("<") - 1)))
document.head.appendChild(script)
})
function GetAbsoluteLink(baseUri, relativeUri) {
var absolute = baseUri;
if(relativeUri.startsWith("../")) {
var tmp = absolute.split("/")
tmp.pop();
absolute = tmp.join("/")
absolute = GetAbsoluteLink(absolute, relativeUri.substring(3, relativeUri.length))
} else if(relativeUri.startsWith("//")) {
absolute = baseUri.substring(0, baseUri.indexOf("/")) + relativeUri
} else {
absolute += "/" + relativeUri
}
return absolute
}
function InsertString(toInsert, position, text) {
return [text.slice(0, position), toInsert, text.slice(position)].join('')
}
function MakeTextGetRequest(url) {
var request = new XMLHttpRequest();
request.open('GET', url, false);
request.send(null);
if (request.status == 200) {
return request.responseText;
} else {
return "Something went wrong: " + request.status;
}
}
</script>
</body>
</html>