asameshimae / Anonymise Medium

// ==UserScript==
// @name         Anonymise Medium
// @namespace    https://github.com/asameshimae
// @version      0.4
// @description  Hide certain personal info from Medium to support anonymous browsing - e.g. for marking assessed work
// @author       @asameshimae
// @license      MIT
// @match        https://medium.com/*
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    // create a new style element to add override rules to
    var s = document.createElement("style");
    s.appendChild(document.createTextNode(""));
    document.head.appendChild(s);
    const oldTitle = document.title;
    document.title = '...';

    // add rules to hide certain elements
	s.sheet.insertRule('header,footer,.js-postShareWidget,.postActionsBar { display:none !important }', 0);
    s.sheet.insertRule('article { margin-bottom:3em !important; margin-top:3em !important }', 0);
    const bodyRule = s.sheet.insertRule('body { display:none !important }',0);
    if(document.location.href.match(/^https?:\/\/medium\.com\/@[^\/]+\/?$/)!==null) {
        // s.sheet.insertRule('.bz, .s.aw, .ds.dt.s.aw.t { display:none !important }', 0);
        var user = document.title.replace(/ . Medium$/,'').trim();
        const hide = () => [...document.querySelectorAll('title,p,a,h1,h2,h3,h4,h5,h6,img')].filter(e=>e.textContent.includes(user) || (e.alt && e.alt.includes(user))).forEach(e=>{e.textContent=e.textContent.replace(user,'███');if(e.alt){e.style.display='none';e.alt.replace(user,'███');}});
        hide();
        setInterval(hide,500);
    }

    // redact usernames/avatars where they may appear in text (such as inline links or browse pages)
    [...document.querySelectorAll('a[data-user-id],a[href^="/@"]')].forEach(e=>(e.textContent='███'));
    setInterval(function(){[...document.querySelectorAll('a[data-user-id],a[href^="/@"]')].forEach(e=>(e.textContent='███'));},1000);

    // hide 'sign in' banner at the bottom if it exists, which - when certain elements are hidden - may obscure the final line(s) of text
    const stickyFooter = document.querySelector('.js-stickyFooter');
    if(stickyFooter) stickyFooter.style.display='none';

    setTimeout(_=>{ s.sheet.deleteRule(bodyRule); document.title='anonymised'; },1000);


})();