-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathworker.js
56 lines (53 loc) · 1.44 KB
/
worker.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
async function dashUrl(ytUrl) {
return await fetch(ytUrl)
.then(async (r) => await r.text())
.then((r) => r.match(/(?<=dashManifestUrl":").+?(?=",)/g)[0]);
}
async function hlsUrl(ytUrl) {
return await fetch(ytUrl)
.then(async (r) => await r.text())
.then((r) => r.match(/(?<=hlsManifestUrl":").*\.m3u8/g)[0]);
}
async function handleRequest(request) {
let pathName = new URL(request.url).pathname.split("/");
switch (pathName[3]) {
case "master.mpd":
try {
return Response.redirect(
await dashUrl(
"https://www.youtube.com/channel/" + pathName[2] + "/live"
),
302
);
} catch (err) {
return Response.redirect(
await dashUrl("https://www.youtube.com/watch?v=" + pathName[2]),
302
);
}
case "master.m3u8":
try {
return Response.redirect(
await hlsUrl(
"https://www.youtube.com/channel/" + pathName[2] + "/live"
),
302
);
} catch (err) {
return Response.redirect(
await hlsUrl("https://www.youtube.com/watch?v=" + pathName[2]),
302
);
}
default:
console.log("Error: Invalid path. Use master.mpd or master.m3u8");
break;
}
}
addEventListener("fetch", async (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});