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

  + + + + + +
+