diff --git a/background.js b/background.js index 3f5145c..185147b 100644 --- a/background.js +++ b/background.js @@ -84,6 +84,18 @@ const bibliogramInstances = [ "https://bibliogram.ggc-project.de", ]; const osmDefault = "https://openstreetmap.org"; +const redditDomains = [ + "www.reddit.com", + "np.reddit.com", + "new.reddit.com", + "amp.reddit.com" +]; +const redditBypassPaths = /\/(gallery\/poll\/rpan\/settings\/topics)/; +const redditVersions = [ + "https://old.reddit.com", + "https://i.reddit.com" // Old Mobile view +]; +const redditDefault = redditVersions[0]; const googleMapsRegex = /https?:\/\/(((www|maps)\.)?(google\.).*(\/maps)|maps\.(google\.).*)/; const mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/; const dataLatLngRegex = /(!3d|!4d)(-?[0-9]{1,10}.[0-9]{1,10})/g; @@ -105,10 +117,12 @@ let disableNitter; let disableInvidious; let disableBibliogram; let disableOsm; +let disableRedditVersion; let nitterInstance; let invidiousInstance; let bibliogramInstance; let osmInstance; +let redditVersion; let alwaysProxy; let onlyEmbeddedVideo; let videoQuality; @@ -127,10 +141,12 @@ browser.storage.sync.get( "invidiousInstance", "bibliogramInstance", "osmInstance", + "redditVersion", "disableNitter", "disableInvidious", "disableBibliogram", "disableOsm", + "disableRedditVersion", "alwaysProxy", "onlyEmbeddedVideo", "videoQuality", @@ -146,10 +162,12 @@ browser.storage.sync.get( disableInvidious = result.disableInvidious; disableBibliogram = result.disableBibliogram; disableOsm = result.disableOsm; + disableRedditVersion = result.disableRedditVersion; nitterInstance = result.nitterInstance; invidiousInstance = result.invidiousInstance; bibliogramInstance = result.bibliogramInstance; osmInstance = result.osmInstance || osmDefault; + redditVersion = result.redditVersion || redditDefault; alwaysProxy = result.alwaysProxy; onlyEmbeddedVideo = result.onlyEmbeddedVideo; videoQuality = result.videoQuality; @@ -179,6 +197,9 @@ browser.storage.onChanged.addListener((changes) => { if ("osmInstance" in changes) { osmInstance = changes.osmInstance.newValue || osmDefault; } + if ("redditVersion" in changes) { + redditVersion = changes.redditVersion.newValue || redditDefault; + } if ("disableNitter" in changes) { disableNitter = changes.disableNitter.newValue; } @@ -191,6 +212,9 @@ browser.storage.onChanged.addListener((changes) => { if ("disableOsm" in changes) { disableOsm = changes.disableOsm.newValue; } + if ("disableRedditVersion" in changes) { + disableRedditVersion = changes.disableRedditVersion.newValue; + } if ("alwaysProxy" in changes) { alwaysProxy = changes.alwaysProxy.newValue; } @@ -473,6 +497,26 @@ function redirectGoogleMaps(url, initiator) { return redirect; } +function redirectReddit(url, initiator, type) { + if (disableRedditVersion || isException(url, initiator)) { + return null; + } + // Do not redirect old or mobile Reddit versions on normal Reddit links + if ( + initiator && + (initiator.origin === redditVersion || + redditVersions.includes(initiator.origin) || + redditVersions.includes(initiator.host)) + ) { + return null; + } + // Do not redirect anything other than main_frame + if (type !== "main_frame" || url.pathname.match(redditBypassPaths)) { + return null; + } + return `${redditVersion}${url.pathname}${url.search}`; +} + browser.webRequest.onBeforeRequest.addListener( (details) => { const url = new URL(details.url); @@ -499,6 +543,10 @@ browser.webRequest.onBeforeRequest.addListener( redirect = { redirectUrl: redirectGoogleMaps(url, initiator), }; + } else if (redditDomains.includes(url.host)) { + redirect = { + redirectUrl: redirectReddit(url, initiator, details.type), + } } if (redirect && redirect.redirectUrl) { console.info( diff --git a/pages/options/options.html b/pages/options/options.html index 6641e5c..9664151 100644 --- a/pages/options/options.html +++ b/pages/options/options.html @@ -120,6 +120,28 @@ + + + + + + + Reddit Version Redirects + + + + + + + + + + Nitter Instance @@ -161,6 +183,16 @@ /> + + Reddit Version + + + + Theme diff --git a/pages/options/options.js b/pages/options/options.js index 8149be7..ff08fa4 100644 --- a/pages/options/options.js +++ b/pages/options/options.js @@ -45,21 +45,28 @@ const bibliogramInstances = [ "https://bibliogram.ggc-project.de", ]; const osmInstances = ["https://openstreetmap.org"]; +const redditVersions = [ + "https://old.reddit.com", + "https://i.reddit.com" +]; const autocompletes = [ { id: "nitter-instance", instances: nitterInstances }, { id: "invidious-instance", instances: invidiousInstances }, { id: "bibliogram-instance", instances: bibliogramInstances }, { id: "osm-instance", instances: osmInstances }, + { id: "reddit-version", instances: redditVersions }, ]; let nitterInstance = document.getElementById("nitter-instance"); let invidiousInstance = document.getElementById("invidious-instance"); let bibliogramInstance = document.getElementById("bibliogram-instance"); let osmInstance = document.getElementById("osm-instance"); +let redditVersion = document.getElementById("reddit-version"); let disableNitter = document.getElementById("disable-nitter"); let disableInvidious = document.getElementById("disable-invidious"); let disableBibliogram = document.getElementById("disable-bibliogram"); let disableOsm = document.getElementById("disable-osm"); +let disableRedditVersion = document.getElementById("disable-reddit-version"); let alwaysProxy = document.getElementById("always-proxy"); let onlyEmbeddedVideo = document.getElementById("only-embed"); let videoQuality = document.getElementById("video-quality"); @@ -103,10 +110,12 @@ browser.storage.sync.get( "invidiousInstance", "bibliogramInstance", "osmInstance", + "redditVersion", "disableNitter", "disableInvidious", "disableBibliogram", "disableOsm", + "disableRedditVersion", "alwaysProxy", "onlyEmbeddedVideo", "videoQuality", @@ -127,10 +136,12 @@ browser.storage.sync.get( invidiousInstance.value = result.invidiousInstance || ""; bibliogramInstance.value = result.bibliogramInstance || ""; osmInstance.value = result.osmInstance || ""; + redditVersion.value = result.redditVersion || ""; disableNitter.checked = !result.disableNitter; disableInvidious.checked = !result.disableInvidious; disableBibliogram.checked = !result.disableBibliogram; disableOsm.checked = !result.disableOsm; + disableRedditVersion.checked = !result.disableRedditVersion; alwaysProxy.checked = result.alwaysProxy; onlyEmbeddedVideo.checked = result.onlyEmbeddedVideo; videoQuality.value = result.videoQuality || ""; @@ -273,6 +284,15 @@ let osmInstanceChange = debounce(() => { }, 500); osmInstance.addEventListener("input", osmInstanceChange); +let redditVersionChange = debounce(() => { + if (redditVersion.checkValidity()) { + browser.storage.sync.set({ + redditVersion: parseURL(redditVersion.value), + }); + } +}, 500); +redditVersion.addEventListener("input", redditVersionChange); + disableNitter.addEventListener("change", (event) => { browser.storage.sync.set({ disableNitter: !event.target.checked }); }); @@ -289,6 +309,10 @@ disableOsm.addEventListener("change", (event) => { browser.storage.sync.set({ disableOsm: !event.target.checked }); }); +disableRedditVersion.addEventListener("change", (event) => { + browser.storage.sync.set({ disableRedditVersion: !event.target.checked }); +}); + alwaysProxy.addEventListener("change", (event) => { browser.storage.sync.set({ alwaysProxy: event.target.checked }); }); diff --git a/pages/popup/popup.html b/pages/popup/popup.html index e06159a..871e2c6 100644 --- a/pages/popup/popup.html +++ b/pages/popup/popup.html @@ -116,6 +116,29 @@ + + + + + + + Reddit Version Redirects + + + + + + + + + + +