Merge pull request #102 from nitrohorse/add-old-reddit-support

feat(): Add support for redirecting to the old Reddit desktop or mobile design
This commit is contained in:
Simon Brazell 2020-11-05 08:21:35 +11:00 committed by GitHub
commit bbd648a2e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 129 additions and 0 deletions

View File

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

View File

@ -120,6 +120,28 @@
</tbody>
</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">
@ -161,6 +183,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">

View File

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

View File

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

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