class FetchUserjs { constructor() { this.homeUrl = 'https://greasyfork.org/zh-CN/scripts/24508'; this.api = 'https://greasyfork.org/en/scripts/by-site/{host}.json'; this.host = location.host.split('.').splice(-2).join('.'); this.showTime = 10; this.quietKey = 'jae_fetch_userjs_quiet'; this.cacheKey = 'jae_fetch_userjs_cache'; this.tplBox = '
'; } /* Nano Templates - https://github.com/trix/nano */ nano(template, data) { return template.replace(/\{([\w\.]*)\}/g, function(str, key) { let keys = key.split("."), v = data[keys.shift()]; for (let i = 0, l = keys.length; i < l; i++) v = v[keys[i]]; return (typeof v !== "undefined" && v !== null) ? v : ""; }); } getJSON(url, callback) { GM_xmlhttpRequest({ method: 'GET', url: url, onload: (res) => { let json = JSON.parse(res.responseText); callback(json); } }); } getData(host, callback) { let data = sessionStorage.getItem(this.cacheKey); if (data) { data = JSON.parse(data); callback(data); } else { let api = this.nano(this.api, { host: this.host }); this.getJSON(api, (json) => { sessionStorage.setItem(this.cacheKey, JSON.stringify(json)); callback(json); }); } } setSize(w, h) { $('.jae-userscript').css({ width: w, height: h }) } addEventListener(eventName, handler) { document.getElementById('jae_userscript_box').addEventListener(eventName, handler) } bindEvent() { this.timeId = setTimeout(() => { $('#jae_userscript_box').remove(); }, this.showTime * 1000); this.addEventListener('max', () => { this.setSize(860, 492) $('.jae-userscript').addClass('jae-userscript-shadow') clearTimeout(this.timeId); }) this.addEventListener('min', () => { setTimeout(() => { $('.jae-userscript').removeClass('jae-userscript-shadow') this.setSize(370, 56) }, 500) }) this.addEventListener('close', () => { sessionStorage.setItem(this.quietKey, 1); $('#jae_userscript_box').remove(); }) } execFrameJs(frameWindow) { let uiJs = GM_getResourceText('uiJs'); return function(jsStr) { frameWindow.eval(jsStr); }.call(frameWindow, uiJs); } get isQuiet() { let quiet = sessionStorage.getItem(this.quietKey); return quiet ? true : false; } render() { this.isQuiet || this.getData(this.host, (json) => { if (json.length) { $('body').append(this.tplBox); let ui = GM_getResourceText('ui'); let dom = document.getElementsByClassName('jae-userscript')[0] var tpl = ''; dom.innerHTML = tpl; var iframeDom = dom.children[0]; iframe.write(iframeDom, ui); this.execFrameJs(jaeFetchUserJSFrame.window); this.bindEvent(); } }); } } ljs.exec(['jQuery', 'iframe'], () => { let fu = new FetchUserjs(); fu.render(); });