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-21 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub file list beautifier
  3. // @description Adds colors to files by type, displays small images in place of file-type icons in a repository source tree
  4. // @author wOxxOm
  5. // @namespace wOxxOm.scripts
  6. // @license MIT License
  7. // @include https://github.com/*
  8. // @version 3.0.1
  9. // @icon https://octodex.github.com/images/murakamicat.png
  10. // @run-at document-start
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // ==/UserScript==
  14.  
  15. /* jshint lastsemic:true, multistr:true, laxbreak:true, -W030, -W041, -W084 */
  16.  
  17. // Your changes will be remembered in script database even after a forced update of the script
  18. var config = userConfig({
  19. iconSize: 24,
  20. colorSeed1: 13,
  21. colorSeed2: 1299721,
  22. colorSeed3: 179426453,
  23. });
  24.  
  25. (document.head || document.documentElement).insertAdjacentHTML('beforeend',
  26. '<style id="wOxxOm-GHB">\
  27. .wOxxOm-image-icon {\
  28. max-width:' + config.iconSize + 'px; max-height:' + config.iconSize + 'px;\
  29. width:auto; height:auto; margin:auto;\
  30. position:absolute; left:0; top:0; right:0; bottom:0 }\
  31. .wOxxOm-image-td {\
  32. position:relative; padding:0; min-width:' + (config.iconSize+4) + 'px; line-height:inherit }\
  33. a[file-type=":folder"] { font-weight: bold }\
  34. </style>'
  35. );
  36.  
  37. var filetypes = {};
  38. var styledom = document.getElementById('wOxxOm-GHB');
  39.  
  40. var filesdom = document.getElementsByTagName('table');
  41. var ob = new MutationObserver(GHBsentry);
  42.  
  43. lazyObserve();
  44. beautify(filesdom[0]);
  45. document.addEventListener('DOMContentLoaded', function() { beautify(filesdom[0]) });
  46.  
  47. function GHBsentry(mutations) {
  48. if (!/^\/[^\/]+\/[^\/]+(\/tree\/.*)?$/.test(location.pathname)) {
  49. ob.disconnect();
  50. lazyObserve();
  51. return;
  52. }
  53. if (mutations.length == 1 && !mutations[0].addedNodes.length || !filesdom[0])
  54. return;
  55. //console.log([].concat.apply([], mutations.map(m => [].slice.call(m.addedNodes))));
  56. for (var i=0, ml=mutations.length, m; i<ml && (m=mutations[i]); i++) {
  57. for (var j=0, added=m.addedNodes, nl=added.length, n; j<nl && (n=added[j]); j++) {
  58. if (n.nodeType != 3) {
  59. if (n.localName == 'a' && /\/(tree|blob)\//.test(n.href) && !n.hasAttribute('file-type')
  60. || n.localName == 'div' && n.matches('.file-wrap, .new-discussion-timeline, .repo-file-upload-tree-target')
  61. ) {
  62. var table = n.closest('table') || n.querySelector('table');
  63. if (table) {
  64. beautify(table);
  65. ob.disconnect();
  66. lazyObserve();
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74.  
  75. function lazyObserve() {
  76. // postpone observing until the parser can breathe after the initial burst of activity during page load
  77. setTimeout(function() {
  78. beautify(filesdom[0]);
  79. ob.observe(document, {subtree:true, childList:true});
  80. }, 0);
  81. }
  82.  
  83. function beautify(container, links) {
  84. if (!container && !links)
  85. return;
  86. //console.log(container.innerHTML);
  87. if (!links) {
  88. var table = container.localName == 'table' ? container : container.querySelector('table');
  89. if (!table)
  90. return;
  91. links = table.getElementsByClassName('js-navigation-open');
  92. }
  93.  
  94. for (var a, i=0, len=links.length; i<len && (a=links[i++]); ) {
  95. if (!a.href)
  96. continue;
  97. var tr = a.closest('tr');
  98. if (tr && tr.querySelector('.octicon-file-directory')) {
  99. a.setAttribute('file-type', ':folder');
  100. continue;
  101. }
  102.  
  103. var ext = a.href.match(/\.(\w+)$/);
  104. ext = ext && ext[1] || ':empty';
  105. a.setAttribute('file-type', ext);
  106. if (!filetypes[ext])
  107. addFileTypeStyle(ext);
  108.  
  109. if (/^(png|jpe?g|bmp|gif|cur|ico)$/.test(ext)) {
  110. var m = a.href.match(/github\.com\/(.+?\/)blob\/(.*)$/);
  111. if (!m)
  112. continue;
  113. var iconCell = a.closest('.js-navigation-item').querySelector('.icon');
  114. var icon = iconCell.querySelector('.octicon-file-text');
  115.  
  116. if (icon.style.display == 'none')
  117. continue;
  118. icon.style.display = 'none';
  119. icon.insertAdjacentHTML('afterend',
  120. '<img class="wOxxOm-image-icon" src="https://raw.githubusercontent.com/' + m[1] + m[2] + '">');
  121. iconCell.classList.add('wOxxOm-image-td');
  122. }
  123. }
  124. }
  125.  
  126. function addFileTypeStyle(type) {
  127. filetypes[type] = true;
  128. for (var hash=0, i=0, len=type.length; i<len; i++)
  129. hash = ((hash << 5) - hash) + type.charCodeAt(i);
  130. hash = Math.abs(hash*config.colorSeed1 |0);
  131. var color = 'a[file-type="' + type + '"] { color: hsl(' +
  132. (hash%360) + ',' +
  133. (hash*config.colorSeed2%50 + 50 |0) + '%,' +
  134. (hash*config.colorSeed3%30 + 25 |0) + '%) }';
  135. styledom.appendChild(document.createTextNode(color));
  136. }
  137.  
  138. function userConfig(params) {
  139. Object.keys(params).forEach(function(k) {
  140. var def = params[k];
  141. var saved = GM_getValue(k, def);
  142. if (saved && saved != def)
  143. params[k] = saved;
  144. });
  145. return params;
  146. }