GreasyFork script icon (https)

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

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

  1. // ==UserScript==
  2. // @name GreasyFork script icon (https)
  3. // @namespace wOxxOm.scripts
  4. // @description On a script info page it shows its https:// 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.0
  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. setTimeout(function() {
  16. var scriptID = location.href.match(/scripts\/(\d+)/)[1];
  17. var iconsrc = GM_getValue(scriptID);
  18.  
  19. if (iconsrc && iconsrc.match(/^https:\/\//))
  20. addIcon(iconsrc);
  21. else
  22. GM_xmlhttpRequest({
  23. method: 'GET',
  24. url: location.href.replace(/(scripts\/\d+[^/]+)(\/.*)?$/,'$1/code/1.user.js'),
  25. timeout: 5000,
  26. onprogress: parseIcon,
  27. onload: parseIcon
  28. });
  29.  
  30. function parseIcon(r) {
  31. if (!iconsrc) {
  32. var m = r.responseText.match(/\n\s*\/\/\s+@icon\s+(https:\/\/.*?)[\s\r\n]/);
  33. if (m)
  34. addIcon(m[1], r);
  35. }
  36. }
  37.  
  38. function addIcon(url, response) {
  39. var h2 = document.querySelector('#script-info header h2');
  40. h2 ? __add(h2) : __wait();
  41.  
  42. function __add(h2) {
  43. if (!h2)
  44. h2 = document.querySelector('#script-info header h2');
  45. console.log(h2);
  46. if (h2) {
  47. var div = h2.insertAdjacentHTML('afterbegin','<div style="\
  48. position: absolute;\
  49. left: 0;\
  50. width: calc(96px + 1ex);\
  51. display: inline-block;\
  52. text-align: right"></div>')
  53. var img = h2.firstChild.appendChild(document.createElement('img'));
  54. img.maxWidth = img.maxHeight = 64;
  55. img.style.setProperty('width', 'auto');
  56. img.style.setProperty('height', 'auto');
  57. img.src = url;
  58.  
  59. iconsrc = url;
  60. GM_setValue(scriptID, iconsrc)
  61.  
  62. if (response && (response.readyState != 4)) // not DONE loading
  63. response.abort();
  64. }
  65. }
  66.  
  67. function __wait() {
  68. var ob = new MutationObserver(function(mutations){
  69. for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++) {
  70. if (m.target.localName == 'h2') {
  71. __add();
  72. ob.disconnect();
  73. return;
  74. }
  75. }
  76. });
  77. ob.observe(document, {subtree:true, childList:true});
  78. }
  79. }
  80. }, 0);