GreasyFork script icon

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

As of 2020-02-17. See the latest version.

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