Bilibili-Evolved/min/gui-settings.min.js
2020-10-28 20:46:23 +08:00

1 line
6.7 KiB
JavaScript

(()=>{return(e,t)=>{const{ThemeColors:n}=t.import("theme-colors");const{SettingsSearch:i}=t.import("settings-search");const{Validator:s}=t.import("text-validate");let o=[];let c=[];let a=[];function r(e){let t=e.nextElementSibling;const n=[];while(t!==null&&!t.classList.contains("category")){n.push(t);t=t.nextElementSibling}return n}function l(){a.forEach(t=>t.value=e[t.getAttribute("key")]);c.forEach(t=>t.checked=e[t.getAttribute("key")])}function d(){document.querySelector(".gui-settings-mask").addEventListener("click",()=>{document.querySelectorAll(".gui-settings-widgets-box,.gui-settings-box,.gui-settings-mask,.bilibili-evolved-about").forEach(e=>e.classList.remove("opened"))});a.forEach(t=>{t.setAttribute("placeholder",e[t.getAttribute("key")])});document.querySelectorAll(".gui-settings-content ul li.category").forEach(e=>{e.addEventListener("click",e=>{const t=document.querySelector(".gui-settings-search");if(t.value!==""){t.value="";raiseEvent(t,"input")}e.currentTarget.classList.toggle("folded");r(e.currentTarget).forEach(e=>e.classList.toggle("folded"))})});document.querySelectorAll(".gui-settings-dropdown>input").forEach(e=>{e.addEventListener("click",t=>{t.target.parentElement.classList.toggle("opened");const n=i=>{const s=i.target;const o=dq(`li[data-key=${e.getAttribute("key")}]`);console.log(o,e.getAttribute("key"),s);if(o.contains(s)&&o!==s){return}t.target.parentElement.classList.remove("opened");document.body.removeEventListener("click",n)};document.body.addEventListener("click",n)})});dqa(".gui-settings-header .operation").forEach(t=>{t.addEventListener("click",t=>{if(t.target.classList.contains("export")){const t=document.createElement("a");t.style.display="none";const n=URL.createObjectURL(new Blob([JSON.stringify(_.omit(e,"cache"))]));t.href=n;t.download="bilibili-evolved-settings.json";document.body.insertAdjacentElement("beforeend",t);t.click();URL.revokeObjectURL(n);t.remove()}else if(t.target.classList.contains("import")){const t=document.createElement("input");t.style.display="none";t.type="file";t.accept=".json,text/json";document.body.insertAdjacentElement("beforeend",t);t.addEventListener("change",async()=>{try{const n=t.files[0];const i=JSON.parse(await new Response(n).text());for(const[t,n]of Object.entries(i)){if(t in e&&t!=="cache"){e[t]=n}}Toast.success("已成功导入设置, 部分设置需要刷新后生效.","导入设置",3e3)}catch(e){Toast.error("选择的设置文件无效.","导入设置",3e3)}finally{t.remove()}},{once:true});t.click()}})})}function u(){c.forEach(t=>{t.addEventListener("change",()=>{const n=t.getAttribute("key");const i=t.checked;e[n]=i})});a.forEach(t=>{t.addEventListener("change",()=>{const n=t.getAttribute("key");const i=s.getValidator(n).validate(t.value);e[n]=i;t.value=i})})}function g(){const e=o.map(e=>[e.getAttribute("dependencies").split(" ").map(e=>o.find(t=>t.getAttribute("key")===e)),e]);const t=e=>e.nodeName.toUpperCase()==="LI"?e:t(e.parentElement);e.forEach(([e,n])=>{if(e[0]===undefined){return}const i=()=>{if(e.every(e=>e.checked)){t(n).classList.remove("disabled")}else{t(n).classList.add("disabled")}};e.forEach(e=>e.addEventListener("change",i));i()})}function f(){if(typeof offlineData!=="undefined"){}}const m=["playerFocus","outerWatchlater","quickFavorite"];const p=()=>{if(m.some(t=>e[t])){m.forEach(t=>{e[t]=false});Toast.info(`\n<div>为避免b站播放器改版导致网站无法正常使用, 以下功能已自动关闭并禁用:\n<span>自动定位到播放器</span> <span>外置稍后再看</span> <span>启用快速收藏</span>\n详情见<a target="_blank" href="https://github.com/the1812/Bilibili-Evolved/issues/1076" class="link">讨论区</a>, 这些功能将在恢复后再解除禁用.\n若当前页面是视频页面且出现问题, 刷新即可恢复正常.</div>\n`.trim(),"通知")}};function y(){if(window.devicePixelRatio===1){o.find(e=>e.getAttribute("key")==="harunaScale").disabled=true;o.find(e=>e.getAttribute("key")==="imageResolution").disabled=true;e.harunaScale=false;e.imageResolution=false}c.filter(e=>m.includes(e.getAttribute("key"))).forEach(e=>e.disabled=true)}function b(){for(const[e,t]of Object.entries(Resource.displayNames)){const n=o.find(t=>t.getAttribute("key")===e);if(!n){continue}switch(n.type){case"checkbox":n.nextElementSibling.nextElementSibling.innerHTML=t;break;case"text":const e=n.parentElement;if(e.classList.contains("gui-settings-textbox-container")){n.previousElementSibling.innerHTML=t}else if(e.classList.contains("gui-settings-dropdown")){e.previousElementSibling.innerHTML=t}break;default:break}}}(async()=>{t.applyStyle("guiSettingsStyle");t.applyImportantStyle("iconsStyle");document.body.classList.add("round-corner");const s=document.body&&unsafeWindow.parent.window!==unsafeWindow;if(s){document.querySelector(".gui-settings-icon-panel").style.display="none"}addSettingsListener("guiSettingsDockSide",e=>{document.body.classList.toggle("gui-settings-dock-right",e==="右侧")},true);addSettingsListener("autoHideSideBar",e=>{document.body.classList.toggle("gui-settings-auto-hide-side-bar",e)},true);addSettingsListener("elegantScrollbar",e=>{document.documentElement.classList.toggle("elegant-scrollbar",e)},true);const r=t.data.guiSettingsHtml.text;document.body.insertAdjacentHTML("beforeend",r);const{style:m}=await t.importAsync("mdi");if(!m){document.body.insertAdjacentHTML("afterbegin",`<link rel="stylesheet" href="//cdn.materialdesignicons.com/3.6.95/css/materialdesignicons.min.css">`)}const h=document.querySelector(".widgets-container");const v=h.querySelector(".empty-tip");Observer.childList(h,()=>{if(h.childElementCount<=1){v.classList.add("show")}else{v.classList.remove("show")}});const E=document.querySelectorAll(".gui-settings-widgets-box,.gui-settings-box");const L=document.querySelector(".gui-settings-icon-panel");p();L.addEventListener("mouseover",async()=>{const{loadTooltip:s}=await t.importAsync("settings-tooltip.loader");await s();await t.applyDropdownOptions();t.applyWidgets();raiseEvent(L,"be:load");const r=dq(".bilibili-evolved-about");if(r){raiseEvent(r,"be:about-load")}(new n).setupDom();E.forEach(e=>e.classList.add("loaded"));o=[...document.querySelectorAll("input[key]")];c=o.filter(e=>e.type==="checkbox");a=o.filter(e=>e.type==="text"&&!e.parentElement.classList.contains("gui-settings-dropdown"));d();f();l();g();u();y();b();dq(".script-info .version").textContent=scriptVersion+" v"+GM.info.script.version;(async()=>{const t=dq(".script-info .content-hash");if(Object.keys(e.cache).length===0){t.remove();return}const n=async e=>{const t=(new TextEncoder).encode(e);const n=await crypto.subtle.digest("SHA-256",t);const i=Array.from(new Uint8Array(n));const s=i.map(e=>e.toString(16).padStart(2,"0")).join("");return s};const i=await n(JSON.stringify(e.cache));t.textContent=`内容包: ${i.substring(0,7)}`})();new i},{once:true})})()}})();