Medium Unlocker (via Freedium)

Redirect Medium's premium posts to Freedium, also works for custom domains

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Medium Unlocker (via Freedium)
// @namespace    http://tampermonkey.net/
// @version      0.0.7
// @description  Redirect Medium's premium posts to Freedium, also works for custom domains
// @author       d0gkiller87
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=medium.com
// @grant        none
// @run-at       document-start
// @noframes
// @license      MIT
// ==/UserScript==

/* jshint esversion: 11 */

(function() {
  'use strict';

  function hasMediumLogo() {
    return document.querySelector( '#wordmark-medium-desc' ) !== null;
  }

  function isMediumPost() {
    const path = window.location.pathname;
    const pathParts = path.split( '/' );
    return (
      path !== '/' &&
      ( pathParts[pathParts.length - 1].match( /-[a-f0-9]{12,}$/ ) ) &&
      hasMediumLogo()
    );
  }

  let asking = false;
  function askRedirect() {
    if ( asking ) return;

    asking = true;
    if ( confirm( 'Redirect to Freedium?' ) ) {
      window.location.host = 'freedium-mirror.cfd';
    }
    asking = false;
  }

  if ( window.top !== window.self ) {
    return;
  }

  let cancel_state_change_handler = false;

  const readyStateChangeHandler = document.addEventListener( 'readystatechange', event => {
    if ( document.readyState !== 'interactive' ) return;
    document.removeEventListener( 'readystatechange', readyStateChangeHandler );

    if ( hasMediumLogo() ) {
      const hookedReplaceState = patchHistory( 'replaceState' );
      Object.defineProperty( history, 'replaceState', {
        configurable: false,
        enumerable: true,
        get() {
          return hookedReplaceState;
        },
        set( fn ) {
          // modification blocked
        }
      });
      window.addEventListener( 'replaceState', askRedirect );
    }

    if ( !isMediumPost() ) return;
    if ( cancel_state_change_handler ) return;
    askRedirect();
  });

  function patchHistory( method ) {
    const original = window.history[method];
    return function() {
      const result = original.apply( this, arguments );
      const event = new Event( method );
      event.arguments = arguments;
      window.dispatchEvent( event );
      return result;
    };
  }

  if ( window.performance ) {
    const newNavigations = window.performance.getEntriesByType( 'navigation' );
    for ( const navigation of newNavigations ) {
      if ( navigation.type === 'back_forward' ) {
        cancel_state_change_handler = true;
        document.removeEventListener( 'readystatechange', readyStateChangeHandler );
        return;
      }
    }

    const oldNavigation = window.performance?.navigation?.type;
    if ( oldNavigation && oldNavigation === window.performance.navigation.TYPE_BACK_FORWARD ) {
      cancel_state_change_handler = true;
      document.removeEventListener( 'readystatechange', readyStateChangeHandler );
      return;
    }
  }
})();