forked from swiftinside/invidious
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
140 lines (127 loc) · 5.58 KB
/
test.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* eslint-disable no-console */
const kDefaultInvidiousURL = new URL('https://www.invidio.us');
const kKnowninvidiousHosts = [
'invidio.us',
'invidious.snopyta.org',
'invidious.13ad.de',
'invidious.ggc-project.de',
'yt.maisputain.ovh',
'invidious.toot.koeln',
'yewtu.be',
'invidious.fdn.fr',
];
const ythostnames = [
'youtube.com',
'www.youtube.com',
'm.youtube.com',
'www.m.youtube.com',
'youtube-nocookie.com',
'www.youtube-nocookie.com',
];
const ytwatchhostnames = [
'youtu.be',
];
function alreadySanitized(url) {
for (let i = 0; i < kKnowninvidiousHosts.length; i += 1) {
if (url.hostname === kKnowninvidiousHosts[i]) {
return true;
}
}
return false;
}
function sanitizeYouTubeURL(url, invidiousURL) {
// We are given a YT URL, translate it to the invidious equivalent.
if (alreadySanitized(url)) {
return url;
}
const sanitizedURL = new URL(invidiousURL.href);
// 1. Handle cases where a video ID is specified in the input URL
let p = /(?:\/|%3D|v=|vi=)(?<vid>[0-9A-z-_]{11})(?:[%#?&]|$)/;
let m = p.exec(url.href);
if (m !== null && m.length > 1) {
sanitizedURL.pathname = '/watch';
sanitizedURL.search = `?v=${m.groups.vid}`;
return sanitizedURL;
}
// 2. Handle all other cases.
p = /(.*\.)youtu(be.com|\.be)\/(.*)/;
m = p.exec(url.href);
if (m !== null && m.length >= 3) {
sanitizedURL.pathname = url.pathname;
sanitizedURL.search = url.search;
return sanitizedURL;
}
// 3. Dunno how to handle that, so I will just redirect to invidious
return sanitizedURL;
}
function isYouTubeLink(url) {
return ythostnames.includes(url.hostname) || ytwatchhostnames.includes(url.hostname);
}
function test(url, instanceURL) {
if (isYouTubeLink(url)) {
return sanitizeYouTubeURL(url, instanceURL);
}
return url;
}
const testURLs = [
'https://m.youtube.com/watch?v=v0NSeysrDYw',
'http://www.youtube.com/watch?v=v0NSeysrDYw',
'http://www.youtube.com/v/v0NSeysrDYw?version=3&autohide=1',
'http://youtu.be/v0NSeysrDYw',
'http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3Dv0NSeysrDYw&format=json',
'http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3Dv0NSeysrDYw%26feature%3Dshare',
'https://www.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3Dv0NSeysrDYw%26feature%3Dem-uploademail',
'https://www.youtube.com/watch?v=v0NSeysrDYw&feature=em-uploademail',
'https://www.youtube.com/watch?v=v0NSeysrDYw&feature=feedrec_grec_index',
'https://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/v0NSeysrDYw',
'https://www.youtube.com/v/v0NSeysrDYw?fs=1&hl=en_US&rel=0',
'https://www.youtube.com/watch?v=v0NSeysrDYw#t=0m10s',
'https://www.youtube.com/embed/v0NSeysrDYw?rel=0',
'https://www.youtube-nocookie.com/embed/v0NSeysrDYw?rel=0',
'https://www.youtube-nocookie.com/embed/v0NSeysrDYw?rel=0',
'http://www.youtube.com/user/Scobleizer#p/u/1/v0NSeysrDYw',
'http://www.youtube.com/watch?v=v0NSeysrDYw&feature=channel',
'http://www.youtube.com/watch?v=v0NSeysrDYw&playnext_from=TL&videos=osPknwzXEas&feature=sub',
'http://www.youtube.com/ytscreeningroom?v=v0NSeysrDYw',
'http://www.youtube.com/watch?v=v0NSeysrDYw&feature=youtu.be',
'http://www.youtube.com/user/Scobleizer#p/u/1/v0NSeysrDYw?rel=0',
'http://www.youtube.com/embed/v0NSeysrDYw?rel=0',
'https://www.youtube.com/watch?v=v0NSeysrDYw',
'http://youtube.com/v/v0NSeysrDYw?feature=youtube_gdata_player',
'http://youtube.com/?v=v0NSeysrDYw&feature=youtube_gdata_player',
'http://www.youtube.com/watch?v=v0NSeysrDYw&feature=youtube_gdata_player',
'http://youtube.com/?vi=v0NSeysrDYw&feature=youtube_gdata_player',
'http://youtube.com/watch?v=v0NSeysrDYw&feature=youtube_gdata_player',
'http://youtube.com/watch?vi=v0NSeysrDYw&feature=youtube_gdata_player',
'http://youtube.com/vi/v0NSeysrDYw?feature=youtube_gdata_player',
'http://youtu.be/v0NSeysrDYw?feature=youtube_gdata_player',
'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/v0NSeysrDYw',
'https://www.youtube.com/watch?v=v0NSeysrDYw&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655',
'http://www.youtube.com/v/v0NSeysrDYw?fs=1&hl=en_US&rel=0',
'http://www.youtube.com/watch?v=v0NSeysrDYw&feature=feedrec_grec_index',
'http://www.youtube.com/watch?v=v0NSeysrDYw#t=0m10s',
'http://www.youtube.com/embed/v0NSeysrDYw',
'http://www.youtube.com/v/v0NSeysrDYw',
'http://www.youtube.com/e/v0NSeysrDYw',
'http://www.youtube.com/?v=v0NSeysrDYw',
'http://www.youtube.com/watch?feature=player_embedded&v=v0NSeysrDYw',
'http://www.youtube.com/?feature=player_embedded&v=v0NSeysrDYw',
'http://www.youtube.com/user/IngridMichaelsonVEVO#p/u/11/v0NSeysrDYw',
'http://www.youtube-nocookie.com/v/v0NSeysrDYw?version=3&hl=en_US&rel=0',
'http://www.youtube.com/user/dreamtheater#p/u/1/v0NSeysrDYw',
'https://youtu.be/v0NSeysrDYw?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b',
'http://www.youtube.com/watch?v=v0NSeysrDYw&feature=youtu.be',
'http://youtu.be/v0NSeysrDYw&feature=channel',
'http://www.youtube.com/ytscreeningroom?v=v0NSeysrDYw',
'http://www.youtube.com/embed/v0NSeysrDYw?rel=0',
'http://youtube.com/vi/v0NSeysrDYw&feature=channel',
'http://youtube.com/?v=v0NSeysrDYw&feature=channel',
'http://youtube.com/?feature=channel&v=v0NSeysrDYw',
'http://youtube.com/?vi=v0NSeysrDYw&feature=channel',
'http://youtube.com/watch?v=v0NSeysrDYw&feature=channel',
'http://youtube.com/watch?vi=v0NSeysrDYw&feature=channel',
'https://www.youtube.com/buzzsprout',
];
for (let i = 0; i < testURLs.length; i += 1) {
console.log(`Test ${i}: ${testURLs[i]} -> ${test(new URL(testURLs[i]), kDefaultInvidiousURL).href}`);
}