-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (50 loc) · 1.48 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
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond with hello worker text
* @param {Request} request
* @param {string} name of the cookie to grab
*/
//Class to handle the webpage title
class th {
element(e)
{
e.prepend("Raikan10's ");
}
}
//Class to handle description
class des {
element(e) {
e.setInnerContent("If you guys are reading this, thank you from the bottom of my heart! You guys are awesome!");
}
}
//class to handle the link
class link {
element(e) {
e.setInnerContent("Add me on LinkedIn");
e.setAttribute("href", "https://www.linkedin.com/in/ajay-rajnikanth/");
}
}
async function handleRequest(request) {
const url = "https://cfw-takehome.developers.workers.dev/api/variants";
var urls = await fetch(url)
.then((response) => {
return response.json();
});
let cookieString = request.headers.get('Cookie');
const cookie = (cookieString)?cookieString[cookieString.length -1]:null;
urls = urls.variants;
var which = (cookie == null)?Math.floor(Math.random() * 2):cookie;
console.log(which);
var response = await fetch(urls[which]);
//Rewriting HTML template
const hr = new HTMLRewriter()
.on('title', new th())
.on('h1#title', new th())
.on('p#description', new des())
.on('a#url', new link());
response = new Response(response.body, response)
if(cookie==null)response.headers.set('Set-Cookie', 'which='+which);
return hr.transform(response);
}