GitHub file list beautifier

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

当前为 2017-12-07 提交的版本,查看 最新版本

  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. // @match https://github.com/*
  8. // @version 3.0.4
  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).appendChild(document.createElement('style')).textContent =
  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. var filetypes = {};
  37. var styledom = document.getElementById('wOxxOm-GHB');
  38.  
  39. var filesdom = document.getElementsByTagName('table');
  40. var ob = new MutationObserver(GHBsentry);
  41.  
  42. document.addEventListener('DOMContentLoaded', function() { beautify(filesdom[0]) });
  43. beautify(filesdom[0]);
  44. lazyObserve();
  45.  
  46. function GHBsentry(mutations) {
  47. if (!/^\/[^\/]+\/[^\/]+(\/tree\/.*)?$/.test(location.pathname)) {
  48. ob.disconnect();
  49. lazyObserve();
  50. return;
  51. }
  52. if (mutations.length == 1 && !mutations[0].addedNodes.length || !filesdom[0])
  53. return;
  54. //console.log([].concat.apply([], mutations.map(m => [].slice.call(m.addedNodes))));
  55. for (var i=0, ml=mutations.length, m; i<ml && (m=mutations[i]); i++) {
  56. for (var j=0, added=m.addedNodes, nl=added.length, n; j<nl && (n=added[j]); j++) {
  57. if (n.nodeType != 3) {
  58. if (n.localName == 'a' && /\/(tree|blob)\//.test(n.href) && !n.hasAttribute('file-type')
  59. || n.localName == 'div' && n.matches('.file-wrap, .new-discussion-timeline, .repo-file-upload-tree-target')
  60. ) {
  61. var table = n.closest('table') || n.querySelector('table');
  62. if (table) {
  63. beautify(table);
  64. ob.disconnect();
  65. lazyObserve();
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73.  
  74. function lazyObserve() {
  75. // postpone observing until the parser can breathe after the initial burst of activity during page load
  76. setTimeout(function() {
  77. beautify(filesdom[0]);
  78. ob.observe(document, {subtree:true, childList:true});
  79. }, 0);
  80. }
  81.  
  82. function beautify(container, links) {
  83. if (!container && !links)
  84. return;
  85. //console.log(container.innerHTML);
  86. if (!links) {
  87. var table = container.localName == 'table' ? container : container.querySelector('table');
  88. if (!table)
  89. return;
  90. links = table.getElementsByClassName('js-navigation-open');
  91. }
  92.  
  93. for (var a, i=0, len=links.length; i<len && (a=links[i++]); ) {
  94. if (!a.href)
  95. continue;
  96. var tr = a.closest('tr');
  97. if (tr && tr.querySelector('.octicon-file-directory')) {
  98. a.setAttribute('file-type', ':folder');
  99. continue;
  100. }
  101.  
  102. var ext = a.href.match(/\.(\w+)$/);
  103. ext = ext && ext[1] || ':empty';
  104. a.setAttribute('file-type', ext);
  105. if (!filetypes[ext])
  106. addFileTypeStyle(ext);
  107.  
  108. if (/^(png|jpe?g|bmp|gif|cur|ico)$/.test(ext)) {
  109. var m = a.href.match(/github\.com\/(.+?\/)blob\/(.*)$/);
  110. if (!m)
  111. continue;
  112. var iconCell = a.closest('.js-navigation-item').querySelector('.icon');
  113. var icon = iconCell.querySelector('.octicon-file-text');
  114.  
  115. if (icon.style.display == 'none')
  116. continue;
  117. icon.style.display = 'none';
  118. icon.insertAdjacentHTML('afterend',
  119. '<img class="wOxxOm-image-icon" src="https://raw.githubusercontent.com/' + m[1] + m[2] + '">');
  120. iconCell.classList.add('wOxxOm-image-td');
  121. }
  122. }
  123. }
  124.  
  125. function addFileTypeStyle(type) {
  126. filetypes[type] = true;
  127. for (var hash=0, i=0, len=type.length; i<len; i++)
  128. hash = ((hash << 5) - hash) + type.charCodeAt(i);
  129. hash = Math.abs(hash*config.colorSeed1 |0);
  130. var color = 'a[file-type="' + type + '"] { color: hsl(' +
  131. (hash%360) + ',' +
  132. (hash*config.colorSeed2%50 + 50 |0) + '%,' +
  133. (hash*config.colorSeed3%15 + 25 |0) + '%) }';
  134. styledom.appendChild(document.createTextNode(color));
  135. }
  136.  
  137. function userConfig(params) {
  138. var def = {
  139. iconSize: 24,
  140. colorSeed1: 13,
  141. colorSeed2: 1299721,
  142. colorSeed3: 179426453,
  143. };
  144. Object.keys(params).forEach(function(k) {
  145. var saved = GM_getValue(k, def[k]);
  146. if (saved && saved != def[k])
  147. params[k] = saved;
  148. });
  149. return params;
  150. }