aboutsummaryrefslogtreecommitdiff
path: root/content_scripts/bridges/youtube.js
blob: e6b584c3e869d98ac3d4f6db7f215fef9eca98a8 (plain)
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
function get_native_playlist_feed(playlistId){
    return `https://www.youtube.com/feeds/videos.xml?playlist_id=${playlistId}`
}

function get_native_channel_feed(channelId){
    return `https://www.youtube.com/feeds/videos.xml?channel_id=${channelId}`
}

async function get_youtube() {
    let url = window.location.href;

    let patternVideo = /youtube.com\/watch\?v=[A-z0-9-_]+/
    let matchVideo = url.match(patternVideo)

    let patternUser = /youtube.com\/user\/[A-z0-9-_]+.*/
    let matchUser = url.match(patternUser)

    let patternChannel = /youtube.com\/channel\/[A-z0-9-_]+.*/
    let matchChannel = url.match(patternChannel)

    let patternPlaylist = /youtube.com\/playlist\?list=[A-z0-9-_]+.*/
    let matchPlaylist = url.match(patternPlaylist)

    if (matchVideo) {
        let channel_url = document.querySelector("[role='main'] #channel-name a")["href"]
        let matchChannel2 = channel_url.match(patternChannel)
        let channelId = matchChannel2[1]
        let feed_url = `${base_url}/?action=display&bridge=Youtube&context=By+channel+id&c=${channelId}&duration_min=&duration_max=&format=`
        let native_url = get_native_channel_feed(channelId)
        let feeds = get_all_types(feed_url, "channel").concat([{ type: `native (channel): Rss`, url: native_url }])

        let patternListInVideo = /youtube.com\/watch\?v=\w+\&list=(\w+)/
        let matchListInVideo = url.match(patternListInVideo)
        if(matchListInVideo && matchListInVideo[1]){
            let playlistId = matchListInVideo[1]
            let playlist_feed_url = `${base_url}/?action=display&bridge=Youtube&context=By+playlist+Id&p=${playlistId}&duration_min=&duration_max=&format=`
            let native_url = get_native_playlist_feed(playlistId)
            let rb_feeds = get_all_types(playlist_feed_url, "playlist")
            feeds = feeds.concat(rb_feeds.concat([{ type: `native (playlist): Rss`, url: native_url }]))
        }

        return feeds
    }
    if (matchUser && matchUser[1]) {
        let user = matchUser[1];
        let feed_url = `${base_url}/?action=display&bridge=Youtube&context=By+username&u=${user}&duration_min=&duration_max=&format=`
        return get_all_types(feed_url)
    }
    if (matchChannel && matchChannel[1]) {
        let channelId = matchChannel[1]
        let feed_url = `${base_url}/?action=display&bridge=Youtube&context=By+channel+id&c=${channelId}&duration_min=&duration_max=&format=`
        return get_all_types(feed_url)
    }
    if (matchPlaylist && matchPlaylist[1]) {
        let playlistId = matchPlaylist[1]
        let native_url = get_native_playlist_feed(playlistId)
        let feed_url = `${base_url}/?action=display&bridge=Youtube&context=By+playlist+Id&p=${playlistId}&duration_min=&duration_max=&format=`
        let rb_feeds = get_all_types(feed_url)
        return rb_feeds.concat([{ type: `native: Rss`, url: native_url }])
    }
    return []
}

register(["youtube"], get_youtube)