-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.js
executable file
·73 lines (56 loc) · 1.99 KB
/
init.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
require(['dojo/_base/kernel', 'dojo/ready'], function (dojo, ready) {
ready(function () {
document.addEventListener('paste', async (event) => {
try {
await tryPasteLink(event);
} catch (e) {
}
});
});
});
async function tryPasteLink(event) {
if (event.target.id !== 'feedDlg_feedUrl')
return;
let text = (event.clipboardData || window.clipboardData).getData('text');
let url = new URL(text);
if (isValidYoutubeLink(url)) {
event.target.value = await getRssFeedUrl(url);
event.preventDefault();
}
}
async function getRssFeedUrl(url) {
let lastPart = url.pathname.split('/').pop();
if (isChannelId(url)) {
return getRssLinkFromChannelId(lastPart);
} else if (isChannelName(url)) {
return await getRssLinkFromChannelName(lastPart);
} else if (isUser(url)) {
return getRssLinkFromUser(lastPart);
}
throw new Error("channel format not recognized");
}
// TODO: this should fetch a channel id but I can't find a solution without using a api key
async function getRssLinkFromChannelName(channelName) {
throw new Error("not implemented");
}
function getRssLinkFromChannelId(channelId) {
return `https://www.youtube.com/feeds/videos.xml?channel_id=${channelId}`
}
function getRssLinkFromUser(userName) {
return `https://www.youtube.com/feeds/videos.xml?user=${userName}`
}
function isChannelId(url) {
let lastPart = url.pathname.split('/').pop();
return lastPart.startsWith("UC") || lastPart.startsWith("HC");
}
function isChannelName(url) {
return !isChannelId(url) && (url.pathname.startsWith('/c') || url.pathname.startsWith('/channel'));
}
function isUser(url) {
return url.pathname.startsWith('/user')
}
function isValidYoutubeLink(url) {
if (url.hostname !== 'www.youtube.com')
return false;
return !(!url.pathname.startsWith('/c') && !url.pathname.startsWith('/user') && !url.pathname.startsWith('/channel'));
}