From 7893fec24273571ba0d7f3cf9950b66641bf210c Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Sat, 2 Oct 2021 13:36:55 -0500 Subject: Refactor bridges --- bridges/instagram.js | 10 +++++++++ bridges/twitter.js | 12 ++++++++++ bridges/youtube.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ cs.js | 36 +++++++++++++++++------------- instagram.js | 8 ------- manifest.json | 6 ++--- twitter.js | 10 --------- youtube.js | 60 ------------------------------------------------- 8 files changed, 108 insertions(+), 97 deletions(-) create mode 100644 bridges/instagram.js create mode 100644 bridges/twitter.js create mode 100644 bridges/youtube.js delete mode 100644 instagram.js delete mode 100644 twitter.js delete mode 100644 youtube.js diff --git a/bridges/instagram.js b/bridges/instagram.js new file mode 100644 index 0000000..0ced06a --- /dev/null +++ b/bridges/instagram.js @@ -0,0 +1,10 @@ +async function get_insta() { + let url = window.location.href; + let insta_url = url + "?__a=1" + 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) +} + diff --git a/bridges/twitter.js b/bridges/twitter.js new file mode 100644 index 0000000..e9855cb --- /dev/null +++ b/bridges/twitter.js @@ -0,0 +1,12 @@ +function get_twitter() { + let url = window.location.href; + 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 [] +} + diff --git a/bridges/youtube.js b/bridges/youtube.js new file mode 100644 index 0000000..202873b --- /dev/null +++ b/bridges/youtube.js @@ -0,0 +1,63 @@ +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){ + 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) { + 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) { + 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) { + 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 [] +} + diff --git a/cs.js b/cs.js index 2bb7b12..e57176b 100644 --- a/cs.js +++ b/cs.js @@ -15,13 +15,6 @@ let types = { '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]'); @@ -29,7 +22,8 @@ function find_links_in_page() { 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 href = links[i].getAttribute('href') + let feed_url = new URL(href, window.location.href).href let type = types[links[i].getAttribute('type')] let feed = { type: `native: ${title || type}`, @@ -59,24 +53,34 @@ function get_all_types(feed_url, note) { return feeds; } +let objs = [] + 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; let host = window.location.host; - //url = url.toLowerCase() - if (host.includes("instagram")) { - all_feed_urls = all_feed_urls.concat(await get_insta(url)) - } else if (host.includes("twitter")) { - all_feed_urls = all_feed_urls.concat(get_twitter(url)) - } else if (host.includes("youtube")) { - all_feed_urls = all_feed_urls.concat(await get_youtube(url)) + for (const obj of objs) { + for (const obj_host of obj.hosts) { + if (host.includes(obj_host)) { + result = await obj.callback() + all_feed_urls = all_feed_urls.concat(result) + break + } + } } all_feed_urls = all_feed_urls.concat(find_links_in_page()) return all_feed_urls } +function register(hosts, callback) { + objs.push({hosts, callback}) +} + +register(["instagram"], get_insta) +register(["twitter"], get_twitter) +register(["youtube"], get_youtube) + browser.runtime.onMessage.addListener(function (msg, sender) { return Promise.resolve(get_feed_urls()); }) diff --git a/instagram.js b/instagram.js deleted file mode 100644 index 56b3785..0000000 --- a/instagram.js +++ /dev/null @@ -1,8 +0,0 @@ -async function get_insta(url) { - let insta_url = url + "?__a=1" - 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) -} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 9cb0321..f8c500a 100644 --- a/manifest.json +++ b/manifest.json @@ -10,9 +10,9 @@ "https://*/*" ], "js": [ - "youtube.js", - "twitter.js", - "instagram.js", + "bridges/youtube.js", + "bridges/twitter.js", + "bridges/instagram.js", "cs.js" ] } diff --git a/twitter.js b/twitter.js deleted file mode 100644 index 1f21047..0000000 --- a/twitter.js +++ /dev/null @@ -1,10 +0,0 @@ -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 [] -} diff --git a/youtube.js b/youtube.js deleted file mode 100644 index 0f095e8..0000000 --- a/youtube.js +++ /dev/null @@ -1,60 +0,0 @@ -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(url) { - let patternVideo = /youtube.com\/watch\?v=\w+/ - let matchVideo = url.match(patternVideo) - - 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 (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){ - 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) { - 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) { - 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) { - 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 [] -} -- cgit v1.2.3