Last active
September 4, 2018 21:26
-
-
Save domharrington/98e187367bd654301f8758eac200f9e9 to your computer and use it in GitHub Desktop.
Replace `require()` calls with links to npm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Replace `require()` calls with links to npm | |
// @namespace https://gist.github.com/domharrington | |
// @version 0.2 | |
// @author @domharrington | |
// @match https://github.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function replaceRequires() { | |
Array.from(document.querySelectorAll('.pl-c1')).filter(el => el.textContent === 'require').filter(el => el.nextElementSibling.textContent.match(/^'[^\/|.\/|..\/]/)).map(el => el.nextElementSibling).forEach(el => { | |
const textNode = Array.from(el.childNodes).find(node => node instanceof Text); | |
const a = document.createElement('a'); | |
a.href = `https://www.npmjs.com/package/${textNode.textContent}`; | |
a.textContent = textNode.textContent; | |
el.replaceChild(a, textNode); | |
}); | |
} | |
replaceRequires(); | |
document.addEventListener('pjax:end', replaceRequires); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment