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