halo-theme-sakura/script/utils.min.js

1 line
8.8 KiB
JavaScript

"use strict";var Util={_version:"1.3.4",getNavLangSub:function getNavLangSub(){var currentLang=navigator.language;currentLang=currentLang.substr(0,2);if(!currentLang){currentLang=navigator.browserLanguage}return currentLang},setCookie:function setCookie(key,value,days){var keyVE=key+Util._version,expires="";if(days){var date=new Date;date.setTime(date.getTime()+days*24*60*60*1e3);expires="; expires="+date.toUTCString()}document.cookie=keyVE+"="+(value||"")+expires+"; path=/"},getCookie:function getCookie(key){var keyVE=key+Util._version+"=";var ca=document.cookie.split(";");for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==" "){c=c.substring(1,c.length)}if(c.indexOf(keyVE)==0)return c.substring(keyVE.length,c.length)}return null},removeCookie:function removeCookie(key){var keyVE=key+Util._version+"=";document.cookie=keyVE+"=; Max-Age=-99999999;"},setLocalStorage:function(_setLocalStorage){function setLocalStorage(_x,_x2){return _setLocalStorage.apply(this,arguments)}setLocalStorage.toString=function(){return _setLocalStorage.toString()};return setLocalStorage}(function(key,value){var expires=arguments.length>2&&arguments[2]!==undefined?arguments[2]:3600;var keyVE=key+Util._version;var date=new Date;try{localStorage.setItem(keyVE,JSON.stringify({expires:date.valueOf()+expires*1e3,data:value}))}catch(e){if(e.name==="QuotaExceededError"){console.log("数据已满,自动清空");localStorage.clear();setLocalStorage(key,value,expires)}}}),getLocalStorage:function getLocalStorage(key){var keyVE=key+Util._version;var result=JSON.parse(localStorage.getItem(keyVE));var date=new Date;if(result&&result.expires>date){return result.data}else{localStorage.removeItem(keyVE);return null}},removeLocalStorage:function removeLocalStorage(key){var keyVE=key+Util._version;localStorage.removeItem(keyVE)},loadJS:function loadJS(url,callback){Util._loadRes("script",url,callback)},loadCSS:function loadCSS(url,callback){Util._loadRes("link",url,callback)},_loadRes:function _loadRes(type,url,callback){var dom,fn=callback||function(){};switch(type){case"script":dom=document.createElement(type);dom.type="text/javascript";dom.src=url;break;case"link":dom=document.createElement(type);dom.type="text/css";dom.type="stylesheet";dom.href=url;break;default:console.warn("暂不支持:"+type+" 类型");return}if(dom.readyState){dom.onreadystatechange=function(){if(dom.readyState=="loaded"||dom.readyState=="complete"){dom.onreadystatechange=null;fn()}}}else{dom.onload=function(){fn()}}var head=document.getElementsByTagName("head")[0];head.appendChild(dom)},getRandomColor:function getRandomColor(){var min=arguments.length>0&&arguments[0]!==undefined?arguments[0]:0;var max=arguments.length>1&&arguments[1]!==undefined?arguments[1]:1;if(!min){min=0}if(!max){max=0}min=isNaN(min)?0:Number(min);max=isNaN(max)?1:Number(max);min=Math.min(Math.max(Math.abs(min),0),1);max=Math.min(Math.max(Math.abs(max),0),1);max=max<min?1:max;return"#"+function(h){return new Array(7-h.length).join("0")+h}(((Math.random()*(max-min)+min)*16777216<<0).toString(16))},getNearestKeyframe:function getNearestKeyframe(keyframesIndex,milliseconds){var keyframeIdx=this._search(keyframesIndex.times,milliseconds);return{index:keyframeIdx,milliseconds:table.times[keyframeIdx],fileposition:table.filepositions[keyframeIdx]}},_search:function _search(list,value){var idx=0;var last=list.length-1;var mid=0;var lbound=0;var ubound=last;if(value<list[0]){idx=0;lbound=ubound+1}while(lbound<=ubound){mid=lbound+Math.floor((ubound-lbound)/2);if(mid===last||value>=list[mid]&&value<list[mid+1]){idx=mid;break}else if(list[mid]<value){lbound=mid+1}else{ubound=mid-1}}return idx},removeClassByPrefix:function removeClassByPrefix(el,prefix){var classes=el.className.split(" ").filter(function(c){return c.lastIndexOf(prefix,0)!==0});el.className=classes.join(" ").trim()},caclEstimateReadTime:function caclEstimateReadTime(count){var coefficient=arguments.length>1&&arguments[1]!==undefined?arguments[1]:3;var defaultSpeed=400;coefficient=Math.min(6,Math.max(1,coefficient));var speed=defaultSpeed+(coefficient-1)*100;return Math.ceil(count/speed*60)},minuteToTimeString:function minuteToTimeString(s){var timeStr="";var day=Math.floor(s/3600/24);if(day!==0){timeStr=timeStr+day+" 天 "}var hours=Math.floor(s/3600%24);if(day!==0||hours!==0){timeStr=timeStr+hours+" 小时 "}var points=Math.floor(s/60%60);if(hours!==0||points!==0){timeStr=timeStr+points+" 分钟 "}var seconds=Math.floor(s%60);if(points!==0||seconds!==0){timeStr=timeStr+seconds+" 秒"}return timeStr},timeAgo:function timeAgo(time){var currentTime=(new Date).getTime();var between=currentTime-time;var days=Math.floor(between/(24*3600*1e3));if(days===0){var leave1=between%(24*3600*1e3);var hours=Math.floor(leave1/(3600*1e3));if(hours===0){var leave2=leave1%(3600*1e3);var minutes=Math.floor(leave2/(60*1e3));if(minutes===0){var leave3=leave2%(60*1e3);var seconds=Math.round(leave3/1e3);return seconds+" 秒前"}return minutes+" 分钟前"}return hours+" 小时前"}if(days<0){return"刚刚"}if(days<30){return days+" 天前"}var years=Math.floor(days/(30*12));if(years===0){var months=Math.floor(days/30);return months+" 月前"}return years+" 年前"},formatDate:function formatDate(date,fmt){date=new Date(date);if(/(y+)/.test(fmt)){fmt=fmt.replace(RegExp.$1,(date.getFullYear()+"").substr(4-RegExp.$1.length))}var o={"M+":date.getMonth()+1,"d+":date.getDate(),"h+":date.getHours(),"m+":date.getMinutes(),"s+":date.getSeconds()};for(var k in o){if(new RegExp("(".concat(k,")")).test(fmt)){var str=o[k]+"";fmt=fmt.replace(RegExp.$1,RegExp.$1.length===1?str:Util.padLeftZero(str))}}return fmt},padLeftZero:function padLeftZero(str){return("00"+str).substr(str.length)}};var Toast=function Toast(){_classCallCheck(this,Toast);this._t=null;this._timeOut=null;this._settings={duration:2e3,width:260,height:60,top:"top",background:"#fe9600",color:"#fff","font-size":14};Toast.prototype.init=function(opt){_extend(this._settings,opt)};Toast.prototype.create=function(text,duration){if(this._timeOut){clearTimeout(this._timeOut);document.body.removeChild(this._t);this._t=null}if(!text){console.error("提示文本不能为空");return}this._t=document.createElement("div");this._t.className="t-toast";this._t.innerHTML='<p class="message"><span>'+text+"</span></p>";document.body.appendChild(this._t);this.setStyle();var _that=this;this._timeOut=setTimeout(function(){document.body.removeChild(_that._t);_that._timeOut=null;_that._t=null},duration||this._settings.duration)};Toast.prototype.setStyle=function(){this._t.style.width=this._settings.width+"px";this._t.style.height=this._settings.height+"px";this._t.style.position="fixed";this._t.style["text-align"]="center";this._t.style["z-index"]="20200531";if(isNaN(Number(this._settings.top))){if(this._settings.top=="centent"){this._t.style.top=_viewHeight()/2+"px"}else if(this._settings.top=="top"){this._t.style.top="0px"}}else{this._t.style.top=this._settings.top+"px"}this._t.style.left="50%";this._t.style["margin-left"]="-"+this._settings.width/2+"px";this._t.style.background=this._settings.background;this._t.style.color=this._settings.color;this._t.style["border-bottom-left-radius"]="4px";this._t.style["border-bottom-right-radius"]="4px";this._t.style["font-size"]=this._settings["font-size"]+"px";this._t.style.display="flex";this._t.style["justify-content"]="center";this._t.style["align-items"]="center"};function _viewHeight(){return document.documentElement.clientHeight}function _extend(o1,o2){for(var attr in o2){o1[attr]=o2[attr]}}};var Log=function(){return{e:function e(msg,tag){if(!tag||Log.FORCE_GLOBAL_TAG)tag=Log.GLOBAL_TAG;var str="[".concat(tag,"] > ").concat(msg);if(!Log.ENABLE_ERROR){return}if(console.error){console.error(str)}else if(console.warn){console.warn(str)}else{console.log(str)}},i:function i(msg,tag){if(!tag||Log.FORCE_GLOBAL_TAG)tag=Log.GLOBAL_TAG;var str="[".concat(tag,"] > ").concat(msg);if(!Log.ENABLE_INFO){return}if(console.info){console.info(str)}else{console.log(str)}},w:function w(msg,tag){if(!tag||Log.FORCE_GLOBAL_TAG)tag=Log.GLOBAL_TAG;var str="[".concat(tag,"] > ").concat(msg);if(!Log.ENABLE_WARN){return}if(console.warn){console.warn(str)}else{console.log(str)}},d:function d(msg,tag){if(!tag||Log.FORCE_GLOBAL_TAG)tag=Log.GLOBAL_TAG;var str="[".concat(tag,"] > ").concat(msg);if(!Log.ENABLE_DEBUG){return}if(console.debug){console.debug(str)}else{console.log(str)}},v:function v(msg,tag){if(!tag||Log.FORCE_GLOBAL_TAG)tag=Log.GLOBAL_TAG;var str="[".concat(tag,"] > ").concat(msg);if(!Log.ENABLE_VERBOSE){return}console.log(str)}}}();Log.GLOBAL_TAG="Sakura";Log.FORCE_GLOBAL_TAG=false;Log.ENABLE_ERROR=true;Log.ENABLE_INFO=true;Log.ENABLE_WARN=true;Log.ENABLE_DEBUG=true;Log.ENABLE_VERBOSE=true;var RuntimeException=function RuntimeException(message){this._message=message;RuntimeException.prototype={get name(){return"RuntimeException"},get message(){return this._message},toString:function toString(){return this.name+": "+this.message}}};