GreasyFork scripts 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 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 http://icons.iconarchive.com/icons/custom-icon-design/mono-general-1/64/information-icon.png
  6. // @version 1.0.2
  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. console.log(h2);
  45. if (h2) {
  46. var div = h2.insertAdjacentHTML('afterbegin','<div style="\
  47. position: absolute;\
  48. width: 80px;\
  49. margin-left: calc(-80px - 1ex);\
  50. display: inline-block;\
  51. text-align: right"></div>')
  52. var img = h2.firstChild.appendChild(document.createElement('img'));
  53. img.maxWidth = img.maxHeight = 64;
  54. img.style.setProperty('width', 'auto');
  55. img.style.setProperty('height', 'auto');
  56. img.src = url;
  57.  
  58. iconsrc = url;
  59. GM_setValue(scriptID, iconsrc)
  60.  
  61. if (response && (response.readyState != 4)) // not DONE loading
  62. response.abort();
  63. }
  64. }
  65.  
  66. function __wait() {
  67. var ob = new MutationObserver(function(mutations){
  68. for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++) {
  69. if (m.target.localName == 'h2') {
  70. __add();
  71. ob.disconnect();
  72. return;
  73. }
  74. }
  75. });
  76. ob.observe(document, {subtree:true, childList:true});
  77. }
  78. }