GreasyFork scripts icon (https)

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

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

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