From df07b8a7f43f0f68c0125c39abf6aca37d767d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Ng=E1=BB=8Dc=20=C4=90=E1=BB=A9c=20Huy?= Date: Mon, 11 Oct 2021 10:04:25 +0700 Subject: [PATCH] Add redirect from Medium to Scribe Multiple Medium domains would be redirected to two known Scribe instances: scribe.rip scribe.nixnet.services --- src/assets/javascripts/helpers/medium.js | 26 ++++++++++ src/pages/background/background.js | 63 ++++++++++++++++++++++++ src/pages/options/options.html | 32 ++++++++++++ src/pages/options/options.js | 22 +++++++++ 4 files changed, 143 insertions(+) create mode 100644 src/assets/javascripts/helpers/medium.js diff --git a/src/assets/javascripts/helpers/medium.js b/src/assets/javascripts/helpers/medium.js new file mode 100644 index 0000000..c2a3d29 --- /dev/null +++ b/src/assets/javascripts/helpers/medium.js @@ -0,0 +1,26 @@ +const targets = [ + "medium.com", + /.*.medium.com/, + /* Other domains of medium blogs, source(s): + * https://findingtom.com/best-medium-blogs-to-follow/#1-forge + * */ + "towardsdatascience.com", + "uxdesign.cc", + "uxplanet.org", + "betterprogramming.pub", + "aninjusticemag.com", + "betterhumans.pub", + "psiloveyou.xyz", + "entrepreneurshandbook.co", + "blog.coinbase.com" +]; + +const redirects = [ + "https://scribe.rip", + "https://scribe.nixnet.services/" +]; + +export default { + targets, + redirects, +}; diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 9b8c3a1..453788c 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -5,6 +5,7 @@ import twitterHelper from "../../assets/javascripts/helpers/twitter.js"; import youtubeHelper from "../../assets/javascripts/helpers/youtube.js"; import instagramHelper from "../../assets/javascripts/helpers/instagram.js"; import mapsHelper from "../../assets/javascripts/helpers/google-maps.js"; +import mediumHelper from "../../assets/javascripts/helpers/medium.js"; import redditHelper from "../../assets/javascripts/helpers/reddit.js"; import searchHelper from "../../assets/javascripts/helpers/google-search.js"; import googleTranslateHelper from "../../assets/javascripts/helpers/google-translate.js"; @@ -13,6 +14,9 @@ import wikipediaHelper from "../../assets/javascripts/helpers/wikipedia.js"; const nitterInstances = twitterHelper.redirects; const twitterDomains = twitterHelper.targets; const youtubeDomains = youtubeHelper.targets; +const mediumDomains = mediumHelper.targets; +const scribeInstances = mediumHelper.redirects; +const scribeDefault = mediumHelper.redirects[0]; const invidiousInstances = youtubeHelper.redirects; const instagramDomains = instagramHelper.targets; const bibliogramInstances = instagramHelper.redirects; @@ -43,6 +47,7 @@ let disableInvidious; let disableBibliogram; let disableOsm; let disableReddit; +let disableScribe; let disableSearchEngine; let disableSimplyTranslate; let disableWikipedia; @@ -50,6 +55,7 @@ let nitterInstance; let invidiousInstance; let bibliogramInstance; let osmInstance; +let scribeInstance; let redditInstance; let searchEngineInstance; let simplyTranslateInstance; @@ -77,6 +83,7 @@ browser.storage.sync.get( "bibliogramInstance", "osmInstance", "redditInstance", + "scribeInstance", "searchEngineInstance", "simplyTranslateInstance", "wikipediaInstance", @@ -85,6 +92,7 @@ browser.storage.sync.get( "disableBibliogram", "disableOsm", "disableReddit", + "disableScribe", "disableSearchEngine", "disableSimplyTranslate", "disableWikipedia", @@ -108,6 +116,7 @@ browser.storage.sync.get( bibliogramInstance = result.bibliogramInstance; osmInstance = result.osmInstance || osmDefault; redditInstance = result.redditInstance || redditDefault; + scribeInstance = result.scribeInstance || scribeDefault; searchEngineInstance = result.searchEngineInstance; simplyTranslateInstance = result.simplyTranslateInstance || simplyTranslateDefault; @@ -169,6 +178,9 @@ browser.storage.onChanged.addListener((changes) => { if ("redditInstance" in changes) { redditInstance = changes.redditInstance.newValue || redditDefault; } + if ("scribeInstance" in changes) { + scribeInstance = changes.scribeInstance.newValue || scribeDefault; + } if ("searchEngineInstance" in changes) { searchEngineInstance = changes.searchEngineInstance.newValue; } @@ -515,6 +527,53 @@ function redirectReddit(url, initiator, type) { return `${redditInstance}${url.pathname}${url.search}`; } +function redirectScribe(url, initiator, type) { + if (disableScribe || isException(url, initiator)) { + return null; + } + // Do not redirect when already on the selected view + if ( + (initiator && initiator.origin === scribeInstance) || + url.origin === scribeInstance + ) { + return null; + } + // Do not redirect exclusions nor anything other than main_frame + if (type !== "main_frame") { + return null; + } + if (url.host === "i.redd.it") { + if (scribeInstance.includes("libredd")) { + return `${scribeInstance}/img${url.pathname}${url.search}`; + } else if (scribeInstance.includes("teddit")) { + // As of 2021-04-09, redirects for teddit images are nontrivial: + // - navigating to the image before ever navigating to its page causes + // 404 error (probably needs fix on teddit project) + // - some image links on teddit are very different + // Therefore, don't support redirecting image links for teddit. + return null; + } else { + return null; + } + } else if (url.host === "redd.it") { + if ( + scribeInstance.includes("teddit") && + !url.pathname.match(/^\/+[^\/]+\/+[^\/]/) + ) { + // As of 2021-04-22, redirects for teddit redd.it/foo links don't work. + // It appears that adding "/comments" as a prefix works, so manually add + // that prefix if it is missing. Even though redd.it/comments/foo links + // don't seem to work or exist, guard against affecting those kinds of + // paths. + // + // Note the difference between redd.it/comments/foo (doesn't work) and + // teddit.net/comments/foo (works). + return `${scribeInstance}/comments${url.pathname}${url.search}`; + } + } + return `${scribeInstance}${url.pathname}${url.search}`; +} + function redirectSearchEngine(url, initiator) { if (disableSearchEngine || isException(url, initiator)) { return null; @@ -607,6 +666,10 @@ browser.webRequest.onBeforeRequest.addListener( redirect = { redirectUrl: redirectReddit(url, initiator, details.type), }; + } else if (mediumDomains.includes(url.host)) { + redirect = { + redirectUrl: redirectScribe(url, initiator, details.type), + }; } else if (url.href.match(googleSearchRegex)) { redirect = { redirectUrl: redirectSearchEngine(url, initiator), diff --git a/src/pages/options/options.html b/src/pages/options/options.html index 65f1e9a..43db929 100644 --- a/src/pages/options/options.html +++ b/src/pages/options/options.html @@ -141,6 +141,28 @@ + +
+ + + + + + + +
+

+ Scribe Redirects +

+
+   + +
@@ -256,6 +278,16 @@ /> +
+

Scribe Instance

+
+ +
+

Search Engine Instance

diff --git a/src/pages/options/options.js b/src/pages/options/options.js index 91e7ac0..938abd2 100644 --- a/src/pages/options/options.js +++ b/src/pages/options/options.js @@ -6,6 +6,7 @@ import youtubeHelper from "../../assets/javascripts/helpers/youtube.js"; import instagramHelper from "../../assets/javascripts/helpers/instagram.js"; import mapsHelper from "../../assets/javascripts/helpers/google-maps.js"; import redditHelper from "../../assets/javascripts/helpers/reddit.js"; +import mediumHelper from "../../assets/javascripts/helpers/medium.js"; import searchHelper from "../../assets/javascripts/helpers/google-search.js"; import googleTranslateHelper from "../../assets/javascripts/helpers/google-translate.js"; import wikipediaHelper from "../../assets/javascripts/helpers/wikipedia.js"; @@ -15,6 +16,7 @@ const invidiousInstances = youtubeHelper.redirects; const bibliogramInstances = instagramHelper.redirects; const osmInstances = mapsHelper.redirects; const redditInstances = redditHelper.redirects; +const scribeInstances = mediumHelper.redirects; const searchEngineInstances = searchHelper.redirects; const simplyTranslateInstances = googleTranslateHelper.redirects; const wikipediaInstances = wikipediaHelper.redirects; @@ -24,6 +26,7 @@ const autocompletes = [ { id: "bibliogram-instance", instances: bibliogramInstances }, { id: "osm-instance", instances: osmInstances }, { id: "reddit-instance", instances: redditInstances }, + { id: "scribe-instance", instances: scribeInstances }, { id: "search-engine-instance", instances: searchEngineInstances.map((instance) => instance.link), @@ -38,6 +41,7 @@ let invidiousInstance = document.getElementById("invidious-instance"); let bibliogramInstance = document.getElementById("bibliogram-instance"); let osmInstance = document.getElementById("osm-instance"); let redditInstance = document.getElementById("reddit-instance"); +let scribeInstance = document.getElementById("scribe-instance"); let searchEngineInstance = document.getElementById("search-engine-instance"); let simplyTranslateInstance = document.getElementById( "simply-translate-instance" @@ -48,6 +52,7 @@ let disableInvidious = document.getElementById("disable-invidious"); let disableBibliogram = document.getElementById("disable-bibliogram"); let disableOsm = document.getElementById("disable-osm"); let disableReddit = document.getElementById("disable-reddit"); +let disableScribe = document.getElementById("disable-scribe"); let disableSearchEngine = document.getElementById("disable-search-engine"); let disableSimplyTranslate = document.getElementById( "disable-simply-translate" @@ -103,6 +108,7 @@ browser.storage.sync.get( "bibliogramInstance", "osmInstance", "redditInstance", + "scribeInstance", "searchEngineInstance", "simplyTranslateInstance", "wikipediaInstance", @@ -111,6 +117,7 @@ browser.storage.sync.get( "disableBibliogram", "disableOsm", "disableReddit", + "disableScribe", "disableSearchEngine", "disableSimplyTranslate", "disableWikipedia", @@ -139,6 +146,7 @@ browser.storage.sync.get( bibliogramInstance.value = result.bibliogramInstance || ""; osmInstance.value = result.osmInstance || ""; redditInstance.value = result.redditInstance || ""; + scribeInstance.value = result.scribeInstance || ""; searchEngineInstance.value = (result.searchEngineInstance && result.searchEngineInstance.link) || ""; simplyTranslateInstance.value = result.simplyTranslateInstance || ""; @@ -148,6 +156,7 @@ browser.storage.sync.get( disableBibliogram.checked = !result.disableBibliogram; disableOsm.checked = !result.disableOsm; disableReddit.checked = !result.disableReddit; + disableScribe.checked = !result.disableScribe; disableSearchEngine.checked = !result.disableSearchEngine; disableSimplyTranslate.checked = !result.disableSimplyTranslate; disableWikipedia.checked = !result.disableWikipedia; @@ -311,6 +320,15 @@ const redditInstanceChange = debounce(() => { }, 500); redditInstance.addEventListener("input", redditInstanceChange); +const scribeInstanceChange = debounce(() => { + if (scribeInstance.checkValidity()) { + browser.storage.sync.set({ + scribeInstance: parseURL(scribeInstance.value), + }); + } +}, 500); +scribeInstance.addEventListener("input", scribeInstanceChange); + const searchEngineInstanceChange = debounce(() => { const instance = searchEngineInstances.find( (instance) => instance.link === searchEngineInstance.value @@ -369,6 +387,10 @@ disableReddit.addEventListener("change", (event) => { browser.storage.sync.set({ disableReddit: !event.target.checked }); }); +disableScribe.addEventListener("change", (event) => { + browser.storage.sync.set({ disableScribe: !event.target.checked }); +}); + disableSearchEngine.addEventListener("change", (event) => { browser.storage.sync.set({ disableSearchEngine: !event.target.checked }); });