-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (94 loc) · 2.78 KB
/
index.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
const links = [
{name: "Linkedin", url: "https://linkedin.com"},
{name: "GitHub", url: "https://github.com"},
{name: "Twitter", url: "https://twitter.com"},
{name: "Facebook", url: "https://facebook.com"},
]
const socialLinks = [
{iconSrc: "https://simpleicons.org/icons/linkedin.svg", url: "https://www.linkedin.com/in/arthurbbaker"},
{iconSrc: "https://simpleicons.org/icons/github.svg", url: "https://github.com/MCArth"},
]
const htmlAddress = "https://static-links-page.signalnerve.workers.dev"
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Handle request
* @param {Request} request
*/
async function handleRequest(request) {
// Returning links if path is /links
const path = request.url.split('/')
const API = path[path.length-1]
if (API === 'links') {
return new Response(JSON.stringify(links), {
headers: { 'content-type': 'application/json' },
})
}
const res = await fetch(htmlAddress)
// Return error if html page to edit responds with an error
if (!res.ok) {
return new Response(
null,
{
status: 500,
statusText: `The target html hosting server responded with ${res.status}`
}
)
}
// make necessary changes to html
return new HTMLRewriter()
.on("div#links", new LinkWriter())
.on("div#social", new SocialLinkWriter())
.on("div#profile", new ProfileUpdater())
.on("img#avatar", new SetImage())
.on("h1#name", new SetName())
.on('title', new TitleRewriter())
.on('body', new BackgroundChanger())
.transform(res)
}
class LinkWriter {
element(ele) {
let htmlLinks = ""
for (const link of links) {
htmlLinks += `<a href="${link.url}">${link.name}</a>`
}
ele.setInnerContent(htmlLinks, {html: true})
}
}
class SocialLinkWriter {
element(ele) {
let htmlLinks = ""
for (const link of socialLinks) {
htmlLinks += `<a href="${link.url}"><img style="width: 32px; height: 32px;" src=${link.iconSrc}></img></a>`
}
ele.setInnerContent(htmlLinks, {html: true})
ele.setAttribute('style', 'display: flex;')
ele.setAttribute('class', '')
}
}
class ProfileUpdater {
element(ele) {
ele.setAttribute('style', 'display: block')
}
}
class SetImage {
element(ele) {
ele.setAttribute('src', 'https://media-exp1.licdn.com/dms/image/C4E03AQH9f-Zhnidd7A/profile-displayphoto-shrink_800_800/0?e=1608768000&v=beta&t=a9gSje1WM33RRMXdbBu5xKJPXGkv64_YK0DrHhtWMXM')
}
}
class SetName {
element(ele) {
ele.setInnerContent('<div style="color: black; text-align: center;">Arthur</div>', {html: true})
}
}
class TitleRewriter {
element(ele) {
ele.setInnerContent('boom', {html: false})
}
}
class BackgroundChanger {
element(ele) {
ele.setAttribute('class', 'bg-gray-200')
}
}