Skip to content

Instantly share code, notes, and snippets.

@domharrington
Last active September 4, 2018 21:26
Show Gist options
  • Save domharrington/98e187367bd654301f8758eac200f9e9 to your computer and use it in GitHub Desktop.
Save domharrington/98e187367bd654301f8758eac200f9e9 to your computer and use it in GitHub Desktop.
Replace `require()` calls with links to npm
// ==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