Add support for the old Reddit design

Add support for redirecting Reddit links to the old desktop and
mobile designs.
This commit is contained in:
nitrohorse 2020-09-05 19:50:59 -07:00
parent ae6f4f6a6c
commit 9100240454
No known key found for this signature in database
GPG Key ID: 6F3175557E766CBF
5 changed files with 134 additions and 0 deletions

View File

@ -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(

View File

@ -120,6 +120,28 @@
</tbody>
</table>
</section>
<section class="settings-block">
<table class="option" aria-label="Toggle Reddit version redirects">
<tbody>
<tr>
<td>
<h1 data-localise="__MSG_disableRedditVersion__">
Reddit Version Redirects
</h1>
</td>
<td>
<input
aria-hidden="true"
id="disable-reddit-version"
type="checkbox"
checked
/>&nbsp;
<label for="disable-reddit-version" class="checkbox-label"> </label>
</td>
</tr>
</tbody>
</table>
</section>
<section class="settings-block">
<h1 data-localise="__MSG_nitterInstance__">Nitter Instance</h1>
<div class="autocomplete">
@ -161,6 +183,16 @@
/>
</div>
</section>
<section class="settings-block">
<h1 data-localise="__MSG_redditVersion__">Reddit Version</h1>
<div class="autocomplete">
<input
id="reddit-version"
type="url"
placeholder="https://old.reddit.com"
/>
</div>
</section>
<section class="settings-block">
<h1 data-localise="__MSG_theme__">Theme</h1>
<select id="theme">

View File

@ -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 });
});

View File

@ -116,6 +116,29 @@
</table>
</section>
<section class="settings-block">
<table class="option" aria-label="Toggle Reddit version redirects">
<tbody>
<tr>
<td>
<h1 data-localise="__MSG_disableRedditVersion__">
Reddit Version Redirects
</h1>
</td>
<td>
<input
aria-hidden="true"
id="disable-reddit-version"
type="checkbox"
checked
/>&nbsp;
<label for="disable-reddit-version" class="checkbox-label"> </label>
</td>
</tr>
</tbody>
</table>
</section>
<section class="settings-block"></section>
<footer>

View File

@ -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 disableRedditVersion = document.querySelector("#disable-reddit-version");
let version = document.querySelector("#version");
window.browser = window.browser || window.chrome;
@ -14,6 +15,7 @@ browser.storage.sync.get(
"disableInvidious",
"disableBibliogram",
"disableOsm",
"disableRedditVersion",
"theme",
],
(result) => {
@ -22,6 +24,7 @@ browser.storage.sync.get(
disableInvidious.checked = !result.disableInvidious;
disableBibliogram.checked = !result.disableBibliogram;
disableOsm.checked = !result.disableOsm;
disableRedditVersion.checked = !result.disableRedditVersion;
}
);
@ -43,6 +46,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 });
});
document.querySelector("#more-options").addEventListener("click", () => {
browser.runtime.openOptionsPage();
});