-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.nonmoduleworker.js
72 lines (63 loc) · 1.75 KB
/
index.nonmoduleworker.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
const someHost = "https://example.org"
const url = someHost + ""
async function gatherResponse(response) {
const { headers } = response
const contentType = headers.get("content-type") || ""
if (contentType.includes("application/json")) {
return JSON.stringify(await response.json())
}
else if (contentType.includes("application/text")) {
return response.text()
}
else if (contentType.includes("text/html")) {
return response.text()
}
else {
return response.text()
}
}
class AttributeRewriter {
constructor(attributeName) {
this.attributeName = attributeName
}
element(element) {
const attribute = element.getAttribute(this.attributeName)
if (attribute) {
element.setAttribute(
this.attributeName,
attribute.replace('iana', 'bananarama'),
)
}
}
}
class AppendBob {
constructor(somdata) {
this.somdata = somdata;
}
element(element) {
element.append(`<p>is your uncle ${this.somdata}</p>`,{html: true});
}
}
async function handleRequest(request, path) {
const urll = new URL(request.url);
const init = {
headers: {
"content-type": "text/html;charset=UTF-8",
},
}
const response = await fetch(someHost+urll.pathname, init)
const contentType = response.headers.get("Content-Type")
const results = await gatherResponse(response)
console.log(results)
if (contentType.startsWith("text/html")) {
const rewriter = new HTMLRewriter()
.on("a", new AttributeRewriter("href"))
.on("body div", new AppendBob(urll.pathname))
return rewriter.transform(new Response(results, init));
} else {
return new Response(results, init)
}
}
addEventListener("fetch", event => {
return event.respondWith(handleRequest(event.request, event.path))
})