GitHub - show images in repo as icons

Show images in Github repositories as 16x16 icons

目前为 2014-10-24 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub - show images in repo as icons
  3. // @description Show images in Github repositories as 16x16 icons
  4. // @namespace wOxxOm.scripts
  5. // @include https://github.com/*
  6. // @match https://github.com/*
  7. // @version 1
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. function iconify(n) {
  13. var aa = (n.className == 'js-directory-link') ? [n] : n.getElementsByClassName('js-directory-link');
  14. for (var a, i=0; i<aa.length && (a=aa[i++]); ) {
  15. var r = a.href.match(/github\.com\/(.+?\/)blob\/([^\/]+\/.+?\.(?:png|jpg|jpeg|bmp|gif|cur|ico))$/);
  16. if (!r)
  17. continue;
  18.  
  19. var td = a.parentNode.parentNode;
  20. if (td.localName != 'td')
  21. continue;
  22. td = td.previousElementSibling;
  23.  
  24. var icon = td.firstElementChild;
  25. var img = document.createElement('img');
  26. td.insertBefore(img, icon);
  27. td.removeChild(icon);
  28.  
  29. img.style.width = img.style.height = '16px';
  30. img.src = 'https://raw.githubusercontent.com/' + r[1] + r[2];
  31. }
  32. }
  33.  
  34. if (document.body)
  35. iconify(document.body);
  36.  
  37. var ob = new MutationObserver(function(mutations){
  38. for (var m, i=0; i<mutations.length && (m=mutations[i++]); )
  39. if (m.target.id == 'js-repo-pjax-container' || (m.target.className == 'files' && m.target.localName == 'table'))
  40. for (var nn=m.addedNodes, n, j=0; j<nn.length && (n=nn[j++]); )
  41. if (n.nodeType == Node.ELEMENT_NODE)
  42. iconify(n);
  43. });
  44. ob.observe(document, {subtree:true, childList:true});