aboutsummaryrefslogtreecommitdiff
path: root/cs.js
blob: b6b35c67baad7abc09244d39567b694c67604cac (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
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
var base_url = undefined
let formats = ["Atom", "Mrss", "Html"]
let types = {
    'application/rss+xml': "rss",
    'application/atom+xml': "atom",
    'application/rdf+xml': "rdf",
    'application/rss': "rss",
    'application/atom': "atom",
    'application/rdf': "rdf",
    'text/rss+xml': "rss",
    'text/atom+xml': "atom",
    'text/rdf+xml': "rdf",
    'text/rss': "rss",
    'text/atom': "atom",
    'text/rdf': "rdf"
}

function normalize_feed_url(feed_url) {
    if (feed_url.startsWith('/')) {
        let url = window.location.href
        feed_url = url.split('/')[0] + '//' + url.split('/')[2] + feed_url;
    }
    return feed_url
}

function find_links_in_page() {
    let links = document.querySelectorAll('link[type]');
    let feeds = [];
    for (let i = 0; i < links.length; i++) {
        if (links[i].hasAttribute('type') && links[i].getAttribute('type') in types) {
            let title = links[i].getAttribute('title')
            let feed_url = normalize_feed_url(links[i].getAttribute('href'));
            let type = types[links[i].getAttribute('type')]
            let feed = {
                type: `native: ${title || type}`,
                url: feed_url,
            };
            feeds.push(feed);
        }
    }
    return feeds
}

function get_all_types(feed_url) {
    let feeds = [];
    formats.forEach(el => {
        feeds.push({
            type: `rss-bridge: ${el}`,
            url: feed_url + el
        });
    })
    return feeds;
}

async function get_insta(url) {
    let insta_url = url + "?__a=1"
    console.log("fetching")
    let res = await fetch(insta_url);
    let json = await res.json();
    let uid = json.graphql.user.id
    let feed_url = `${base_url}/?action=display&bridge=Instagram&context=Username&u=${uid}&media_type=all&format=`;
    return get_all_types(feed_url)
}

function get_twitter(url) {
    let pattern = /twitter.com\/(\w+).*/
    let match = url.match(pattern);
    if (match) {
        let twitter_handle = match[1]
        let feed_url = `${base_url}/?action=display&bridge=Twitter&context=By+username&u=${twitter_handle}&format=`
        return get_all_types(feed_url)
    }
    return []
}

async function get_youtube(url) {
    // use link to channel if given a video url
    let patternVideo = /youtube.com\/watch\?v=\w+/
    if (url.match(patternVideo)) {
        url = document.querySelector("#channel-name a")["href"]
    }

    let patternUser = /youtube.com\/user\/(\w+).*/
    let matchUser = url.match(patternUser)

    let patternChannel = /youtube.com\/channel\/(\w+).*/
    let matchChannel = url.match(patternChannel)

    let patternPlaylist = /youtube.com\/playlist\?list=(\w+)/
    let matchPlaylist = url.match(patternPlaylist)

    
    if (matchUser) {
        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)
    } else if (matchChannel) {
        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)
    } else if (matchPlaylist) {
        let playlistId = matchPlaylist[1]
        let native_url = `https://www.youtube.com/feeds/videos.xml?playlist_id=${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 []
}

async function get_feed_urls() {
    let settings = await browser.storage.sync.get("rb");
    base_url = settings.rb;
    let all_feed_urls = []
    let url = window.location.href;
    //url = url.toLowerCase()
    if (url.includes("instagram")) {
        all_feed_urls = all_feed_urls.concat(await get_insta(url))
    } else if (url.includes("twitter")) {
        all_feed_urls = all_feed_urls.concat(get_twitter(url))
    } else if (url.includes("youtube")) {
        all_feed_urls = all_feed_urls.concat(await get_youtube(url))
    }
    all_feed_urls = all_feed_urls.concat(find_links_in_page())
    return all_feed_urls
}

browser.runtime.onMessage.addListener(function (msg, sender) {
    return Promise.resolve(get_feed_urls());
})