Last active
August 29, 2015 14:05
-
-
Save spiralx/e570edbeb624e9ef0236 to your computer and use it in GitHub Desktop.
GitHub package.json dependency linker
This file contains 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 Github - package.json/bower.json dependency linker | |
// @version 0.0.2 | |
// @namespace http://spiralx.org/github-npm-autolink | |
// @author James Skinner <[email protected]> http://userscripts.org/users/55684 | |
// @description When viewing a package.json file, automatically links dependencies to their NPM page | |
// @include https://github.com/*/package.json | |
// @include https://github.com/*/bower.json | |
// @run-at document-end | |
// @grant unsafeWindow GM_xmlhttpRequest | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js | |
// @require https://gist.githubusercontent.com/spiralx/6404947/raw/364699bfa5effc13a14d452491f2d9d1edac11b5/fmt.js | |
// ==/UserScript== | |
(function(window, document, $) { | |
function updateUrl(module, url) { | |
console.info(module, url); | |
var t = '"' + module + '"'; | |
$nt | |
.filter(function() { | |
return $(this).text().trim() === t | |
}) | |
.html(format('"<a href="{0}">{1}</a>"', url, module)); | |
} | |
var $code = $('.highlight .js-file-line'), | |
$nt = $code.find('.nt'), | |
pkg = JSON.parse($code.text()), | |
isNpm = /\/package.json$/.test(location.pathname); | |
//console.log(GM_xmlhttpRequest); | |
'dependencies devDependencies peerDependencies bundleDependencies bundledDependencies optionalDependencies'.split(' ').forEach(function(k) { | |
var deps = pkg[k] || {}; | |
for (var module in deps) { | |
var | |
url = 'https://www.npmjs.org/package/' + module, | |
jsonUrl = 'http://registry.npmjs.org/' + module + '/latest'; | |
console.info(module); | |
if (isNpm) { | |
updateUrl(module, 'https://www.npmjs.org/package/' + module); | |
} | |
else { | |
//updateUrl(module, 'https://bower.herokuapp.com/packages/' + module); | |
//$.getJSON('http://bower.herokuapp.com/packages/' + module, function(data) { | |
// console.info(data); | |
// updateUrl(module, data.url); | |
//}) | |
// .fail(function(jqXHR, textStatus, errorThrown) { | |
// console.warn(textStatus, errorThrown); | |
// }); | |
var ret = GM_xmlhttpRequest({ | |
method: 'GET', | |
url: 'http://bower.herokuapp.com/packages/' + module, | |
onload: function(response) { | |
console.log(module, response); | |
var data = JSON.parse(response.responseJSON); | |
updateUrl(module, data.url); | |
}, | |
onerror: function(response) { | |
console.warn(module, response.statusText); | |
} | |
}); | |
console.log(ret); | |
} | |
} | |
}); | |
})(window, document, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment