GreasyFork scripts icon (https)

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

目前為 2014-12-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        GreasyFork scripts icon (https)
// @namespace   wOxxOm.scripts
// @description On a script info page it shows its https:// icon from the script meta block
// @icon        http://icons.iconarchive.com/icons/custom-icon-design/mono-general-1/64/information-icon.png
// @version     1.0
// @author      wOxxOm
// @include     /^https://greasyfork\.org/(.*?/)?scripts/\d+.*$/
// @run-at      document-start
// @grant       GM_xmlhttpRequest
// @grant       GM_setValue
// @grant       GM_getValue
// ==/UserScript==

setTimeout(function() {
  var scriptID = location.href.match(/scripts\/(\d+)/)[1];
  var iconsrc = GM_getValue(scriptID);

  if (iconsrc && iconsrc.match(/^https:\/\//))
    addIcon(iconsrc);
  else
    GM_xmlhttpRequest({
      method: 'GET',
      url: location.href.replace(/(scripts\/\d+[^/]+)(\/.*)?$/,'$1/code/1.user.js'),
      timeout: 5000,
      onprogress: parseIcon,
      onload: parseIcon
    });

  function parseIcon(r) {
    if (!iconsrc) {
      var m = r.responseText.match(/\n\s*\/\/\s+@icon\s+(https:\/\/.*?)[\s\r\n]/);
      if (m)
        addIcon(m[1], r);
    }
  }

  function addIcon(url, response) {
    var h2 = document.querySelector('#script-info header h2');
    h2 ? __add(h2) : __wait();

    function __add(h2) {
      if (!h2)
        h2 = document.querySelector('#script-info header h2');
      console.log(h2);
      if (h2) {
        var div = h2.insertAdjacentHTML('afterbegin','<div style="\
            position: absolute;\
            left: 0;\
            width: calc(96px + 1ex);\
            display: inline-block;\
            text-align: right"></div>')
        var img = h2.firstChild.appendChild(document.createElement('img'));
        img.maxWidth = img.maxHeight = 64;
        img.style.setProperty('width', 'auto');
        img.style.setProperty('height', 'auto');
        img.src = url;

        iconsrc = url;
        GM_setValue(scriptID, iconsrc)

        if (response && (response.readyState != 4)) // not DONE loading
          response.abort();
      }
    }

    function __wait() {
      var ob = new MutationObserver(function(mutations){
        for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++) {
          if (m.target.localName == 'h2') {
            __add();
            ob.disconnect();
            return;
          }
        }
      });
      ob.observe(document, {subtree:true, childList:true});
    }
  }
}, 0);