KameSame Open Framework - Settings module

Settings module for KameSame Open Framework

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greasyfork.org/scripts/451521/1126911/KameSame%20Open%20Framework%20-%20Settings%20module.js

// ==UserScript==
// @name        KameSame Open Framework - Settings module
// @version     0.4
// @author      Robin Findley, Timberpile
// @description Settings module for KameSame Open Framework
// @homepage    https://github.com/timberpile/kamesame-open-framework#readme
// @supportURL  https://github.com/timberpile/kamesame-open-framework/issues
// @match       http*://*.kamesame.com/*
// @copyright   2022+, Robin Findley, Timberpile
// @namespace   timberpile@proton.me
// @run-at      document-start
// @grant       none
// @license     MIT
// ==/UserScript==

(()=>{"use strict";var __webpack_modules__={767:(__unused_webpack_module,__webpack_exports__,__webpack_require__)=>{__webpack_require__.d(__webpack_exports__,{Cx:()=>getSettingValue,wB:()=>configToHTML,xI:()=>setSettingValue});const ksof=()=>window.ksof,configToHTML=t=>{if(t.configList={},!ksof().settings)return"";const e=(t,e,i)=>`<div id="${t}" class="ksof_stabs"><ul>${e.join("")}</ul>${i.join("")}</div>`,i=(t,e,i)=>`<div class="row${e?" full":""}"${c(i)}>${t}</div>`,s=t=>`<div class="left">${t}</div>`,n=t=>`<div class="right">${t}</div>`,o=t=>t.replace(/[<>]/g,(t=>"<"==t?"&lt":">"==t?"&gt":"")),a=t=>t.replace(/"/g,"&quot"),c=t=>t?` title="${t.replace(/"/g,"&quot")}"`:"",l=(g,d,p)=>{if("string"!=typeof d.type)return"";const f=`${t.cfg.scriptId}_${g}`;let h,u,b="",m="";const _=t=>"string"!=typeof t.label?"":s(`<label for="${f}">${t.label}</label>`),v=d.type;if("tabset"==v){const t=d;for(h in u={},t.content)m+=l(h,t.content[h],u);u.tabs&&u.pages&&(b=e(f,u.tabs,u.pages))}else if("page"==v){const t=d;for(h in"object"!=typeof t.content&&(t.content={}),p.tabs||(p.tabs=[]),p.pages||(p.pages=[]),p.tabs.push(`<li id="${f}_tab"${c(t.hoverTip)}><a href="#${f}">${t.label}</a></li>`),u={},t.content)m+=l(h,t.content[h],u);u.tabs&&u.pages&&(b=e(f,u.tabs,u.pages)),p.pages.push(`<div id="${f}">${b}${m}</div>`),p.isPage=!0,b=""}else if("group"==v){const t=d;for(h in"object"!=typeof t.content&&(t.content={}),u={},t.content)m+=l(h,t.content[h],u);u.tabs&&u.pages&&(b=e(f,u.tabs,u.pages)),b=`<fieldset id="${f}" class="ksof_group"><legend>${t.label}</legend>${b}${m}</fieldset>`}else if("dropdown"==v){const e=d;t.configList[g]=e;let s=getSettingValue(t,r,g);for(h in void 0===s&&(s=void 0!==e.default?e.default:Object.keys(e.content)[0],setSettingValue(t,r,g,s)),b=`<select id="${f}" name="${g}" class="setting"${c(e.hoverTip)}>`,e.content)b+=`<option name="${h}">${o(e.content[h])}</option>`;b+="</select>",b=_(e)+n(b),b=i(b,e.fullWidth,e.hoverTip)}else if("list"==v){const e=d;t.configList[g]=e;let s=getSettingValue(t,r,g);void 0===s&&(void 0!==e.default?s=e.default:!0===e.multi?(s={},Object.keys(e.content).forEach((t=>{s[t]=!1}))):s=Object.keys(e.content)[0],setSettingValue(t,r,g,s));let a=` size="${e.size||Object.keys(e.content).length||4}"`;for(h in!0===e.multi&&(a+=" multiple"),b=`<select id="${f}" name="${g}" class="setting list"${a}${c(e.hoverTip)}>`,e.content)b+=`<option name="${h}">${o(e.content[h])}</option>`;b+="</select>",b=_(e)+n(b),b=i(b,e.fullWidth,e.hoverTip)}else if("checkbox"==v){const e=d;t.configList[g]=e,b=_(e);let s=getSettingValue(t,r,g);void 0===s&&(s=e.default||!1,setSettingValue(t,r,g,s)),b+=n(`<input id="${f}" class="setting" type="checkbox" name="${g}">`),b=i(b,e.fullWidth,e.hoverTip)}else if("input"==v){const e=d,s=e.subtype||"text";t.configList[g]=e,b+=_(e);let o=getSettingValue(t,r,g);if(void 0===o){const i="number"===e.subtype;o=e.default||(i?0:""),setSettingValue(t,r,g,o)}b+=n(`<input id="${f}" class="setting" type="${s}" name="${g}"${e.placeholder?` placeholder="${a(e.placeholder)}"`:""}>`),b=i(b,e.fullWidth,e.hoverTip)}else if("number"==v){const e=d,s=e.type;t.configList[g]=e,b+=_(e);let o=getSettingValue(t,r,g);if(void 0===o){const i="number"===e.type;o=e.default||(i?0:""),setSettingValue(t,r,g,o)}b+=n(`<input id="${f}" class="setting" type="${s}" name="${g}"${e.placeholder?` placeholder="${a(e.placeholder)}"`:""}>`),b=i(b,e.fullWidth,e.hoverTip)}else if("text"==v){const e=d,s=e.type;t.configList[g]=e,b+=_(e);let o=getSettingValue(t,r,g);void 0===o&&(o=e.default||"",setSettingValue(t,r,g,o)),b+=n(`<input id="${f}" class="setting" type="${s}" name="${g}"${e.placeholder?` placeholder="${a(e.placeholder)}"`:""}>`),b=i(b,e.fullWidth,e.hoverTip)}else if("color"==v){const e=d;t.configList[g]=e,b+=_(e);let s=getSettingValue(t,r,g);void 0===s&&(s=e.default||"#000000",setSettingValue(t,r,g,s)),b+=n(`<input id="${f}" class="setting" type="color" name="${g}">`),b=i(b,e.fullWidth,e.hoverTip)}else if("button"==v){const e=d;t.configList[g]=e,b+=_(e);const s=o(e.text||"Click");b+=n(`<button type="button" class="setting" name="${g}">${s}</button>`),b=i(b,e.fullWidth,e.hoverTip)}else if("divider"==v)b+="<hr>";else if("section"==v)b+=`<section>${d.label||""}</section>`;else if("html"==v){const t=d;switch(b+=_(t),b+=t.html,t.wrapper){case"row":b=i(b,void 0,t.hoverTip);break;case"left":b=s(b);break;case"right":b=n(b)}}return b};let r=ksof().settings[t.cfg.scriptId];void 0===r&&(ksof().settings[t.cfg.scriptId]=r={});let g="";const d={},p=`${t.cfg.scriptId}_dialog`;for(const e in t.cfg.content)g+=l(e,t.cfg.content[e],d);return d.tabs&&d.pages&&(g=e(p,d.tabs,d.pages)+g),`<form>${g}</form>`},getSettingValue=(context,base,name)=>{const item=context.configList[name],evaluate=void 0!==item.path,path=item.path||name;try{return evaluate?eval(path.replace(/@/g,"base.")):base[path]}catch(t){return}},setSettingValue=(context,base,name,value)=>{const item=context.configList[name],evaluate=void 0!==item.path,path=item.path||name;try{if(!evaluate)return base[path]=value;let depth=0,newPath="",param="",c;for(let idx=0;idx<path.length;idx++)c=path[idx],"["===c?0==depth++?(newPath+="[",param=""):param+="[":"]"===c?0==--depth?newPath+=`${JSON.stringify(eval(param))}]`:param+="]":("@"===c&&(c="base."),0===depth?newPath+=c:param+=c);eval(`${newPath}=value`)}catch(t){return}}}},__webpack_module_cache__={};function __webpack_require__(t){var e=__webpack_module_cache__[t];if(void 0!==e)return e.exports;var i=__webpack_module_cache__[t]={exports:{}};return __webpack_modules__[t](i,i.exports,__webpack_require__),i.exports}__webpack_require__.d=(t,e)=>{for(var i in e)__webpack_require__.o(e,i)&&!__webpack_require__.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},__webpack_require__.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var __webpack_exports__={};(()=>{var t=__webpack_require__(767);const e=()=>window.ksof;class i{cfg;configList;keepSettings;reversions;#t;background;constructor(t){this.cfg=t,this.configList={},this.#t=$(),this.background=s()}static save(t){const i="string"==typeof t?t:t.cfg.scriptId,s=e().settings[i];return s?e().fileCache.save(`ksof.settings.${i}`,s):Promise.resolve("")}save(){return i.save(this)}static async load(t,i){const s="string"==typeof t?t:t.cfg.scriptId,n=t=>(e().settings[s]=i?o(i,t):t,e().settings[s]);try{return n(await e().fileCache.load(`ksof.settings.${s}`))}catch(t){return n.call(null,{})}}load(t){return i.load(this,t)}saveBtn(){const t=this.cfg.scriptId,i=e().settings[t];if(i){const t=this.#t.find(".ui-tabs-active").toArray().map((t=>`#${t.attributes.getNamedItem("id")?.value||""}`));t.length>0&&(i.ksofActiveTabs=t)}void 0!==this.cfg.autosave&&!0!==this.cfg.autosave||this.save(),this.cfg.onSave&&this.cfg.onSave(e().settings[this.cfg.scriptId]),e().trigger("ksof.settings.save"),this.keepSettings=!0,this.#t.dialog("close")}cancel(){this.#t.dialog("close"),"function"==typeof this.cfg.onCancel&&this.cfg.onCancel(e().settings[this.cfg.scriptId])}open(){if("ready"!=e().getState("ksof.Settings"))return;if(this.#t.length>0)return;n(),!1!==this.cfg.background&&this.background.open(),this.#t=$(`<div id="ksofs_${this.cfg.scriptId}" class="ksof_settings" style="display:none"></div>`),this.#t.html((0,t.wB)(this));let i=500;window.innerWidth<510&&(i=280,this.#t.addClass("narrow")),this.#t.dialog({title:this.cfg.title,buttons:[{text:"Save",click:this.saveBtn.bind(this)},{text:"Cancel",click:this.cancel.bind(this)}],width:i,maxHeight:document.body.clientHeight,modal:!1,autoOpen:!1,appendTo:"#ksof_ds",resize:((t,e)=>{const i=this.#t.hasClass("narrow");i&&e.size.width>=510?this.#t.removeClass("narrow"):!i&&e.size.width<490&&this.#t.addClass("narrow")}).bind(this),close:()=>{this.close(!1)}}),$(this.#t.dialog("widget")).css("position","fixed"),this.#t.parent().addClass("ksof_settings_dialog"),$(".ksof_stabs").tabs({activate:(()=>{const t=$(this.#t.dialog("widget"));(t.outerHeight()||0)+t.position().top>document.body.clientHeight&&this.#t.dialog("option","maxHeight",document.body.clientHeight)}).bind(null)});const s=e().settings[this.cfg.scriptId];if(s&&s.ksofActiveTabs instanceof Array){const t=s.ksofActiveTabs;for(let e=0;e<t.length;e++){const i=$(t[e]);i.closest(".ui-tabs").tabs({active:i.index()})}}this.#t.dialog("open"),this.#t.find(".setting[multiple]").on("mousedown",(t=>{if(0!=t.button)return!0;const e=$(t.currentTarget),i=t.currentTarget.scrollTop;return t.target.selected=!t.target.selected,setTimeout((()=>{t.currentTarget.scrollTop=i,e.focus()}),0),this.#e(t)}).bind(this)),this.#t.find(".setting").on("change",this.#e.bind(this)),this.#t.find("form").on("submit",(()=>!1)),this.#t.find("button.setting").on("click",(t=>{const e=t.target.attributes.name.value,i=this.configList[e];if("button"==i.type){const s=i;s.onClick.call(t,e,s,this.#e.bind(this,t))}}).bind(this)),"function"==typeof this.cfg.preOpen&&this.cfg.preOpen(this.#t),this.reversions=o({},e().settings[this.cfg.scriptId]),this.refresh()}#e(i){const s=$(i.currentTarget),n=s.attr("name");if(!n)return!1;const o=this.configList[n];let a;"dropdown"==o.type?a=s.find(":checked").attr("name"):"list"==o.type?!0===o.multi?(a={},s.find("option").each(((t,e)=>{const i=e.getAttribute("name")||`#${e.index}`;a[i]=e.selected}))):a=s.find(":checked").attr("name"):"input"==o.type?"number"===o.subtype&&(a=Number(s.val())):a="checkbox"==o.type?s.is(":checked"):"number"==o.type?Number(s.val()):s.val();let c={valid:!0,msg:""};{const t=o;if(t.validate){const e=t.validate.call(i.target,a,t);"boolean"==typeof e?c={valid:e,msg:""}:"string"==typeof e&&(c={valid:!1,msg:e})}}if("number"==o.type){const t=o;t.min&&Number(a)<t.min?(c.valid=!1,0===c.msg.length&&("number"==typeof t.max?c.msg=`Must be between ${t.min} and ${t.max}`:c.msg=`Must be ${t.min} or higher`)):t.max&&Number(a)>t.max&&(c.valid=!1,0===c.msg.length&&("number"==typeof t.min?c.msg=`Must be between ${t.min} and ${t.max}`:c.msg=`Must be ${t.max} or lower`))}else if("text"==o.type){const t=o;void 0!==t.match&&null===a.match(t.match)&&(c.valid=!1,0===c.msg.length&&(c.msg="Invalid value"))}const l=s.closest(".right");l.find(".note").remove(),"string"==typeof c.msg&&c.msg.length>0&&l.append(`<div class="note${c.valid?"":" error"}">${c.msg}</div>`),c.valid?s.removeClass("invalid"):s.addClass("invalid");const r=this.cfg.scriptId,g=e().settings[r];if(c.valid){const e=o;(0,t.xI)(this,g,n,a),e.onChange&&e.onChange.call(i.target,n,a,e),this.cfg.onChange&&this.cfg.onChange.call(i.target,n,a,e),!0===e.refreshOnChange&&this.refresh()}return!1}close(t){this.keepSettings||!0===t||(e().settings[this.cfg.scriptId]=o({},this.reversions||{}),delete this.reversions),delete this.keepSettings,this.#t.dialog("destroy"),this.#t=$(),!1!==this.cfg.background&&this.background.close(),"function"==typeof this.cfg.onClose&&this.cfg.onClose(e().settings[this.cfg.scriptId])}refresh(){const i=this.cfg.scriptId,s=e().settings[i];for(const e in this.configList){const n=this.#t.find(`#${i}_${e}`),o=this.configList[e],a=(0,t.Cx)(this,s,e);"dropdown"==o.type?n.find(`option[name="${a}"]`).prop("selected",!0):"list"==o.type?!0===o.multi?n.find("option").each(((t,e)=>{const i=e.getAttribute("name")||`#${e.index}`;e.selected=a[i]})):n.find(`option[name="${a}"]`).prop("selected",!0):"checkbox"==o.type?n.prop("checked",a):n.val(a)}"function"==typeof this.cfg.onRefresh&&this.cfg.onRefresh(e().settings[this.cfg.scriptId])}}const s=()=>({open:()=>{const t=n();let e=t.find("> #ksofs_bkgd");0===e.length&&(e=$('<div id="ksofs_bkgd" refcnt="0"></div>'),t.prepend(e));const i=Number(e.attr("refcnt"));e.attr("refcnt",i+1)},close:()=>{const t=$("#ksof_ds > #ksofs_bkgd");if(0===t.length)return;const e=Number(t.attr("refcnt"));e<=0||t.attr("refcnt",e-1)}}),n=()=>{let t=$("#ksof_ds");return 0===t.length&&(t=$('<div id="ksof_ds"></div></div>'),$("body").prepend(t),$("#ksof_ds").on("keydown keyup keypress",".ksof_settings_dialog",(t=>{t.stopPropagation()}))),t},o=(...t)=>{const e={},i=(t,e)=>{for(const s in e)if("object"==typeof e[s]&&null!==e[s]){const n=e[s];Array.isArray(n)?t[s]=n.slice():(t[s]=t[s]||{},i(t[s],n))}else t[s]=e[s];return t};for(const s in t)i(e,t[s]);return e};(async t=>{const e=window.ksof;e.Settings=(()=>{const t=t=>new i(t);return t.save=t=>i.save(t),t.load=(t,e)=>i.load(t,e),t.background=s(),t})(),e.settings={};const n=e.supportFiles["jqui_ksmain.css"];e.include("Jquery"),await e.ready("document, Jquery"),await e.loadScript(e.supportFiles["jquery_ui.js"],!0),await e.loadCSS(n,!0);try{delete $.fn.autocomplete}catch(t){}setTimeout((()=>{e.setState("ksof.Settings","ready")}),0)})()})()})();