Shorten Medium URL

Shorten URL in medium.com

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Shorten Medium URL
// @namespace    https://github.com/FlandreDaisuki
// @version      0.1
// @description  Shorten URL in medium.com
// @author       FlandreDaisuki
// @match        https://medium.com/*
// @grant        none
// @noframes
// @run-at       document-start
// ==/UserScript==

const shorten = () => {
  if (location.pathname.match(/^\/([^/]*)\/.*-(\w+)$/)) {
    const url = new URL(location.href);
    url.search = '';
    url.pathname = url.pathname.replace(/^\/([^/]*)\/.*-(\w+)$/, '/$1/$2');

    history.replaceState({}, document.title, url.href.replace(url.origin, ''));
  }
};

window.observer = new MutationObserver(shorten);

window.addEventListener('load', shorten);

window.addEventListener('beforeunload', () => {
  window.observer.disconnect();
});

document.addEventListener('DOMContentLoaded', () => {
  const rootEl = document.getElementById('container');
  const config = { childList: true };
  window.observer.observe(rootEl, config);
});