GreasyFork script icon

On a script info page it shows its icon from the script meta block

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

  1. // ==UserScript==
  2. // @name GreasyFork script icon
  3. // @namespace wOxxOm.scripts
  4. // @description On a script info page it shows its icon from the script meta block
  5. // @icon https://icons.iconarchive.com/icons/custom-icon-design/mono-general-1/64/information-icon.png
  6. // @version 1.1.1
  7. // @author wOxxOm
  8. // @include /^https://greasyfork\.org/(.*?/)?scripts/\d+.*$/
  9. // @run-at document-start
  10. // @grant GM_xmlhttpRequest
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // ==/UserScript==
  14.  
  15. var scriptID = location.href.match(/scripts\/(\d+)/)[1];
  16. var iconsrc = GM_getValue(scriptID);
  17.  
  18. if (iconsrc && iconsrc.match(/^data:image|https:/))
  19. addIcon();
  20. else {
  21. GM_xmlhttpRequest({
  22. method: 'GET',
  23. url: location.href.replace(/(scripts\/\d+[^/]+)(\/.*)?$/,'$1/code/1.user.js'),
  24. timeout: 10000,
  25. onload: function (r) {
  26. var m = r.responseText.match(/\n\s*\/\/\s+@icon\s+(https?)(:\/\/.*?)[\s\r\n]/);
  27. if (!m)
  28. return;
  29.  
  30. if (m[1] == 'https')
  31. return addIcon(m[1]+m[2]);
  32. // download http icon and store it in script db if it's small
  33. GM_xmlhttpRequest({
  34. method: 'GET',
  35. url: m[1]+m[2],
  36. timeout: 10000,
  37. headers: {'Accept':'image/png,image/*;q=0.8,*/*;q=0.5'},
  38. overrideMimeType: 'text\/plain; charset=x-user-defined',
  39. onload: function(ri) {
  40. var rb = ri.response, rbl = rb.length;
  41. if (rbl > 100000) {
  42. console.log('Script icon exceeds 100k, ignoring');
  43. return;
  44. }
  45.  
  46. var ext = ri.finalUrl.substr(ri.finalUrl.lastIndexOf('.')+1).toLowerCase();
  47. var mime = ['png','bmp','gif'].indexOf(ext) >= 0 ? ext : ext.match(/'^jpe?g?/) ? 'jpeg' : ext=='ico' ? 'x-icon' : null;
  48. if (!mime)
  49. return;
  50.  
  51. var rb8 = new Uint8Array(rbl)
  52. for (var i=0; i<rbl; i++)
  53. rb8[i] = rb.charCodeAt(i);
  54. var rbs = String.fromCharCode.apply(null, rb8);
  55.  
  56. addIcon('data:image/' + mime + ';base64,' + btoa(rbs));
  57. }
  58. });
  59. }
  60. });
  61. }
  62.  
  63. function addIcon(url) {
  64. if (url)
  65. iconsrc = url;
  66. var h2 = document.querySelector('#script-info header h2');
  67. h2 ? __add(h2) : __wait();
  68.  
  69. function __add(h2) {
  70. if (!h2)
  71. if (!(h2 = document.querySelector('#script-info header h2')))
  72. return;
  73.  
  74. h2.insertAdjacentHTML('afterbegin','<div style="\
  75. position: absolute;\
  76. width: 80px;\
  77. margin-left: calc(-80px - 1ex);\
  78. display: inline-block;\
  79. text-align: right"></div>')
  80. var img = h2.firstChild.appendChild(document.createElement('img'));
  81. img.style.maxWidth = img.style.maxHeight = '64px';
  82. img.style.width = img.style.height = 'auto';
  83. img.src = iconsrc;
  84.  
  85. GM_setValue(scriptID, iconsrc);
  86. }
  87.  
  88. function __wait() {
  89. var ob = new MutationObserver(function(mutations){
  90. for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++) {
  91. if (m.target.localName == 'h2') {
  92. __add();
  93. ob.disconnect();
  94. return;
  95. }
  96. }
  97. });
  98. ob.observe(document, {subtree:true, childList:true});
  99. }
  100. }