aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Powers <mark@marks.kitchen>2021-10-02 13:36:55 -0500
committerMark Powers <mark@marks.kitchen>2021-10-02 13:36:55 -0500
commit7893fec24273571ba0d7f3cf9950b66641bf210c (patch)
treec78df61028d814e2bbb437b90ac393989f0208ec
parenta1fbc4ac46e1c41d8c4522ae2b7af402ba0515f7 (diff)
Refactor bridges
-rw-r--r--bridges/instagram.js (renamed from instagram.js)6
-rw-r--r--bridges/twitter.js (renamed from twitter.js)4
-rw-r--r--bridges/youtube.js (renamed from youtube.js)19
-rw-r--r--cs.js36
-rw-r--r--manifest.json6
5 files changed, 41 insertions, 30 deletions
diff --git a/instagram.js b/bridges/instagram.js
index 56b3785..0ced06a 100644
--- a/instagram.js
+++ b/bridges/instagram.js
@@ -1,8 +1,10 @@
-async function get_insta(url) {
+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)
-} \ No newline at end of file
+}
+
diff --git a/twitter.js b/bridges/twitter.js
index 1f21047..e9855cb 100644
--- a/twitter.js
+++ b/bridges/twitter.js
@@ -1,4 +1,5 @@
-function get_twitter(url) {
+function get_twitter() {
+ let url = window.location.href;
let pattern = /twitter.com\/(\w+).*/
let match = url.match(pattern);
if (match) {
@@ -8,3 +9,4 @@ function get_twitter(url) {
}
return []
}
+
diff --git a/youtube.js b/bridges/youtube.js
index 0f095e8..202873b 100644
--- a/youtube.js
+++ b/bridges/youtube.js
@@ -6,17 +6,19 @@ 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+/
+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\/(\w+).*/
+
+ let patternUser = /youtube.com\/user\/[A-z0-9-_]+.*/
let matchUser = url.match(patternUser)
- let patternChannel = /youtube.com\/channel\/(\w+).*/
+ let patternChannel = /youtube.com\/channel\/[A-z0-9-_]+.*/
let matchChannel = url.match(patternChannel)
- let patternPlaylist = /youtube.com\/playlist\?list=(\w+)/
+ let patternPlaylist = /youtube.com\/playlist\?list=[A-z0-9-_]+.*/
let matchPlaylist = url.match(patternPlaylist)
if (matchVideo) {
@@ -48,13 +50,14 @@ async function get_youtube(url) {
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/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"
]
}