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. icon.style.display = 'hidden';
  26.  
  27. var img = document.createElement('img');
  28. td.insertBefore(img, icon);
  29. td.removeChild(icon);
  30.  
  31. img.style.width = img.style.height = '16px';
  32. img.src = 'https://raw.githubusercontent.com/' + r[1] + r[2];
  33. }
  34. }
  35.  
  36. if (document.body)
  37. iconify(document.body);
  38.  
  39. var ob = new MutationObserver(function(mutations){
  40. for (var m, i=0; i<mutations.length && (m=mutations[i++]); )
  41. if (m.target.id == 'js-repo-pjax-container' || (m.target.className == 'files' && m.target.localName == 'table'))
  42. for (var nn=m.addedNodes, n, j=0; j<nn.length && (n=nn[j++]); )
  43. if (n.nodeType == Node.ELEMENT_NODE)
  44. iconify(n);
  45. });
  46. ob.observe(document, {subtree:true, childList:true});