about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSimon Brazell <simon@brazell.com.au>2020-11-05 08:21:35 +1100
committerGitHub <noreply@github.com>2020-11-05 08:21:35 +1100
commitbbd648a2e41dd2a0323676545e38ab1f6871bebb (patch)
treebf88def64b35beaec301f1c3b88b00e1331df35f
parentMerge pull request #118 from B0pol/patch-1 (diff)
parentUpdate comment for clarity (diff)
downloadlibredirect-bbd648a2e41dd2a0323676545e38ab1f6871bebb.zip
Merge pull request #102 from nitrohorse/add-old-reddit-support
feat(): Add support for redirecting to the old Reddit desktop or mobile design
-rw-r--r--background.js43
-rw-r--r--pages/options/options.html32
-rw-r--r--pages/options/options.js24
-rw-r--r--pages/popup/popup.html23
-rw-r--r--pages/popup/popup.js7
5 files changed, 129 insertions, 0 deletions
diff --git a/background.js b/background.js
index dc04ffe6..ad9d3c45 100644
--- a/background.js
+++ b/background.js
@@ -87,6 +87,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 oldRedditViews = [
+  "https://old.reddit.com", // desktop
+  "https://i.reddit.com" // mobile
+];
+const oldRedditDefaultView = oldRedditViews[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;
@@ -108,10 +120,12 @@ let disableNitter;
 let disableInvidious;
 let disableBibliogram;
 let disableOsm;
+let disableOldReddit;
 let nitterInstance;
 let invidiousInstance;
 let bibliogramInstance;
 let osmInstance;
+let oldRedditView;
 let alwaysProxy;
 let onlyEmbeddedVideo;
 let videoQuality;
@@ -131,10 +145,12 @@ browser.storage.sync.get(
     "invidiousInstance",
     "bibliogramInstance",
     "osmInstance",
+    "oldRedditView",
     "disableNitter",
     "disableInvidious",
     "disableBibliogram",
     "disableOsm",
+    "disableOldReddit",
     "alwaysProxy",
     "onlyEmbeddedVideo",
     "videoQuality",
@@ -151,10 +167,12 @@ browser.storage.sync.get(
     disableInvidious = result.disableInvidious;
     disableBibliogram = result.disableBibliogram;
     disableOsm = result.disableOsm;
+    disableOldReddit = result.disableOldReddit;
     nitterInstance = result.nitterInstance;
     invidiousInstance = result.invidiousInstance;
     bibliogramInstance = result.bibliogramInstance;
     osmInstance = result.osmInstance || osmDefault;
+    oldRedditView = result.oldRedditView || oldRedditDefaultView;
     alwaysProxy = result.alwaysProxy;
     onlyEmbeddedVideo = result.onlyEmbeddedVideo;
     videoQuality = result.videoQuality;
@@ -185,6 +203,9 @@ browser.storage.onChanged.addListener((changes) => {
   if ("osmInstance" in changes) {
     osmInstance = changes.osmInstance.newValue || osmDefault;
   }
+  if ("oldRedditView" in changes) {
+    oldRedditView = changes.oldRedditView.newValue || oldRedditDefaultView;
+  }
   if ("disableNitter" in changes) {
     disableNitter = changes.disableNitter.newValue;
   }
@@ -197,6 +218,9 @@ browser.storage.onChanged.addListener((changes) => {
   if ("disableOsm" in changes) {
     disableOsm = changes.disableOsm.newValue;
   }
+  if ("disableOldReddit" in changes) {
+    disableOldReddit = changes.disableOldReddit.newValue;
+  }
   if ("alwaysProxy" in changes) {
     alwaysProxy = changes.alwaysProxy.newValue;
   }
@@ -485,6 +509,21 @@ function redirectGoogleMaps(url, initiator) {
   return redirect;
 }
 
+function redirectReddit(url, initiator, type) {
+  if (disableOldReddit || isException(url, initiator)) {
+    return null;
+  }
+  // Do not redirect when already on the selected view
+  if (initiator && initiator.origin === oldRedditView || url.origin === oldRedditView) {
+    return null;
+  }
+  // Do not redirect exclusions nor anything other than main_frame
+  if (type !== "main_frame" || url.pathname.match(redditBypassPaths)) {
+    return null;
+  }
+  return `${oldRedditView}${url.pathname}${url.search}`;
+}
+
 browser.webRequest.onBeforeRequest.addListener(
   (details) => {
     const url = new URL(details.url);
@@ -511,6 +550,10 @@ browser.webRequest.onBeforeRequest.addListener(
       redirect = {
         redirectUrl: redirectGoogleMaps(url, initiator),
       };
+    } else if (redditDomains.includes(url.host) || oldRedditViews.includes(url.origin)) {
+      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 3bddd403..a821524b 100644
--- a/pages/options/options.html
+++ b/pages/options/options.html
@@ -121,6 +121,28 @@
         </table>
       </section>
       <section class="settings-block">
+        <table class="option" aria-label="Toggle old Reddit redirects">
+          <tbody>
+            <tr>
+              <td>
+                <h1 data-localise="__MSG_disableOldReddit__">
+                  Old Reddit Redirects
+                </h1>
+              </td>
+              <td>
+                <input
+                  aria-hidden="true"
+                  id="disable-old-reddit"
+                  type="checkbox"
+                  checked
+                />&nbsp;
+                <label for="disable-old-reddit" class="checkbox-label"> </label>
+              </td>
+            </tr>
+          </tbody>
+        </table>
+      </section>
+      <section class="settings-block">
         <h1 data-localise="__MSG_nitterInstance__">Nitter Instance</h1>
         <div class="autocomplete">
           <input
@@ -162,6 +184,16 @@
         </div>
       </section>
       <section class="settings-block">
+        <h1 data-localise="__MSG_oldRedditView__">Old Reddit View (Desktop or Mobile)</h1>
+        <div class="autocomplete">
+          <input
+            id="old-reddit-view"
+            type="url"
+            placeholder="https://old.reddit.com"
+          />
+        </div>
+      </section>
+      <section class="settings-block">
         <h1 data-localise="__MSG_theme__">Theme</h1>
         <select id="theme">
           <option value="">System</option>
diff --git a/pages/options/options.js b/pages/options/options.js
index e6bb9837..2c3d5df9 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 oldRedditViews = [
+  "https://old.reddit.com", // desktop
+  "https://i.reddit.com" // mobile
+];
 const autocompletes = [
   { id: "nitter-instance", instances: nitterInstances },
   { id: "invidious-instance", instances: invidiousInstances },
   { id: "bibliogram-instance", instances: bibliogramInstances },
   { id: "osm-instance", instances: osmInstances },
+  { id: "old-reddit-view", instances: oldRedditViews },
 ];
 
 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 oldRedditView = document.getElementById("old-reddit-view");
 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 disableOldReddit = document.getElementById("disable-old-reddit");
 let alwaysProxy = document.getElementById("always-proxy");
 let onlyEmbeddedVideo = document.getElementById("only-embed");
 let videoQuality = document.getElementById("video-quality");
@@ -104,10 +111,12 @@ browser.storage.sync.get(
     "invidiousInstance",
     "bibliogramInstance",
     "osmInstance",
+    "oldRedditView",
     "disableNitter",
     "disableInvidious",
     "disableBibliogram",
     "disableOsm",
+    "disableOldReddit",
     "alwaysProxy",
     "onlyEmbeddedVideo",
     "videoQuality",
@@ -129,10 +138,12 @@ browser.storage.sync.get(
     invidiousInstance.value = result.invidiousInstance || "";
     bibliogramInstance.value = result.bibliogramInstance || "";
     osmInstance.value = result.osmInstance || "";
+    oldRedditView.value = result.oldRedditView || "";
     disableNitter.checked = !result.disableNitter;
     disableInvidious.checked = !result.disableInvidious;
     disableBibliogram.checked = !result.disableBibliogram;
     disableOsm.checked = !result.disableOsm;
+    disableOldReddit.checked = !result.disableOldReddit;
     alwaysProxy.checked = result.alwaysProxy;
     onlyEmbeddedVideo.checked = result.onlyEmbeddedVideo;
     videoQuality.value = result.videoQuality || "";
@@ -276,6 +287,15 @@ let osmInstanceChange = debounce(() => {
 }, 500);
 osmInstance.addEventListener("input", osmInstanceChange);
 
+let oldRedditViewChange = debounce(() => {
+  if (oldRedditView.checkValidity()) {
+    browser.storage.sync.set({
+      oldRedditView: parseURL(oldRedditView.value),
+    });
+  }
+}, 500);
+oldRedditView.addEventListener("input", oldRedditViewChange);
+
 disableNitter.addEventListener("change", (event) => {
   browser.storage.sync.set({ disableNitter: !event.target.checked });
 });
@@ -292,6 +312,10 @@ disableOsm.addEventListener("change", (event) => {
   browser.storage.sync.set({ disableOsm: !event.target.checked });
 });
 
+disableOldReddit.addEventListener("change", (event) => {
+  browser.storage.sync.set({ disableOldReddit: !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 e06159a5..faf0deb8 100644
--- a/pages/popup/popup.html
+++ b/pages/popup/popup.html
@@ -116,6 +116,29 @@
       </table>
     </section>
 
+    <section class="settings-block">
+      <table class="option" aria-label="Toggle old Reddit redirects">
+        <tbody>
+          <tr>
+            <td>
+              <h1 data-localise="__MSG_disableOldReddit__">
+                Old Reddit Redirects
+              </h1>
+            </td>
+            <td>
+              <input
+                aria-hidden="true"
+                id="disable-old-reddit"
+                type="checkbox"
+                checked
+              />&nbsp;
+              <label for="disable-old-reddit" class="checkbox-label"> </label>
+            </td>
+          </tr>
+        </tbody>
+      </table>
+    </section>
+
     <section class="settings-block"></section>
 
     <footer>
diff --git a/pages/popup/popup.js b/pages/popup/popup.js
index 54eae408..34253702 100644
--- a/pages/popup/popup.js
+++ b/pages/popup/popup.js
@@ -4,6 +4,7 @@ let disableNitter = document.querySelector("#disable-nitter");
 let disableInvidious = document.querySelector("#disable-invidious");
 let disableBibliogram = document.querySelector("#disable-bibliogram");
 let disableOsm = document.querySelector("#disable-osm");
+let disableOldReddit = document.querySelector("#disable-old-reddit");
 let version = document.querySelector("#version");
 
 window.browser = window.browser || window.chrome;
@@ -14,6 +15,7 @@ browser.storage.sync.get(
     "disableInvidious",
     "disableBibliogram",
     "disableOsm",
+    "disableOldReddit",
     "theme",
   ],
   (result) => {
@@ -22,6 +24,7 @@ browser.storage.sync.get(
     disableInvidious.checked = !result.disableInvidious;
     disableBibliogram.checked = !result.disableBibliogram;
     disableOsm.checked = !result.disableOsm;
+    disableOldReddit.checked = !result.disableOldReddit;
   }
 );
 
@@ -43,6 +46,10 @@ disableOsm.addEventListener("change", (event) => {
   browser.storage.sync.set({ disableOsm: !event.target.checked });
 });
 
+disableOldReddit.addEventListener("change", (event) => {
+  browser.storage.sync.set({ disableOldReddit: !event.target.checked });
+});
+
 document.querySelector("#more-options").addEventListener("click", () => {
   browser.runtime.openOptionsPage();
 });