Github JSON Dependencies Linker

Linkify all dependencies found in an JSON file.

目前為 2015-03-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @id          Github_JSON_Dependencies_Linker@https://github.com/jerone/UserScripts
// @name        Github JSON Dependencies Linker
// @namespace   https://github.com/jerone/UserScripts
// @description Linkify all dependencies found in an JSON file.
// @author      jerone
// @copyright   2015+, jerone (http://jeroenvanwarmerdam.nl)
// @license     GNU GPLv3
// @homepage    https://github.com/jerone/UserScripts/tree/master/Github_JSON_Dependencies_Linker
// @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_JSON_Dependencies_Linker
// @supportURL  https://github.com/jerone/UserScripts/issues
// @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
// @version     0.1.0
// @grant       GM_xmlhttpRequest
// @run-at      document-end
// @include     https://github.com/*/package.json
// @include     https://github.com/*/bower.json
// @include     https://github.com/*/project.json
// ==/UserScript==
/* global GM_xmlhttpRequest */

(function() {

	var isNPM = location.pathname.endsWith('/package.json'),
		isBower = location.pathname.endsWith('/bower.json'),
		isNuGet = location.pathname.endsWith('/project.json'),
		blobElm = document.querySelector('.blob-wrapper'),
		blobLineElms = blobElm.querySelectorAll('.blob-code > span'),
		pkg = (function() {
			// JSON parser could fail on JSON with comments;
			try {
				return JSON.parse(blobElm.textContent);
			} catch (ex) {
				function stripJsonComments(str) {
						/*!
							strip-json-comments
							Strip comments from JSON. Lets you use comments in your JSON files!
							https://github.com/sindresorhus/strip-json-comments
							by Sindre Sorhus
							MIT License
						*/
						var currentChar;
						var nextChar;
						var insideString = false;
						var insideComment = false;
						var ret = '';
						for (var i = 0; i < str.length; i++) {
							currentChar = str[i];
							nextChar = str[i + 1];
							if (!insideComment && str[i - 1] !== '\\' && currentChar === '"') {
								insideString = !insideString;
							}
							if (insideString) {
								ret += currentChar;
								continue;
							}
							if (!insideComment && currentChar + nextChar === '//') {
								insideComment = 'single';
								i++;
							} else if (insideComment === 'single' && currentChar + nextChar === '\r\n') {
								insideComment = false;
								i++;
								ret += currentChar;
								ret += nextChar;
								continue;
							} else if (insideComment === 'single' && currentChar === '\n') {
								insideComment = false;
							} else if (!insideComment && currentChar + nextChar === '/*') {
								insideComment = 'multi';
								i++;
								continue;
							} else if (insideComment === 'multi' && currentChar + nextChar === '*/') {
								insideComment = false;
								i++;
								continue;
							}
							if (insideComment) {
								continue;
							}
							ret += currentChar;
						}
						return ret;
					}
					// Strip out comments from the JSON and try again;
				return JSON.parse(stripJsonComments(blobElm.textContent));
			}
		})(),
		dependencyKeys = [
			'dependencies',
			'devDependencies',
			'peerDependencies',
			'bundleDependencies',
			'bundledDependencies',
			'optionalDependencies'
		],
		modules = [];

	// Get an unique list of all modules;
	dependencyKeys.forEach(function(dependencyKey) {
		var dependencies = pkg[dependencyKey] || {};
		Object.keys(dependencies).forEach(function(module) {
			if (modules.indexOf(module) === -1) {
				modules.push(module);
			}
		});
	});

	// Get url depending on json type;
	var getUrl = (function() {
		if (isNPM) {
			return function(module) {
				var url = 'https://www.npmjs.org/package/' + module;
				linkify(module, url);
			};
		} else if (isBower) {
			return function(module) {
				GM_xmlhttpRequest({
					method: 'GET',
					url: 'http://bower.herokuapp.com/packages/' + module,
					onload: function(response) {
						var data = JSON.parse(response.responseText);
						var re = /github\.com\/([\w\-\.]+)\/([\w\-\.]+)/i;
						var parsedUrl = re.exec(data.url.replace(/\.git$/, ''));
						if (parsedUrl) {
							var user = parsedUrl[1];
							var repo = parsedUrl[2];
							var url = 'https://github.com/' + user + '/' + repo;
							linkify(module, url);
						} else {
							linkify(module, data.url);
						}
					}
				});
			};
		} else if (isNuGet) {
			return function(module) {
				var url = 'https://www.nuget.org/packages/' + module;
				linkify(module, url);
			};
		}
	})();

	// Linkify module;
	function linkify(module, url) {

		// Try to find the module; could be mulitple locations;
		var moduleFilterText = '"' + module + '"';
		var moduleElms = Array.prototype.filter.call(blobLineElms, function(blobLineElm) {
			return blobLineElm.textContent.trim() === moduleFilterText;
		});

		// Modules could exist in multiple dependency lists;
		Array.prototype.forEach.call(moduleElms, function(moduleElm) {

			// Module names are textNodes on Github;
			var moduleElmText = Array.prototype.find.call(moduleElm.childNodes, function(moduleElmChild) {
				return moduleElmChild.nodeType === 3;
			});

			var moduleElmLink = document.createElement('a');
			moduleElmLink.setAttribute('href', url);
			moduleElmLink.appendChild(document.createTextNode(module));

			// Replace textNode, so we keep surrounding elements (like the highlighted quotes);
			moduleElm.replaceChild(moduleElmLink, moduleElmText);
		});
	}

	// Init;
	modules.forEach(function(module) {
		getUrl(module);
	});

})();