Github - package.json dependency linker

When viewing a package.json file, automatically links dependencies to their NPM page

As of 2014-10-08. See the latest version.

  1. // ==UserScript==
  2. // @name Github - package.json dependency linker
  3. // @description When viewing a package.json file, automatically links dependencies to their NPM page
  4. // @author James Skinner <spiralx@gmail.com> http://userscripts.org/users/55684
  5. // @namespace http://spiralx.org/
  6. // @license BSD
  7. // @version 0.0.2
  8. // @include https://github.com/*/package.json
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.js
  10. // ==/UserScript==
  11.  
  12. function format(formatString, data) {
  13. data = arguments.length == 2 && typeof data === "object" && !Array.isArray(data)
  14. ? data
  15. : [].slice.call(arguments, 1);
  16.  
  17. return formatString
  18. .replace(/\{\{/g, String.fromCharCode(0))
  19. .replace(/\}\}/g, String.fromCharCode(1))
  20. .replace(/\{([^}]+)\}/g, function(match, path) {
  21. try {
  22. var p = path.replace(/\[(-?\w+)\]/g, '.$1').split('.');
  23. //console.log('path="%s" (%s), data=%s', path, p.toSource(), data.toSource());
  24. return String(p.reduce(function(o, n) {
  25. return o.slice && !isNaN(n) ? o.slice(n).shift() : o[n];
  26. }, data));
  27. }
  28. catch (ex) {
  29. return match;
  30. }
  31. })
  32. .replace(/\x00/g, "{")
  33. .replace(/\x01/g, "}");
  34. }
  35.  
  36. // --------------------------------------------------------------------------
  37.  
  38. var $code = $('.highlight .js-file-line'),
  39. pkg = JSON.parse($code.text()),
  40. depKeys = [
  41. "dependencies",
  42. "devDependencies",
  43. "peerDependencies",
  44. "bundleDependencies",
  45. "bundledDependencies",
  46. "optionalDependencies"
  47. ];
  48. depKeys.forEach(function(k) {
  49. var deps = pkg[k] || {};
  50. for (var module in deps) {
  51. var
  52. url = 'https://www.npmjs.org/package/' + module,
  53. // jsonUrl = 'http://registry.npmjs.org/' + module + '/latest',
  54. t = '"' + module + '"';
  55. $code
  56. .find('.nt')
  57. .filter(function() {
  58. return $(this).text().trim() === t
  59. })
  60. .html(format('"<a href="{0}">{1}</a>"', url, module));
  61. }
  62. });