diff --git a/package.json b/package.json index 65a2768..e8a03ca 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "autoload.js", "type": "module", "scripts": { - "build": "rollup -c rollup.config.js -f iife | terser > waifu-tips.js" + "build": "rollup -c rollup.config.js -f iife | terser -c -m > waifu-tips.js" }, "repository": { "type": "git", diff --git a/waifu-tips.js b/waifu-tips.js index 459779e..6d6e4fd 100644 --- a/waifu-tips.js +++ b/waifu-tips.js @@ -1 +1 @@ -(function(){"use strict";var fa_comment='\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e';var fa_paper_plane='\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e';var fa_user_circle='\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e';var fa_street_view='\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e';var fa_camera_retro='\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e';var fa_info_circle='\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e';var fa_xmark='\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e';function loadWidget(config){let{apiPath:apiPath,cdnPath:cdnPath}=config;let useCDN=false,modelList;if(typeof cdnPath==="string"){useCDN=true;if(!cdnPath.endsWith("/"))cdnPath+="/"}else if(typeof apiPath==="string"){if(!apiPath.endsWith("/"))apiPath+="/"}else{console.error("Invalid initWidget argument!");return}localStorage.removeItem("waifu-display");sessionStorage.removeItem("waifu-text");document.body.insertAdjacentHTML("beforeend",`
\n\t\t\t
\n\t\t\t\n\t\t\t
\n\t\t
`);setTimeout((()=>{document.getElementById("waifu").style.bottom=0}),0);function randomSelection(obj){return Array.isArray(obj)?obj[Math.floor(Math.random()*obj.length)]:obj}let userAction=false,userActionTimer,messageTimer,messageArray=["好久不见,日子过得好快呢……","大坏蛋!你都多久没理人家了呀,嘤嘤嘤~","嗨~快来逗我玩吧!","拿小拳拳锤你胸口!","记得把小家加入 Adblock 白名单哦!"];window.addEventListener("mousemove",(()=>userAction=true));window.addEventListener("keydown",(()=>userAction=true));setInterval((()=>{if(userAction){userAction=false;clearInterval(userActionTimer);userActionTimer=null}else if(!userActionTimer){userActionTimer=setInterval((()=>{showMessage(randomSelection(messageArray),6e3,9)}),2e4)}}),1e3);(function registerEventListener(){const tools={hitokoto:{icon:fa_comment,callback:showHitokoto},asteroids:{icon:fa_paper_plane,callback:()=>{if(window.Asteroids){if(!window.ASTEROIDSPLAYERS)window.ASTEROIDSPLAYERS=[];window.ASTEROIDSPLAYERS.push(new Asteroids)}else{const script=document.createElement("script");script.src="https://fastly.jsdelivr.net/gh/stevenjoezhang/asteroids/asteroids.js";document.head.appendChild(script)}}},"switch-model":{icon:fa_user_circle,callback:loadOtherModel},"switch-texture":{icon:fa_street_view,callback:loadRandModel},photo:{icon:fa_camera_retro,callback:()=>{showMessage("照好了嘛,是不是很可爱呢?",6e3,9);Live2D.captureName="photo.png";Live2D.captureFrame=true}},info:{icon:fa_info_circle,callback:()=>{open("https://github.com/stevenjoezhang/live2d-widget")}},quit:{icon:fa_xmark,callback:()=>{localStorage.setItem("waifu-display",Date.now());showMessage("愿你有一天能与重要的人重逢。",2e3,11);document.getElementById("waifu").style.bottom="-500px";setTimeout((()=>{document.getElementById("waifu").style.display="none";document.getElementById("waifu-toggle").classList.add("waifu-toggle-active")}),3e3)}}};if(!Array.isArray(config.tools)){config.tools=Object.keys(tools)}for(let tool of config.tools){if(tools[tool]){const{icon:icon,callback:callback}=tools[tool];document.getElementById("waifu-tool").insertAdjacentHTML("beforeend",`${icon}`);document.getElementById(`waifu-tool-${tool}`).addEventListener("click",callback)}}const devtools=()=>{};console.log("%c",devtools);devtools.toString=()=>{showMessage("哈哈,你打开了控制台,是想要看看我的小秘密吗?",6e3,9)};window.addEventListener("copy",(()=>{showMessage("你都复制了些什么呀,转载要记得加上出处哦!",6e3,9)}));window.addEventListener("visibilitychange",(()=>{if(!document.hidden)showMessage("哇,你终于回来了~",6e3,9)}))})();function welcomeMessage(){const message=`欢迎阅读「${document.title.split(" - ")[0]}」`;let text;if(location.pathname==="/"){const now=(new Date).getHours();if(now>5&&now<=7)text="早上好!一日之计在于晨,美好的一天就要开始了。";else if(now>7&&now<=11)text="上午好!工作顺利嘛,不要久坐,多起来走动走动哦!";else if(now>11&&now<=13)text="中午了,工作了一个上午,现在是午餐时间!";else if(now>13&&now<=17)text="午后很容易犯困呢,今天的运动目标完成了吗?";else if(now>17&&now<=19)text="傍晚了!窗外夕阳的景色很美丽呢,最美不过夕阳红~";else if(now>19&&now<=21)text="晚上好,今天过得怎么样?";else if(now>21&&now<=23)text=["已经这么晚了呀,早点休息吧,晚安~","深夜时要爱护眼睛呀!"];else text="你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?";return text}else if(document.referrer!==""){const referrer=new URL(document.referrer),domain=referrer.hostname.split(".")[1];const domains={baidu:"百度",so:"360搜索",google:"谷歌搜索"};if(location.hostname===referrer.hostname)return message;if(domain in domains)text=domains[domain];else text=referrer.hostname;return`Hello!来自 ${text} 的朋友
${message}`}else{return message}}showMessage(welcomeMessage(),7e3,8);function showHitokoto(){fetch("https://v1.hitokoto.cn").then((response=>response.json())).then((result=>{const text=`这句一言来自 「${result.from}」,是 ${result.creator} 在 hitokoto.cn 投稿的。`;showMessage(result.hitokoto,6e3,9);setTimeout((()=>{showMessage(text,4e3,9)}),6e3)}))}function showMessage(text,timeout,priority){if(!text||sessionStorage.getItem("waifu-text")&&sessionStorage.getItem("waifu-text")>priority)return;if(messageTimer){clearTimeout(messageTimer);messageTimer=null}text=randomSelection(text);sessionStorage.setItem("waifu-text",priority);const tips=document.getElementById("waifu-tips");tips.innerHTML=text;tips.classList.add("waifu-tips-active");messageTimer=setTimeout((()=>{sessionStorage.removeItem("waifu-text");tips.classList.remove("waifu-tips-active")}),timeout)}(function initModel(){let modelId=localStorage.getItem("modelId"),modelTexturesId=localStorage.getItem("modelTexturesId");if(modelId===null){modelId=1;modelTexturesId=53}loadModel(modelId,modelTexturesId);fetch(config.waifuPath).then((response=>response.json())).then((result=>{window.addEventListener("mouseover",(event=>{for(let{selector:selector,text:text}of result.mouseover){if(!event.target.matches(selector))continue;text=randomSelection(text);text=text.replace("{text}",event.target.innerText);showMessage(text,4e3,8);return}}));window.addEventListener("click",(event=>{for(let{selector:selector,text:text}of result.click){if(!event.target.matches(selector))continue;text=randomSelection(text);text=text.replace("{text}",event.target.innerText);showMessage(text,4e3,8);return}}));result.seasons.forEach((({date:date,text:text})=>{const now=new Date,after=date.split("-")[0],before=date.split("-")[1]||after;if(after.split("/")[0]<=now.getMonth()+1&&now.getMonth()+1<=before.split("/")[0]&&(after.split("/")[1]<=now.getDate()&&now.getDate()<=before.split("/")[1])){text=randomSelection(text);text=text.replace("{year}",now.getFullYear());messageArray.push(text)}}))}))})();async function loadModelList(){const response=await fetch(`${cdnPath}model_list.json`);modelList=await response.json()}async function loadModel(modelId,modelTexturesId,message){localStorage.setItem("modelId",modelId);localStorage.setItem("modelTexturesId",modelTexturesId);showMessage(message,4e3,10);if(useCDN){if(!modelList)await loadModelList();const target=randomSelection(modelList.models[modelId]);loadlive2d("live2d",`${cdnPath}model/${target}/index.json`)}else{loadlive2d("live2d",`${apiPath}get/?id=${modelId}-${modelTexturesId}`);console.log(`Live2D 模型 ${modelId}-${modelTexturesId} 加载完成`)}}async function loadRandModel(){const modelId=localStorage.getItem("modelId"),modelTexturesId=localStorage.getItem("modelTexturesId");if(useCDN){if(!modelList)await loadModelList();const target=randomSelection(modelList.models[modelId]);loadlive2d("live2d",`${cdnPath}model/${target}/index.json`);showMessage("我的新衣服好看嘛?",4e3,10)}else{fetch(`${apiPath}rand_textures/?id=${modelId}-${modelTexturesId}`).then((response=>response.json())).then((result=>{if(result.textures.id===1&&(modelTexturesId===1||modelTexturesId===0))showMessage("我还没有其他衣服呢!",4e3,10);else loadModel(modelId,result.textures.id,"我的新衣服好看嘛?")}))}}async function loadOtherModel(){let modelId=localStorage.getItem("modelId");if(useCDN){if(!modelList)await loadModelList();const index=++modelId>=modelList.models.length?0:modelId;loadModel(index,0,modelList.messages[index])}else{fetch(`${apiPath}switch/?id=${modelId}`).then((response=>response.json())).then((result=>{loadModel(result.model.id,0,result.model.message)}))}}}function initWidget(config,apiPath){if(typeof config==="string"){config={waifuPath:config,apiPath:apiPath}}document.body.insertAdjacentHTML("beforeend",`
\n\t\t\t看板娘\n\t\t
`);const toggle=document.getElementById("waifu-toggle");toggle.addEventListener("click",(()=>{toggle.classList.remove("waifu-toggle-active");if(toggle.getAttribute("first-time")){loadWidget(config);toggle.removeAttribute("first-time")}else{localStorage.removeItem("waifu-display");document.getElementById("waifu").style.display="";setTimeout((()=>{document.getElementById("waifu").style.bottom=0}),0)}}));if(localStorage.getItem("waifu-display")&&Date.now()-localStorage.getItem("waifu-display")<=864e5){toggle.setAttribute("first-time",true);setTimeout((()=>{toggle.classList.add("waifu-toggle-active")}),0)}else{loadWidget(config)}}window.initWidget=initWidget})(); +!function(){"use strict";function e(e){return Array.isArray(e)?e[Math.floor(Math.random()*e.length)]:e}let t;function o(o,s,n){if(!o||sessionStorage.getItem("waifu-text")&&sessionStorage.getItem("waifu-text")>n)return;t&&(clearTimeout(t),t=null),o=e(o),sessionStorage.setItem("waifu-text",n);const i=document.getElementById("waifu-tips");i.innerHTML=o,i.classList.add("waifu-tips-active"),t=setTimeout((()=>{sessionStorage.removeItem("waifu-text"),i.classList.remove("waifu-tips-active")}),s)}class s{constructor(e,t,o){this.useCDN=e,this.apiPath=t,this.cdnPath=o}async loadModelList(){const e=await fetch(`${this.cdnPath}model_list.json`);this.modelList=await e.json()}async loadModel(t,s,n){if(localStorage.setItem("modelId",t),localStorage.setItem("modelTexturesId",s),o(n,4e3,10),this.useCDN){this.modelList||await this.loadModelList();const o=e(this.modelList.models[t]);loadlive2d("live2d",`${this.cdnPath}model/${o}/index.json`)}else loadlive2d("live2d",`${this.apiPath}get/?id=${t}-${s}`),console.log(`Live2D 模型 ${t}-${s} 加载完成`)}async loadRandModel(){const t=localStorage.getItem("modelId"),s=localStorage.getItem("modelTexturesId");if(this.useCDN){this.modelList||await this.loadModelList();const s=e(this.modelList.models[t]);loadlive2d("live2d",`${this.cdnPath}model/${s}/index.json`),o("我的新衣服好看嘛?",4e3,10)}else fetch(`${this.apiPath}rand_textures/?id=${t}-${s}`).then((e=>e.json())).then((e=>{1!==e.textures.id||1!==s&&0!==s?this.loadModel(t,e.textures.id,"我的新衣服好看嘛?"):o("我还没有其他衣服呢!",4e3,10)}))}async loadOtherModel(){let e=localStorage.getItem("modelId");if(this.useCDN){this.modelList||await this.loadModelList();const t=++e>=this.modelList.models.length?0:e;this.loadModel(t,0,this.modelList.messages[t])}else fetch(`${this.apiPath}switch/?id=${e}`).then((e=>e.json())).then((e=>{this.loadModel(e.model.id,0,e.model.message)}))}}function n(t){let{apiPath:n,cdnPath:i}=t,c=!1;if("string"==typeof i)c=!0,i.endsWith("/")||(i+="/");else{if("string"!=typeof n)return void console.error("Invalid initWidget argument!");n.endsWith("/")||(n+="/")}const a=new s(c,n,i);localStorage.removeItem("waifu-display"),sessionStorage.removeItem("waifu-text"),document.body.insertAdjacentHTML("beforeend",'
\n\t\t\t
\n\t\t\t\n\t\t\t
\n\t\t
'),setTimeout((()=>{document.getElementById("waifu").style.bottom=0}),0);let l,d=!1,r=["好久不见,日子过得好快呢……","大坏蛋!你都多久没理人家了呀,嘤嘤嘤~","嗨~快来逗我玩吧!","拿小拳拳锤你胸口!","记得把小家加入 Adblock 白名单哦!"];function m(){fetch("https://v1.hitokoto.cn").then((e=>e.json())).then((e=>{const t=`这句一言来自 「${e.from}」,是 ${e.creator} 在 hitokoto.cn 投稿的。`;o(e.hitokoto,6e3,9),setTimeout((()=>{o(t,4e3,9)}),6e3)}))}window.addEventListener("mousemove",(()=>d=!0)),window.addEventListener("keydown",(()=>d=!0)),setInterval((()=>{d?(d=!1,clearInterval(l),l=null):l||(l=setInterval((()=>{o(e(r),6e3,9)}),2e4))}),1e3),function(){const e={hitokoto:{icon:'\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e',callback:m},asteroids:{icon:'\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e',callback:()=>{if(window.Asteroids)window.ASTEROIDSPLAYERS||(window.ASTEROIDSPLAYERS=[]),window.ASTEROIDSPLAYERS.push(new Asteroids);else{const e=document.createElement("script");e.src="https://fastly.jsdelivr.net/gh/stevenjoezhang/asteroids/asteroids.js",document.head.appendChild(e)}}},"switch-model":{icon:'\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e',callback:()=>a.loadOtherModel()},"switch-texture":{icon:'\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e',callback:()=>a.loadRandModel()},photo:{icon:'\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e',callback:()=>{o("照好了嘛,是不是很可爱呢?",6e3,9),Live2D.captureName="photo.png",Live2D.captureFrame=!0}},info:{icon:'\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e',callback:()=>{open("https://github.com/stevenjoezhang/live2d-widget")}},quit:{icon:'\x3c!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --\x3e',callback:()=>{localStorage.setItem("waifu-display",Date.now()),o("愿你有一天能与重要的人重逢。",2e3,11),document.getElementById("waifu").style.bottom="-500px",setTimeout((()=>{document.getElementById("waifu").style.display="none",document.getElementById("waifu-toggle").classList.add("waifu-toggle-active")}),3e3)}}};Array.isArray(t.tools)||(t.tools=Object.keys(e));for(let o of t.tools)if(e[o]){const{icon:t,callback:s}=e[o];document.getElementById("waifu-tool").insertAdjacentHTML("beforeend",`${t}`),document.getElementById(`waifu-tool-${o}`).addEventListener("click",s)}const s=()=>{};console.log("%c",s),s.toString=()=>{o("哈哈,你打开了控制台,是想要看看我的小秘密吗?",6e3,9)},window.addEventListener("copy",(()=>{o("你都复制了些什么呀,转载要记得加上出处哦!",6e3,9)})),window.addEventListener("visibilitychange",(()=>{document.hidden||o("哇,你终于回来了~",6e3,9)}))}(),o(function(){const e=`欢迎阅读「${document.title.split(" - ")[0]}」`;let t;if("/"===location.pathname){const e=(new Date).getHours();return t=e>5&&e<=7?"早上好!一日之计在于晨,美好的一天就要开始了。":e>7&&e<=11?"上午好!工作顺利嘛,不要久坐,多起来走动走动哦!":e>11&&e<=13?"中午了,工作了一个上午,现在是午餐时间!":e>13&&e<=17?"午后很容易犯困呢,今天的运动目标完成了吗?":e>17&&e<=19?"傍晚了!窗外夕阳的景色很美丽呢,最美不过夕阳红~":e>19&&e<=21?"晚上好,今天过得怎么样?":e>21&&e<=23?["已经这么晚了呀,早点休息吧,晚安~","深夜时要爱护眼睛呀!"]:"你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?",t}if(""!==document.referrer){const o=new URL(document.referrer),s=o.hostname.split(".")[1],n={baidu:"百度",so:"360搜索",google:"谷歌搜索"};return location.hostname===o.hostname?e:(t=s in n?n[s]:o.hostname,`Hello!来自 ${t} 的朋友
${e}`)}return e}(),7e3,8),function(){let s=localStorage.getItem("modelId"),n=localStorage.getItem("modelTexturesId");null===s&&(s=1,n=53),a.loadModel(s,n),fetch(t.waifuPath).then((e=>e.json())).then((t=>{window.addEventListener("mouseover",(s=>{for(let{selector:n,text:i}of t.mouseover)if(s.target.matches(n))return i=e(i),i=i.replace("{text}",s.target.innerText),void o(i,4e3,8)})),window.addEventListener("click",(s=>{for(let{selector:n,text:i}of t.click)if(s.target.matches(n))return i=e(i),i=i.replace("{text}",s.target.innerText),void o(i,4e3,8)})),t.seasons.forEach((({date:t,text:o})=>{const s=new Date,n=t.split("-")[0],i=t.split("-")[1]||n;n.split("/")[0]<=s.getMonth()+1&&s.getMonth()+1<=i.split("/")[0]&&n.split("/")[1]<=s.getDate()&&s.getDate()<=i.split("/")[1]&&(o=(o=e(o)).replace("{year}",s.getFullYear()),r.push(o))}))}))}()}window.initWidget=function(e,t){"string"==typeof e&&(e={waifuPath:e,apiPath:t}),document.body.insertAdjacentHTML("beforeend",'
\n\t\t\t看板娘\n\t\t
');const o=document.getElementById("waifu-toggle");o.addEventListener("click",(()=>{o.classList.remove("waifu-toggle-active"),o.getAttribute("first-time")?(n(e),o.removeAttribute("first-time")):(localStorage.removeItem("waifu-display"),document.getElementById("waifu").style.display="",setTimeout((()=>{document.getElementById("waifu").style.bottom=0}),0))})),localStorage.getItem("waifu-display")&&Date.now()-localStorage.getItem("waifu-display")<=864e5?(o.setAttribute("first-time",!0),setTimeout((()=>{o.classList.add("waifu-toggle-active")}),0)):n(e)}}();