From df07b8a7f43f0f68c0125c39abf6aca37d767d54 Mon Sep 17 00:00:00 2001 From: Ngô Ngọc Đức Huy Date: Mon, 11 Oct 2021 10:04:25 +0700 Subject: Add redirect from Medium to Scribe Multiple Medium domains would be redirected to two known Scribe instances: scribe.rip scribe.nixnet.services --- src/pages/background/background.js | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'src/pages/background') diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 9b8c3a13..453788c8 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), -- cgit 1.4.1 From 362bef86586f04f615b97fdb740424e6568002e0 Mon Sep 17 00:00:00 2001 From: Ngô Ngọc Đức Huy Date: Tue, 12 Oct 2021 15:24:16 +0700 Subject: Fix copy-and-replace mistakes - Change scribe's default input to 'Random instance (none selected)' - Add random pool variables and functions for scribe --- src/pages/background/background.js | 71 ++++++++++++++++++-------------------- src/pages/options/options.html | 2 +- src/pages/options/options.js | 13 ++++++- 3 files changed, 46 insertions(+), 40 deletions(-) (limited to 'src/pages/background') diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 453788c8..d7e37591 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -72,6 +72,7 @@ let useFreeTube; let nitterRandomPool; let invidiousRandomPool; let bibliogramRandomPool; +let scribeRandomPool; let exceptions; window.browser = window.browser || window.chrome; @@ -108,6 +109,7 @@ browser.storage.sync.get( "nitterRandomPool", "invidiousRandomPool", "bibliogramRandomPool", + "scribeRandomPool", "exceptions", ], (result) => { @@ -152,6 +154,9 @@ browser.storage.sync.get( bibliogramRandomPool = result.bibliogramRandomPool ? result.bibliogramRandomPool.split(",") : commonHelper.filterInstances(bibliogramInstances); + scribeRandomPool = result.scribeRandomPool + ? result.scribeRandomPool.split(",") + : commonHelper.filterInstances(bibliogramInstances); } ); @@ -244,6 +249,9 @@ browser.storage.onChanged.addListener((changes) => { if ("bibliogramRandomPool" in changes) { bibliogramRandomPool = changes.bibliogramRandomPool.newValue.split(","); } + if ("scribeRandomPool" in changes) { + scribeRandomPool = changes.scribeRandomPool.newValue.split(","); + } if ("exceptions" in changes) { exceptions = changes.exceptions.newValue.map((e) => { return new RegExp(e); @@ -527,51 +535,38 @@ function redirectReddit(url, initiator, type) { return `${redditInstance}${url.pathname}${url.search}`; } -function redirectScribe(url, initiator, type) { +function redirectMedium(url, initiator) { if (disableScribe || isException(url, initiator)) { return null; } - // Do not redirect when already on the selected view - if ( - (initiator && initiator.origin === scribeInstance) || - url.origin === scribeInstance - ) { + if (url.pathname.split("/").includes("home")) { return null; } - // Do not redirect exclusions nor anything other than main_frame - if (type !== "main_frame") { + if ( + isFirefox() && + initiator && + (initiator.origin === scribeInstance || + scribeInstances.includes(initiator.origin) || + mediumDomains.includes(initiator.host)) + ) { + browser.storage.sync.set({ + redirectBypassFlag: true, + }); 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}`; - } + if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") { + return `${ + scribeInstance || commonHelper.getRandomInstance(scribeRandomPool) + }/pic/${encodeURIComponent(url.href)}`; + } else if (url.pathname.split("/").includes("tweets")) { + return `${ + scribeInstance || commonHelper.getRandomInstance(scribeRandomPool) + }${url.pathname.replace("/tweets", "")}${url.search}`; + } else { + return `${ + scribeInstance || commonHelper.getRandomInstance(scribeRandomPool) + }${url.pathname}${url.search}`; } - return `${scribeInstance}${url.pathname}${url.search}`; } function redirectSearchEngine(url, initiator) { @@ -668,7 +663,7 @@ browser.webRequest.onBeforeRequest.addListener( }; } else if (mediumDomains.includes(url.host)) { redirect = { - redirectUrl: redirectScribe(url, initiator, details.type), + redirectUrl: redirectMedium(url, initiator, details.type), }; } else if (url.href.match(googleSearchRegex)) { redirect = { diff --git a/src/pages/options/options.html b/src/pages/options/options.html index 43db929d..bafff5f8 100644 --- a/src/pages/options/options.html +++ b/src/pages/options/options.html @@ -284,7 +284,7 @@ diff --git a/src/pages/options/options.js b/src/pages/options/options.js index 938abd24..71bdc99e 100644 --- a/src/pages/options/options.js +++ b/src/pages/options/options.js @@ -73,6 +73,7 @@ let useFreeTube = document.getElementById("use-freetube"); let nitterRandomPool = document.getElementById("nitter-random-pool"); let invidiousRandomPool = document.getElementById("invidious-random-pool"); let bibliogramRandomPool = document.getElementById("bibliogram-random-pool"); +let scribeRandomPool = document.getElementById("scribe-random-pool"); let exceptions; window.browser = window.browser || window.chrome; @@ -137,6 +138,7 @@ browser.storage.sync.get( "nitterRandomPool", "invidiousRandomPool", "bibliogramRandomPool", + "scribeRandomPool", ], (result) => { theme.value = result.theme || ""; @@ -184,6 +186,9 @@ browser.storage.sync.get( bibliogramRandomPool.value = result.bibliogramRandomPool || commonHelper.filterInstances(bibliogramInstances); + scribeRandomPool.value = + result.scribeRandomPool || + commonHelper.filterInstances(scribeInstances); } ); @@ -474,7 +479,13 @@ const bibliogramRandomPoolChange = debounce(() => { bibliogramRandomPool: bibliogramRandomPool.value, }); }, 500); -bibliogramRandomPool.addEventListener("input", bibliogramRandomPoolChange); + +const scribeRandomPoolChange = debounce(() => { + browser.storage.sync.set({ + scribeRandomPool: scribeRandomPool.value, + }); +}, 500); +scribeRandomPool.addEventListener("input", scribeRandomPoolChange); theme.addEventListener("change", (event) => { const value = event.target.options[theme.selectedIndex].value; -- cgit 1.4.1 From 4dc7fb4cce36794669ab529a67e644459f151c73 Mon Sep 17 00:00:00 2001 From: Ngô Ngọc Đức Huy Date: Sun, 17 Oct 2021 19:30:20 +0700 Subject: Fix copying mistake --- src/pages/background/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/pages/background') diff --git a/src/pages/background/background.js b/src/pages/background/background.js index d7e37591..7e2ecb6f 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -156,7 +156,7 @@ browser.storage.sync.get( : commonHelper.filterInstances(bibliogramInstances); scribeRandomPool = result.scribeRandomPool ? result.scribeRandomPool.split(",") - : commonHelper.filterInstances(bibliogramInstances); + : commonHelper.filterInstances(scribeInstances); } ); -- cgit 1.4.1 From 263626d7d1225c7294ade084fee4ff30f036af2d Mon Sep 17 00:00:00 2001 From: Ngô Ngọc Đức Huy Date: Fri, 29 Oct 2021 20:35:33 +0700 Subject: Remove default scribe instance --- src/pages/background/background.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'src/pages/background') diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 7e2ecb6f..3a8103df 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -118,12 +118,13 @@ browser.storage.sync.get( bibliogramInstance = result.bibliogramInstance; osmInstance = result.osmInstance || osmDefault; redditInstance = result.redditInstance || redditDefault; - scribeInstance = result.scribeInstance || scribeDefault; + scribeInstance = result.scribeInstance; searchEngineInstance = result.searchEngineInstance; simplyTranslateInstance = result.simplyTranslateInstance || simplyTranslateDefault; wikipediaInstance = result.wikipediaInstance || wikipediaDefault; disableNitter = result.disableNitter; + disableScribe = result.disableScribe; disableInvidious = result.disableInvidious; disableBibliogram = result.disableBibliogram; disableOsm = result.disableOsm; @@ -554,19 +555,9 @@ function redirectMedium(url, initiator) { }); return null; } - if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") { - return `${ - scribeInstance || commonHelper.getRandomInstance(scribeRandomPool) - }/pic/${encodeURIComponent(url.href)}`; - } else if (url.pathname.split("/").includes("tweets")) { - return `${ - scribeInstance || commonHelper.getRandomInstance(scribeRandomPool) - }${url.pathname.replace("/tweets", "")}${url.search}`; - } else { - return `${ + return `${ scribeInstance || commonHelper.getRandomInstance(scribeRandomPool) }${url.pathname}${url.search}`; - } } function redirectSearchEngine(url, initiator) { -- cgit 1.4.1 From b0da859852cbe60a91d1db2e866abbbe35f404cc Mon Sep 17 00:00:00 2001 From: Ngô Ngọc Đức Huy Date: Fri, 29 Oct 2021 20:55:16 +0700 Subject: Detect setting change for scribe redirect --- src/pages/background/background.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/pages/background') diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 3a8103df..741bf6a1 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -193,6 +193,9 @@ browser.storage.onChanged.addListener((changes) => { if ("disableNitter" in changes) { disableNitter = changes.disableNitter.newValue; } + if ("disableScribe" in changes) { + disableScribe = changes.disableScribe.newValue; + } if ("disableInvidious" in changes) { disableInvidious = changes.disableInvidious.newValue; } @@ -540,9 +543,6 @@ function redirectMedium(url, initiator) { if (disableScribe || isException(url, initiator)) { return null; } - if (url.pathname.split("/").includes("home")) { - return null; - } if ( isFirefox() && initiator && @@ -654,7 +654,7 @@ browser.webRequest.onBeforeRequest.addListener( }; } else if (mediumDomains.includes(url.host)) { redirect = { - redirectUrl: redirectMedium(url, initiator, details.type), + redirectUrl: redirectMedium(url, initiator), }; } else if (url.href.match(googleSearchRegex)) { redirect = { -- cgit 1.4.1