GitHub file list beautifier

Adds colors to files by type, displays small images in place of file-type icons in a repository source tree

2016-08-05 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        GitHub file list beautifier
// @description Adds colors to files by type, displays small images in place of file-type icons in a repository source tree
// @namespace   wOxxOm.scripts
// @include     https://github.com/*
// @version     3.0.0
// @run-at      document-start
// @grant       GM_getValue
// @grant       GM_setValue
// ==/UserScript==

/* jshint lastsemic:true, multistr:true, laxbreak:true, -W030, -W041, -W084 */

// Your changes will be remembered in script database even after a forced update of the script
var config = userConfig({
	iconSize: 24,
	colorSeed1: 13,
	colorSeed2: 1299721,
	colorSeed3: 179426453,
});

(document.head || document.documentElement).insertAdjacentHTML('beforeend',
	'<style id="wOxxOm-GHB">\
		.wOxxOm-image-icon {\
			max-width:' + config.iconSize + 'px; max-height:' + config.iconSize + 'px;\
			width:auto; height:auto; margin:auto;\
			position:absolute; left:0; top:0; right:0; bottom:0 }\
		.wOxxOm-image-td {\
			position:relative; padding:0; min-width:' + (config.iconSize+4) + 'px; line-height:inherit }\
		a[file-type=":folder"] { font-weight: bold }\
	</style>'
);

var filetypes = {};
var styledom = document.getElementById('wOxxOm-GHB');

var filesdom = document.getElementsByTagName('table');
var ob = new MutationObserver(GHBsentry);

lazyObserve();
beautify(filesdom[0]);
document.addEventListener('DOMContentLoaded', function() { beautify(filesdom[0]) });

function GHBsentry(mutations) {
	if (!/^\/[^\/]+\/[^\/]+(\/tree\/.*)?$/.test(location.pathname)) {
		ob.disconnect();
		lazyObserve();
		return;
	}
	if (mutations.length == 1 && !mutations[0].addedNodes.length || !filesdom[0])
		return;
	//console.log([].concat.apply([], mutations.map(m => [].slice.call(m.addedNodes))));
	for (var i=0, ml=mutations.length, m; i<ml && (m=mutations[i]); i++) {
		for (var j=0, added=m.addedNodes, nl=added.length, n; j<nl && (n=added[j]); j++) {
			if (n.nodeType != 3) {
				if (n.localName == 'a' && /\/(tree|blob)\//.test(n.href) && !n.hasAttribute('file-type')
				||  n.localName == 'div' && n.matches('.file-wrap, .new-discussion-timeline, .repo-file-upload-tree-target')
				) {
					var table = n.closest('table') || n.querySelector('table');
					if (table) {
						beautify(table);
						ob.disconnect();
						lazyObserve();
						break;
					}
				}
			}
		}
	}
}

function lazyObserve() {
	// postpone observing until the parser can breathe after the initial burst of activity during page load
	setTimeout(function() {
		beautify(filesdom[0]);
		ob.observe(document, {subtree:true, childList:true});
	}, 0);
}

function beautify(container, links) {
	if (!container && !links)
		return;
	//console.log(container.innerHTML);
	if (!links) {
		var table = container.localName == 'table' ? container : container.querySelector('table');
		if (!table)
			return;
		links = table.getElementsByClassName('js-navigation-open');
	}

	for (var a, i=0, len=links.length; i<len && (a=links[i++]); ) {
		if (!a.href)
			continue;
		var tr = a.closest('tr');
		if (tr && tr.querySelector('.octicon-file-directory')) {
			a.setAttribute('file-type', ':folder');
			continue;
		}

		var ext = a.href.match(/\.(\w+)$/);
		ext = ext && ext[1] || ':empty';
		a.setAttribute('file-type', ext);
		if (!filetypes[ext])
			addFileTypeStyle(ext);

		if (/^(png|jpe?g|bmp|gif|cur|ico)$/.test(ext)) {
			var m = a.href.match(/github\.com\/(.+?\/)blob\/(.*)$/);
			if (!m)
				continue;
			var iconCell = a.closest('.js-navigation-item').querySelector('.icon');
			var icon = iconCell.querySelector('.octicon-file-text');

			if (icon.style.display == 'none')
				continue;
			icon.style.display = 'none';
			icon.insertAdjacentHTML('afterend',
									'<img class="wOxxOm-image-icon" src="https://raw.githubusercontent.com/' + m[1] + m[2] + '">');
			iconCell.classList.add('wOxxOm-image-td');
		}
	}
}

function addFileTypeStyle(type) {
	filetypes[type] = true;
	for (var hash=0, i=0, len=type.length; i<len; i++)
		hash = ((hash << 5) - hash) + type.charCodeAt(i);
	hash = Math.abs(hash*config.colorSeed1 |0);
	var color = 'a[file-type="' + type + '"] { color: hsl(' +
		(hash%360) + ',' +
		(hash*config.colorSeed2%50 + 50 |0) + '%,' +
		(hash*config.colorSeed3%30 + 25 |0) + '%) }';
	styledom.appendChild(document.createTextNode(color));
}

function userConfig(params) {
	Object.keys(params).forEach(function(k) {
		var def = params[k];
		var saved = GM_getValue(k, def);
		if (saved && saved != def)
			params[k] = saved;
	});
	return params;
}