jk

jk库(js常用 es6)

目前為 2022-03-16 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/441600/1028888/jk.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// 远程调用代码 :   https://greasyfork.org/scripts/439519-jjkjs/code/jjkjs.user.js
//  <script src="https://greasyfork.org/scripts/439519-jjkjs/code/jjkjs.user.js<?php echo "?v=".rand(1,10000);?>"></script>
// @name            jk
// @namespace       moe.canfire.flf

// @description     jk库(js常用 es6)
// @author          mengzonefire
// @license         MIT
// @match           *
 
// @resource jquery         https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js
// @require         https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.js
// @grant           GM_setValue
// @grant           GM_getValue
// @grant           GM_deleteValue
// @grant           GM_setClipboard
// @grant           GM_xmlhttpRequest
// @grant           GM_info
// @grant           GM_getResourceText
// @grant           GM_addStyle
// @grant           unsafeWindow
// @run-at          document-start
// @connect         *
// @version 0.0.1.20220316115257
// ==/UserScript==
 
//找不到函数定义的解决方法
/*
不要用 function test(){};
而要: "use strict";
test=function(){ };或 String.test=function(){}; 
*/
/*
1.选择器函数,ems,ems0,特快专递   var emlist=jfunc.ems('.div');  for(var em in ems)
2.junc.成员函数
  elementNew,elementRemove,
  addScript,addStyle,addScreenMask,addDiv,
3.jwidget小部件,包括jmask,jloading,jtoast, jdialog,jinputdialog,jconfirmdialog,jmsgdialog
4.jstr成员函数getBefore,getAfter,getBetween0,getBetweenList
  contains,newRegExp,substrCount
*/

window.ems=function(Selector){
	return document.querySelectorAll(Selector);
 };
window.ems0=function(Selector){
	return document.querySelectorAll(Selector)[0];
 };
var jstr={

 getBefore:function(sourcestr,str){
  var p=sourcestr.indexOf(str);
  if(p<0) return '';
  return sourcestr.substr(0,p);
},
getAfter:function(sourcestr,str){
  var p=sourcestr.indexOf(str);
  if(p<0) return sourcestr;
  return sourcestr.substr(p+str.length);
},
getBetween0:function(fullstr,str1,str2){//注意转义问题
  return this.getBefore(this.getAfter(fullstr,str1)+str2,str2);
},
getBetweenList:function(fullstr,strBegin2,strEnd2){//返回一个数组
 var regstr=strBegin2+"(((?!"+strBegin2+").)*)"+strEnd2;
 var rx=new RegExp( regstr, 'gm');
 var result=[];
 var data;
 while( ( data= rx.exec( fullstr ) )!= null){	result.push(data[1]);}
  return result;
},
substrCount:function(sourcstr,substr) { //sourcstr 源字符串 substr特殊字符
     var count=0;
     while(sourcstr.indexOf(substr) != -1 ) {
        sourcstr = sourcstr.replace(substr,"")
        count++;
     }
     return count;
},
contains(fullstr,findstr){
  return (fullstr.indexOf(findstr)!=-1)?true:false;
},
newRegExp:function (regstr_spashMustDouble,igm){//正则的特殊字符串处理
       //// a.replace( myRegExp("ax\zff", "gm"), s2); //直接使用字符串,自动处理需要转义的字符;
	 var charlist='$*+.?\\^|(){}[]'.split(""); //\\只是代表\字符;
	 for(var kid in charlist){
	     regstr_spashMustDouble=regstr_spashMustDouble.replace( charlist[kid],"\\"+charlist[kid]);
	 }
 	 return new RegExp(regstr_spashMustDouble,igm);
	  /*
	  //正则表达式,
	  //第1步,检查其中本身有没有\,如果有,必须改成\\,否则\这字符会自动少掉;
	  //第2步,对元字符要加转义前加\
           
	   */
}  
}//jstr

jfunc={
 /*

*/
 elementNew:function(uid,tag){
    var el=document.querySelector("#"+uid) ;  
    if( (el!=undefined)&&(el!=null)  ){el.parentNode.removeChild(el);};
     el=document.createElement(tag);el.id=uid;
     return el;
},
  elementRemove:function(uid_or_elementObj){//id 或 element
    if(typeof(uid_or_elementObj)=='string') var el=document.querySelector("#"+uid_or_elementObj) ;  
    else var el=uid_or_elementObj;
    if( (el!=undefined)&&(el!=null)  ){el.parentNode.removeChild(el);};
},
addScript:function(uid,url){
    var el=elementNew(id,'script');  
    el.type='text/javascript';el.src=url+'?rnd='+Math.random();
    document.getElementsByTagName("head")[0].appendChild(el); 
},
addStyle:function(uid,css){
	var el=elementNew(uid,"style");
    el.innerHTML=css;
    document.getElementsByTagName("head")[0].appendChild(el); 
},
addDiv:function(uid,Tag){
	var el=elementNew(uid,"div");
    document.body.appendChild(el);	
    return el;
},
addScreenMask:function(uid,opacity){
	var el=elementNew(uid,"screenmask");
	opacity=isNaN(opacity)?0.5:opacity;
	if( 'undefined,null'.indexOf( typeof(bgcolor) ) ==-1) bgcolor='lightgray';
	//opacity:0.3; 影响子元素透明度,且不能改;
	el.style.cssText=`
	display:flex;flex-direction:row;justify-content:center;align-items:center;
	z-index: 9999;
	position: absolute;left:0px;top:0px;right:0px;bottom:0px;
	font-size: 16px;
	background: rgba(0,0,0,${opacity})
	`;
    document.body.appendChild(el);	
    return el;
}
}//-----------------------------------

var jmask={
	show:function(uid,opacity){
	 if( (opacity==undefined)||(opacity==null)){opacity=0.3;};	 
     var thiz=this;
     this.close(uid);
	 mask = document.createElement('div'); mask.id=uid;
     mask.style.cssText=`display:flex;flex-direction:row;justify-content:center;align-items:center;position: absolute;z-index: 9999999;background:rgba(0,0,0,${opacity});font-size: 16px;left:0px;top:0px;right:0px;bottom:0px;`;
     document.body.appendChild(mask);	
     return mask;
	},
  close:function(uid){
	var mask=document.querySelector("#"+uid) ;
	if(   (mask!=undefined)&&(mask!=null)  ){ mask.parentNode.removeChild(mask);};
  }
}//---------------------------------
var jdialog={
/* 
 //弹出输入窗口
 var html='<div id="edt" contenteditable="true" style="flex:1;border:1px solid lightgray;margin:3px;">aab</div>';
 jdlg.show("xx","注意",'请输入:',html,"确定,取消","250px","auto",function(caption){
	 alert( document.querySelector('#edt').innerText );
	jdlg.close("xx");
});

//确认对话框
 var html='<div id="edt"  style="flex:1;margin:3px;padding-left:5px;">你确定删除吗?<br>xx</div>';
 jdlg.show("xx1","注意",'',html,"确定,取消","250px","auto",function(caption){
	 alert( document.querySelector('#edt').innerText );
	jdlg.close("xx1");
});

//提示框
 var html='<div id="edt"  style="flex:1;margin:3px;padding-left:5px;">操作成功!<br>xx</div>';
 jdlg.show("xx2","注意",'',html,"确定","250px","auto",function(caption){
	 alert( document.querySelector('#edt').innerText );
	jdlg.close("xx2");
});
*/

uniqueAddDialogStyle:function(uid){
   var el=document.querySelector(uid);
   if('undefined,null'.indexOf(typeof(document.querySelector(uid)))!=-1) return;//已存在;
   el=document.createElement("style");el.id=uid;
var css=`
screenmask,.screenmask{
	display:flex;flex-direction:row;justify-content:center;align-items:center;
	z-index: 999999;
	position: absolute;left:0px;top:0px;right:0px;bottom:0px;
	background:rgba(100, 100, 100,.7);
	font-size: 16px;
}

dlgform,.dlgform{
	display:flex;flex-direction:column;justify-content:flex-start;
	z-index: 9999;font-size: 16px;
	left:0; right:0; top:0; bottom:0;
    margin:auto;

	background-color:white;padding:5px;
	border-radius:5px;
	font-size:1.1rem;
	
}
dlgheader,.dlgheader{
    display:flex;flex-direction:column;justify-content:center;align-items:flex-start; 
    padding-left:15px;
    height:35px;flex-shrink:0;
    color:#3582d8;
}
lineborder{/* F8F8FF */
	display:flex;height:0px;border:1px solid #dd;border-bottom:0px;;transform:scale(0.9,0.1);
	}
dlghint,.dlghint{
	padding-left:6px;
	height:25px;font-size:1.0rem;
	display:flex;flex-direction:column;justify-content:center;align-items:stretch;
    }
dlgbody,.dlgbody{
	flex:1;
    margin:5px;margin-top:0px;
	display:flex;flex-direction:column;justify-content:center;align-items:stretch;
    }
dlgfooter,.dlgfooter{
	display:flex;flex-direction:row;justify-content:center;align-items:center;
	flex-shrink:0;
	height:38px;
    }
dlgbutton,.dlgbutton{
    flex:1;
    display:flex;flex-direction:row;justify-content:center;align-items:center;
    background-color:#3582d8;
    color:white;
    margin:3px;
    border-radius:3px;
    height:30px;flex-shrink:0;
	}
`;
    el.innerHTML=css;
    document.getElementsByTagName("head")[0].appendChild(el); 
},
 
 show:function(uid,caption,hintHtml,bodyHtml,btncaptions,width,height,Fn_click_caption ){
      this.uniqueAddDialogStyle('dlgcss'+uid);    
      var btnstr='';
      var captionarr=btncaptions.split(",");
      for(var i=0;i<captionarr.length;i++){
      	if(captionarr[i].trim()=='')continue;
          btnstr=btnstr+'<div class="dlgbutton" >'+captionarr[i]+'</div>';
      }
      var btnHtml=btnstr;
      hintHtml=(hintHtml.trim()=='')?'':`<dlghint>${hintHtml}</dlghint>`;
     var thiz=this;
 
     var html =`
<dlgform style="width:${width};height:${height};">
<dlgheader class="">${caption}</dlgheader>
<lineborder></lineborder>
${hintHtml}
<dlgbody class="" >${bodyHtml}</dlgbody>
<div class="dlgfooter">${btnHtml}</div>
</dlgform>
      `; 
   var screenmask=jmask.show('dlgmask'+uid);
   screenmask.innerHTML=html;
   //绑定事件
      var list= screenmask.querySelectorAll('.dlgbutton');
      for (const button of list) {
         button.addEventListener('click', function(event) {
         	var caption=event.target.innerText.trim();
         	Fn_click_caption(caption);
         	thiz.close('dlgmask'+uid);//确保关闭
       })
      };

      return screenmask;
  },
  close:function(uid){
	var mask=document.querySelector("#"+uid) ;
	if(   (mask!=undefined)&&(mask!=null)  ){ mask.parentNode.removeChild(mask);};
  }
}

jinputdialog={
  show(uid,caption,hintHtml,defautInput,width,height,fn_btncaption_inputvalue){
 //弹出输入窗口
 var contentHtml=`<div id="inputedit${uid}" contenteditable="true" style="flex:1;border:1px solid lightgray;margin:3px;">${defautInput}</div>`;
 jdialog.show(uid,caption,hintHtml,contentHtml,"确定,取消",width,height,function(btncaption){
	fn_btncaption_inputvalue(btncaption, document.querySelector(`#inputedit${uid}`).innerText.trim() );
	jdialog.close(`inputedit${uid}`);
  });//------
  }
}
jconfirmdialog={
  show(uid,caption,confirmText,width,height,fn_btncaption){
 //弹出输入窗口
 var contentHtml=`<div id="inputedit${uid}" style="flex:1;border:margin:15px;padding:5px;shrink:0;">${confirmText}</div>`;
 jdialog.show(uid,caption,'',contentHtml,"确定,取消",width,height,function(btncaption){
	fn_btncaption(btncaption  );
	jdialog.close(`inputedit${uid}`);
  });//------
  }
}
jmsgdialog={
  show(uid,caption,confirmText,width,height,fn_btncaption){
 //弹出输入窗口
 var contentHtml=`<div id="inputedit${uid}" style="flex:1;border:margin:15px;padding:5px;">${confirmText}</div>`;
 jdialog.show(uid,caption,'',contentHtml,"确定",width,height,function(btncaption){
	if(fn_btncaption!=undefined) fn_btncaption(btncaption  );
	jdialog.close(`inputedit${uid}`);
  });//------
  }
}
/*
//确认对话框
 var html='<div id="edt"  style="flex:1;margin:3px;padding-left:5px;">你确定删除吗?<br>xx</div>';
 jdlg.show("xx1","注意",'',html,"确定,取消","250px","auto",function(caption){
	 alert( document.querySelector('#edt').innerText );
	jdlg.close("xx1");
});

//提示框
 var html='<div id="edt"  style="flex:1;margin:3px;padding-left:5px;">操作成功!<br>xx</div>';
 jdlg.show("xx2","注意",'',html,"确定","250px","auto",function(caption){
	 alert( document.querySelector('#edt').innerText );
	jdlg.close("xx2");
});
*/
 
jtoast=function(msg,duration){//toast('这是一个弹框',2000)
      duration=isNaN(duration)?500:duration;
      var m = document.createElement('div');
      m.innerHTML = msg;
      m.style.cssText="max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(0, 0, 0,.7);font-size: 16px;";
      document.body.appendChild(m);
      setTimeout(function() {
        var d = 0.5;
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
        m.style.opacity = '0';
        setTimeout(function() { document.body.removeChild(m) }, d * 1000);
      }, duration);
}

var jloading={
  close:function(uid){
         clearInterval(window.jkinterval777);
         jmask.close('mask'+uid);
  },
  show:function(uid,txt,timeout){
      window.jkinterval777=-1;
      if(txt==undefined)txt='waiting ';
          var obj=document.createElement('div');
          obj.innerText==txt+'∴';
          obj.id='hint'+uid;
          obj.style.cssText=`
max-width:60%;min-width:100px;min-height:35px;
display:flex;flex-direction:row;justify-content:center;align-items:center;
z-index:999999;
background:gray;
radius:8px;
height:auto;padding:3px 15px;
font-size:18px;color:white;
`;
  		 
          obj.style.display='';
           window.jkinterval777=setInterval(function(){  
			   if(obj.innerText==''){ obj.innerText=txt+'∴';return;}
               if(obj.innerText==txt+'∴'){ obj.innerText=txt+'∵';return;}
               if(obj.innerText==txt+'∵'){ obj.innerText=txt+'∴';return;}
			   return;//100
           },550);
      var mask=jmask.show('mask'+uid,0);
       mask.appendChild(obj);
       obj.innerText==txt+'∴';
      
           //默认3秒定时关闭
           if( typeof(timeou)!= "undefined" )  timeout=3*1000;
           setTimeout(function(){
               clearInterval(window.jkinterval777); 
               jmask.close('mask'+uid);
           },timeout );
 
     }//show;
}//--------------------------------------
 
var jwidget={
	mask:jmask,
    toast:jtoast,
	loading:jloading,
	dialog:jdialog,
	inputdialog:jinputdialog,
	confirmdialog:jconfirmdialog,
	msgdialog:jmsgdialog,
}
//jfunc.ems
 /*
jwidget.mask.show('xx');或jmask.show('')

*/
//jwidget.mask.show('xx');
//jwidget.mask.close('xx');
//jtoast("hellow",2000);
//jloading.show('cv', 'waiting',3000)
/*
jinputdialog.show('fsf','注意','请输入:','默认值','250px','auto',function(btncaption,inputvalue){
   alert(btncaption+'  '+inputvalue);
});
jconfirmdialog.show('uid','caption','confirm<br>Text?','250px','auto',function(btncaption){
   alert(btncaption);
});
jmsgdialog.show('uid','caption','confirm<br>Text?','250px','auto');
jmsgdialog.show('uid','caption','<div align="center">confirm<br>Text?</div>','250px','auto');

jinputdialog.show('fsf','注意','请输入:','默认值','250px','auto',function(btncaption,inputvalue){
   alert(btncaption+'  '+inputvalue);
});

*/