Skip to content

Instantly share code, notes, and snippets.

@mathix420
Last active May 6, 2024 05:40
Show Gist options
  • Save mathix420/e0604ab0e916622972372711d2829555 to your computer and use it in GitHub Desktop.
Save mathix420/e0604ab0e916622972372711d2829555 to your computer and use it in GitHub Desktop.
Bypass Medium Paywall - Working late 2023 - Greasy Fork, Violentmonkey, Tampermonkey - Click the RAW button to install
// ==UserScript==
// @name Medium Paywall Bypass
// @namespace Violentmonkey Scripts
// @run-at document-start
// @match *://*.medium.com/*
// @match *://medium.com/*
// @match *://*/*
// @grant none
// @version 2.3
// @inject-into content
// @updateURL https://gist.githubusercontent.com/mathix420/e0604ab0e916622972372711d2829555/raw/medium.user.js
// @downloadURL https://gist.githubusercontent.com/mathix420/e0604ab0e916622972372711d2829555/raw/medium.user.js
// @website https://freedium.cfd
// @author Mathix420, ZhymabekRoman
// @description Don't forget to remove `@match` filters you don't want.
// ==/UserScript==
// initCall is telling us if we need to inject the title observer
function mediumRedirecter(initCall = false) {
// I miss typescript...
const mediumPostUrlProperty = ((document.head || {}).querySelector ? document.head.querySelector('meta[property="al:android:url"]') : {}) || {}
if (
// Allow seeing original articles that were already redirected to freedium.
!window.location.href.endsWith('#bypass') &&
// Do not redirect when editing on medium.
!window.location.href.includes("/edit?source=") &&
// Detect if we are on a medium website (regardless of the domain)
(mediumPostUrlProperty.content && mediumPostUrlProperty.content.includes('medium://p/'))
) {
window.location.href = 'https://freedium.cfd/' + window.location.href;
} else if (initCall && /(.*\.|^)medium\.com$/.test(window.location.host)) {
new MutationObserver(function(mutations) {
if (mutations[0].target.textContent) mediumRedirecter();
}).observe(
document.querySelector('title'),
{ subtree: true, characterData: true, childList: true }
);
}
}
mediumRedirecter(true);
@jt-z
Copy link

jt-z commented Mar 8, 2024

Thanks all Freedium-cfd members!

@ZhymabekRoman
Copy link

@mathix420 Can you fix this error? Sometimes this error appears:

TypeError: can't access property "querySelector", document.head is null

@mathix420
Copy link
Author

@ZhymabekRoman I've fixed the type error, but if the head tag is missing it won't be able to detect the medium page before js load.
Sorry for the delay, I got lot of work these days!

@ZhymabekRoman
Copy link

@mathix420 Thank you! I really appreciate your efforts

@a-pav
Copy link

a-pav commented Apr 18, 2024

I think you can simplify the below lines using Optional chaining (?.):

First, completely remove

- const mediumPostUrlProperty = ((document.head || {}).querySelector ? document.head.querySelector('meta[property="al:android:url"]') : {}) || {}

Then inside the first if statement, replace

- (mediumPostUrlProperty.content && mediumPostUrlProperty.content.includes('medium://p/'))
+ document.head?.querySelector('meta[property="al:android:url"]')?.content?.includes('medium://p/')

Personally, I'm not going to use // @match *://*/*. So I put this into a detectMediumWebsite() function and commented it out.

Thanks for your work.

P.S. By the way, are you sure that the MutationObserver is actually registered?

@rodolfogoulart
Copy link

Can you remove? // @match *://*/*

do not make sense to be for all sites.

@ZhymabekRoman
Copy link

do not make sense to be for all sites.

Medium has too many sub-domains

@a-pav
Copy link

a-pav commented Apr 25, 2024

Medium has too many sub-domains

// @match *://*.medium.com/* Should be enough for matching sub-domains.

@ZhymabekRoman
Copy link

Should be enough for matching sub-domains.

What about devopsquare.com, blog.devops.dev, blog.stackademic.com, ai.plainenglish.io, bettermarketing.pub and etc? It's impossible for us to say how many Medium sites there are.

@a-pav
Copy link

a-pav commented Apr 25, 2024

You are right. I didn't know those type of sites exist. But they are just different domains, technically speaking.

To recap:
// @match *://*.medium.com/* Is enough for matching sub-domains.
// @match *://*/* Is needed for matching any possible domain who happens to be a Medium website.

@yluom
Copy link

yluom commented Apr 26, 2024

freedium is offline so unfortunately this script doesn't work anymore for now

@ZhymabekRoman
Copy link

All works as expected. Yeah, we made some codebase refactor, to improve speed and some big bug fixes. And there were some minor downtimes.

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