From ddeda15eeea6d3d4a20524b4f29e80a453c7460c Mon Sep 17 00:00:00 2001 From: OctoNezd Date: Sun, 24 Mar 2024 16:41:33 +0300 Subject: [PATCH] Add button to go to old reddit if new reddit is detected --- src/css/redditChanges.css | 15 ++++++++++++++ src/features/index.ts | 2 ++ src/features/redesignRedirect.ts | 34 ++++++++++++++++++++++++++++++++ webpack.config.js | 4 ++-- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/features/redesignRedirect.ts diff --git a/src/css/redditChanges.css b/src/css/redditChanges.css index 7134486..6b9b634 100644 --- a/src/css/redditChanges.css +++ b/src/css/redditChanges.css @@ -206,3 +206,18 @@ body.prevent-scroll { .search-submit-button { width: 50px; } + +.switch-to-good-reddit { + display: block; + position: fixed; + bottom: 0; + left: 0; + background-color: #ff4500; + color: white; + z-index: 9999999999; + font-size: large; + font-weight: bolder; + text-transform: uppercase; + pointer-events: all; + padding: 3px; +} diff --git a/src/features/index.ts b/src/features/index.ts index 11e419e..eb1c18b 100644 --- a/src/features/index.ts +++ b/src/features/index.ts @@ -6,6 +6,7 @@ import RESCompatibility from "./RESCompatibility"; import PostsEnhancements from "./posts"; import { OLFeature } from "./base"; import WhiteTheme from "./themeSwitch"; +import RedesignRedirect from "./redesignRedirect"; type Constructor = new (...args: any[]) => OLFeature; const features: Array = [ Expandos, @@ -14,6 +15,7 @@ const features: Array = [ RESCompatibility, RedditMarquee, WhiteTheme, + RedesignRedirect, ]; features.push(...PostsEnhancements); export default features; diff --git a/src/features/redesignRedirect.ts b/src/features/redesignRedirect.ts new file mode 100644 index 0000000..775ae83 --- /dev/null +++ b/src/features/redesignRedirect.ts @@ -0,0 +1,34 @@ +import querySelectorAsync from "../utility/querySelectorAsync"; +import { OLFeature } from "./base"; +function replaceSubdomain(url: string, toSubdomain: string) { + const replace = "://" + toSubdomain + "."; + + // Prepend http:// + if (!/^\w*:\/\//.test(url)) { + url = "http://" + url; + } + + // Check if we got a subdomain in url + if (url.match(/\.\w*\b/g)!.length > 1) { + return url.replace(/(:\/\/\w+\.)/, replace); + } + + return url.replace(/:\/\/(\w*\.)/, `${replace}$1`); +} + +export default class RedesignRedirect extends OLFeature { + moduleId = "redesignRedirect"; + moduleName = "RedesignRedirect"; + async init() { + await querySelectorAsync("body"); + if (!document.documentElement.classList.contains("js")) { + document.documentElement.classList.add("new-reddit"); + const goToOldReddit = document.createElement("a"); + goToOldReddit.classList.add("switch-to-good-reddit"); + goToOldReddit.innerText = "Go to old reddit"; + console.log("swt", replaceSubdomain(window.location.href, "old")); + goToOldReddit.href = replaceSubdomain(window.location.href, "old"); + document.body.appendChild(goToOldReddit); + } + } +} diff --git a/webpack.config.js b/webpack.config.js index ded363e..0fe1e3c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -51,7 +51,7 @@ const manifest = { permissions: ["storage"], content_scripts: [ { - matches: ["*://old.reddit.com/*"], + matches: ["*://*.reddit.com/*", "*://reddit.com/*"], js: ["./cs.js", "./vendors.js"], run_at: "document_start", }, @@ -72,7 +72,7 @@ const userScriptBanner = `// ==UserScript== // @version ${fullVersion} // @description Makes old reddit more usable on mobile devices. // @author OctoNezd -// @match https://old.reddit.com/* +// @match https://reddit.com/* // @icon https://raw.githubusercontent.com/OctoNezd/oldlander/main/icons/icon.png // @grant GM.setValue // @grant GM.getValue