Skip to content

Instantly share code, notes, and snippets.

@samhenrigold
Last active September 15, 2023 19:29
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save samhenrigold/4a082dde823bc3cb62e43a2fc2b12b8e to your computer and use it in GitHub Desktop.
Save samhenrigold/4a082dde823bc3cb62e43a2fc2b12b8e to your computer and use it in GitHub Desktop.
UserScript to redirect medium.com articles to scribe.rip
// ==UserScript==
// @name Medium to Scribe
// @description Redirects Medium.com articles to scribe.rip
// @match *://*/*
// @exclude /^https?://(www\.)?medium\.com/((\?.*)|((m|about|creators|membership)/.*))$/
// @run-at document-end
// @version 1.2.4
// @updateURL https://gist.githubusercontent.com/samhenrigold/4a082dde823bc3cb62e43a2fc2b12b8e/raw/medium-to-scribe.js
// ==/UserScript==
/*
I rewrote this script to be more efficient and less prone to breaking. Here's how it works:
0. Before the script even runs, check to see if the current page is a Medium URL but NOT an article URL. This is to prevent the script from redirecting you from Medium's terms of service or signup pages.
1. We check if the page you're on is a Medium article by checking for the presence of a specific meta tag in the document head.
2. Check if we've already written the datetime to this article's localstorage (we use the key "scribeRedirected").
a. If scribeRedirected is present, we know we've already redirected this article to scribe.rip and you've gone back to Medium for whatever reason (If scribe broke the article or something).
b. If it doesn't exist OR if the datetime is older than 30 seconds in the past, we assume you've been sent to this Medium article and want to get redirected to scribe, so we write the current datetime to localStorage and send you on your way.
*/
// Match <meta property="al:android:package" content="com.medium.reader"> in the document head
if (document.querySelector("meta[property='al:android:package']") && document.querySelector("meta[property='al:android:package']").content === "com.medium.reader") {
// check if localStorage.getItem("scribeRedirected") is empty or if the value is within the last 10 minutes
if (localStorage.getItem("scribeRedirected") === null || (new Date().getTime() - localStorage.getItem("scribeRedirected")) > 30_000) {
localStorage.setItem("scribeRedirected", Date.now());
window.history.pushState({}, "", window.location.href);
window.location.hostname = "scribe.rip";
// Make sure the old url is in the history so we can go back to it
}
}
@jamesWalker55
Copy link

I think you may need to change from @run-at from document-start to document-end. As of now the script runs before the page even loads, so document.querySelector("meta[property='al:android:package']") will always return null.

@samhenrigold
Copy link
Author

good catch, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment