-
Notifications
You must be signed in to change notification settings - Fork 0
/
show-page-with-token.js
80 lines (72 loc) · 2.87 KB
/
show-page-with-token.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
let showPageWithToken = async (githubUrl, token) => {
let rawUrl = githubUrl.replace("github.com", "b884r11mg5.execute-api.us-east-1.amazonaws.com/github-content-proxy/" + token).replace("/blob", "");
// HTML
document.open();
document.write(await getDocumentContentWithToken(rawUrl, githubUrl));
if(!document.head.querySelector('base')) {
let base = document.createElement('base');
base.href = rawUrl;
document.head.appendChild(base);
}
document.close();
// CSS
while(document.querySelector("link[rel=stylesheet]:not([status=clear])")) {
let element = document.querySelector("link[rel=stylesheet]:not([status=clear])");
let href = element.getAttribute("href")
if (!isPublicUrl(href)) element.outerHTML = '<style status="clear">' + await getContentWithToken(href, githubUrl) + "</style>";
else element.setAttribute("status", "clear");
}
// JS
while(document.querySelector("script[src]:not([status=clear])")) {
let element = document.querySelector("script[src]:not([status=clear])");
let script = document.createElement('script');
let src = element.getAttribute("src")
if (!isPublicUrl(src)) {
script.innerHTML = "(function () {" + await getContentWithToken(src, githubUrl) + "})();";
console.log(document.body);
document.body.appendChild(script);
script.setAttribute("status", "clear");
element.remove();
} else element.setAttribute("status", "clear");
}
window.dispatchEvent(new Event('load'));
// <a> link
while(document.querySelector("a[href]:not([status=clear])")) {
let element = document.querySelector("a[href]:not([status=clear])");
let href = element.href;
element.setAttribute(
"href",
href.replace(
"https://b884r11mg5.execute-api.us-east-1.amazonaws.com/github-content-proxy/" + token,
location.origin + location.pathname + "?https://github.com/"
) + "&" + token
);
element.setAttribute("status", "clear");
}
// TODO
// Frame
// @import
// loadScript()
}
let getDocumentContentWithToken = (rawUrl, githubUrl) => {
return new Promise((resolve) => {
fetch(rawUrl)
.then(res => {
if (!res.ok) throw new Error('400 or 500 에러 발생')
return res.text()
})
.then(text => resolve(text))
.catch(() => showPage(githubUrl))
});
}
let getContentWithToken = (rawUrl, githubUrl) => {
return new Promise((resolve) => {
fetch(rawUrl)
.then(res => {
if (!res.ok) throw new Error('400 or 500 에러 발생')
return res.text()
})
.then(text => resolve(text))
.catch(() => resolve(''))
});
}