bennyp / UNPKG module links

// ==UserScript==
// @name         UNPKG module links
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Add links to unpkg module specifiers
// @author       You
// @match        https://unpkg.com/*.js?module
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const SPECIFIER_REGEXP = /(import|export) (.*) from ('|")(.*)['"]/g;

    const pre = document.querySelector('pre')
    const src = pre.textContent
        .replace(SPECIFIER_REGEXP, '$1 $2 from $3</pre><a href="$4">$4</a><pre>$3')

    pre.innerHTML = src;

    const style = document.createElement('style')
    style.innerHTML = `pre { display: inline; margin: 0; }`;
    document.head.appendChild(style);
})();