cliff_barr / Upgrade Twitter Images

// ==UserScript==
// @name         Upgrade Twitter Images
// @version      1.1.0
// @description  Automatically redirect Twitter images to their original resolution
// @author       cliff_barr: https://openuserjs.org/users/cliff_barr
// @namespace    https://openuserjs.org/users/cliff_barr
// @match        https://pbs.twimg.com/media/*
// @grant        none
// @license      MIT
// ==/UserScript==

(() => {
    const wl = window.location.href;

    if (sessionStorage.getItem(wl) !== null) return;
  
    if (wl.match(/^https:\/\/pbs.twimg.com\/media\/.+\.jpg$/) !== null) {
        sessionStorage.setItem(wl, '1');
        const upgradeUrl = `${wl}:orig`;
        console.info('Upgraded Twitter image');
        history.pushState({}, upgradeUrl, upgradeUrl);
        window.location.assign(upgradeUrl);
    } else if (wl.match(/^https:\/\/pbs.twimg.com\/media\/.+\?format=\w+&name=\w+$/) !== null) {
        const urlParts = wl.match(/^(https:\/\/pbs.twimg.com\/media\/.+)\?format=(\w+)&name=\w+$/);
        sessionStorage.setItem(wl, '1');
        const upgradeUrl = `${urlParts[1]}.${urlParts[2]}:orig`;
        console.info('Upgraded Twitter image');
        history.pushState({}, upgradeUrl, upgradeUrl);
        window.location.assign(upgradeUrl);
    }
})();