jk

jk库(js常用 es6)

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/441600/1463015/jk.js

  1. // ==UserScript==
  2. /*
  3. 远程调用代码 : https://greasyfork.org/scripts/441600-jk/code/jk.user.js
  4. php <script src="https://greasyfork.org/scripts/441600-jk/code/jk.user.js<?php echo "?v=".rand(1,10000);?>"></script>
  5. js:
  6. var url='https://greasyfork.org/scripts/441600-jk/code/jk.user.js';
  7. el=document.createElement('script');
  8. el.src=url+'?rnd='+Math.random();
  9. document.getElementsByTagName("head")[0].appendChild(el);
  10. */
  11. //
  12. // @name jk
  13. // @namespace moe.canfire.flf
  14.  
  15. // @description jk库(js常用 es6)
  16. // @author mengzonefire
  17. // @license MIT
  18. // @match *
  19. // @resource jquery https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js
  20. // @require https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.js
  21. // @grant GM_setValue
  22. // @grant GM_getValue
  23. // @grant GM_deleteValue
  24. // @grant GM_setClipboard
  25. // @grant GM_xmlhttpRequest
  26. // @grant GM_info
  27. // @grant GM_getResourceText
  28. // @grant GM_addStyle
  29. // @grant unsafeWindow
  30. // @run-at document-start
  31. // @connect *
  32. // ==/UserScript==
  33. //找不到函数定义的解决方法
  34. /*
  35. 不要用 function test(){};
  36. 而要: "use strict";
  37. test=function(){ };或 String.test=function(){};
  38. */
  39. //=====================================
  40. var help=`
  41. /*
  42. 1.选择器函数,selall,sel,特快专递 var emlist=jfunc.selall('.div'); for(var e in ems)
  43. 2.jstr成员函数getBefore,getAfter,getBetween0,getBetweenList
  44. contains,newRegExp,substrCount
  45. //jstr.getBefore,getAfter,getBetween0,getBetweenList,substrCount,contains,excelTable2json
  46. 3.jstorage.getstr,setstr,getjon,setjson
  47. 4.jnode.成员函数,节点操作
  48. jnode.addNew,jnode.remove,
  49. jnode.addScript,addStyle,addScreenMask,addDiv,
  50. 5.jwidget小部件,包括jmask,jloading,jtoast, jdialog,jinputdialog,jconfirmdialog,jmsgdialog
  51. jtoast.show("aa",1000);jtoast.close();
  52. jloading.show('cv', 'waiting',3000);jloading.close();
  53. jinputdialog.show('fsf','注意','请输入:','默认值','250px','auto',function(btncaption,inputvalue){
  54. alert(btncaption+' '+inputvalue);
  55. });
  56. jconfirmdialog.show('uid','caption','confirm<br>Text?','250px','auto',function(btncaption){
  57. alert(btncaption);
  58. });
  59. jmsgdialog.show('uid','caption','confirm<br>Text?','250px','auto');
  60. jmsgdialog.show('uid','caption','<div align="center">confirm<br>Text?</div>','250px','auto');
  61. jmask.show(''); jmask.close();
  62. */
  63. `
  64. console.log(help)
  65. //=====================================
  66.  
  67. window.sels=function(Selector){
  68. return document.querySelectorAll(Selector);
  69. };
  70. window.sel=function(Selector){
  71. return document.querySelectorAll(Selector)[0];
  72. };
  73. window.isValid=function(data_notnull_undefined){
  74. if( (data_notnull_undefined==null)||(data_notnull_undefined==undefined) ) return true;
  75. return false;
  76. };
  77. window.isEmpty=function (val) {
  78. return !(!!val ? typeof val === 'object' ? Array.isArray(val) ? !!val.length : !!Object.keys(val).length : true : false);
  79. };
  80. window.rndBetween=function(min, max) {
  81. // m<=x<=n
  82. var range = max - min;
  83. var rand = Math.random();
  84. var randGrow = Math.round(rand * range); //四舍五入
  85. return min+randGrow;
  86. };
  87. //jstorage.getstr,setstr,getjon,setjson
  88. var jstorage={
  89. setstr:function(name,str){
  90. return window.localStorage.setItem(name,str);
  91. },
  92. getstr:function(name){
  93. return window.localStorage.getItem(name);
  94. },
  95. setjson:function (name,json_arr){
  96. var jsonStr = JSON.stringify(json_arr);
  97. window.localStorage.setItem(name,jsonStr);
  98. },
  99. getjson:function (name,json_arr){
  100. var jsonStr=window.localStorage.getItem(name);
  101. return JSON.parse( jsonStr );
  102. },
  103. };
  104. //jstr.getBefore,getAfter,getBetween0,getBetweenList,substrCount,contains,excelTable2json
  105. var jstr={
  106. replaceAll:function(source,regExpStr,replaceStr){
  107. return source.replace(new RegExp(regExpStr,"gm"),replaceStr );
  108. },
  109.  
  110. getBefore:function(sourcestr,str){
  111. var p=sourcestr.indexOf(str);
  112. if(p<0) return '';
  113. return sourcestr.substr(0,p);
  114. },
  115. getAfter:function(sourcestr,str){
  116. var p=sourcestr.indexOf(str);
  117. if(p<0) return sourcestr;
  118. return sourcestr.substr(p+str.length);
  119. },
  120. getBetween0:function(fullstr,str1,str2){//注意转义问题
  121. return this.getBefore(this.getAfter(fullstr,str1)+str2,str2);
  122. },
  123. getBetweenList:function(fullstr,strBegin2,strEnd2){//返回一个数组
  124. var regstr=strBegin2+"(((?!"+strBegin2+").)*)"+strEnd2;
  125. var rx=new RegExp( regstr, 'gm');
  126. var result=[];
  127. var data;
  128. while( ( data= rx.exec( fullstr ) )!= null){ result.push(data[1]);}
  129. return result;
  130. },
  131. substrCount:function(sourcstr,substr) { //sourcstr 源字符串 substr特殊字符
  132. var count=0;
  133. while(sourcstr.indexOf(substr) != -1 ) {
  134. sourcstr = sourcstr.replace(substr,"")
  135. count++;
  136. }
  137. return count;
  138. },
  139. contains(fullstr,findstr){
  140. return (fullstr.indexOf(findstr)!=-1)?true:false;
  141. },
  142. excelTable2json:function(excelTableText,commaNames,keycolid){
  143. //excel复制过来的转为json 行分隔符是换行或分号,列分隔符是制表符或逗号
  144. /*
  145. var txt=`
  146. 1 1905400127 袁子奕
  147. 2 1905400126 杨蓉
  148. `;
  149. var json=excelTable2json(txt,'no,stuno,stuname',1);
  150. //结果
  151. {
  152. "1905400127":{no:1,stuno:1905400127,stuname:"袁子奕"},
  153. "1905400126":{no:1,stuno:1905400126,stuname:"杨蓉"}
  154. }
  155. */
  156. var names=commaNames.split(',');
  157. var rows= excelTableText.trim().split(/[;\n]/);//分行,换行符或分号
  158. var jsonstr='';
  159. for(var i in rows){
  160. var cols=rows[i].split(/[,\t]/);//分列,制表符或逗号
  161. var tmpstr='';
  162. for( var j in cols){
  163. tmpstr=tmpstr+`,"${names[j]}":"${cols[j]}"`;
  164. }
  165. jsonstr=jsonstr+`,\r\n"${cols[keycolid]}":{ ${tmpstr.substring(1)} }`;
  166. };
  167. jsonstr="{"+jsonstr.substring(1)+"\r\n}";
  168. console.log(jsonstr);
  169. var json=(new Function("", "return " + jsonstr ))();
  170. return json;
  171. },//---------------------------
  172. newRegExp:function (regstr_spashMustDouble,igm){//正则的特殊字符串处理
  173. //// a.replace( myRegExp("ax\zff", "gm"), s2); //直接使用字符串,自动处理需要转义的字符;
  174. var charlist='$*+.?\\^|(){}[]'.split(""); //\\只是代表\字符;
  175. for(var kid in charlist){
  176. regstr_spashMustDouble=regstr_spashMustDouble.replace( charlist[kid],"\\"+charlist[kid]);
  177. }
  178. return new RegExp(regstr_spashMustDouble,igm);
  179. /*
  180. //正则表达式,
  181. //第1步,检查其中本身有没有\,如果有,必须改成\\,否则\这字符会自动少掉;
  182. //第2步,对元字符要加转义前加\
  183. */
  184. }
  185. }//jstr
  186.  
  187. jnode={
  188. addNew:function(uid,tag){
  189. var el=document.querySelector("#"+uid) ;
  190. if( (el!=undefined)&&(el!=null) ){el.parentNode.removeChild(el);};
  191. el=document.createElement(tag);el.id=uid;
  192. return el;
  193. },
  194. remove:function(uid_or_elementObj){//id 或 element
  195. if(typeof(uid_or_elementObj)=='string') var el=document.querySelector("#"+uid_or_elementObj) ;
  196. else var el=uid_or_elementObj;
  197. if( (el!=undefined)&&(el!=null) ){el.parentNode.removeChild(el);};
  198. },
  199. addScript:function(uid,url){
  200. var el=addNew(id,'script');
  201. el.type='text/javascript';el.src=url+'?rnd='+Math.random();
  202. document.getElementsByTagName("head")[0].appendChild(el);
  203. },
  204. addStyle:function(uid,css){
  205. var el=addNew(uid,"style");
  206. el.innerHTML=css;
  207. document.getElementsByTagName("head")[0].appendChild(el);
  208. },
  209. addDiv:function(uid,Tag){
  210. var el=addNew(uid,"div");
  211. document.body.appendChild(el);
  212. return el;
  213. },
  214. addScreenMask:function(uid,opacity){
  215. var el=addNew(uid,"screenmask");
  216. opacity=isNaN(opacity)?0.5:opacity;
  217. if( 'undefined,null'.indexOf( typeof(bgcolor) ) ==-1) bgcolor='lightgray';
  218. //opacity:0.3; 影响子元素透明度,且不能改;
  219. el.style.cssText=`
  220. display:flex;flex-direction:row;justify-content:center;align-items:center;
  221. z-index: 9999;
  222. position: absolute;left:0px;top:0px;right:0px;bottom:0px;
  223. font-size: 16px;
  224. background: rgba(0,0,0,${opacity})
  225. `;
  226. document.body.appendChild(el);
  227. return el;
  228. }
  229. }//-----------------------------------
  230.  
  231. var jmask={
  232. show:function(uid,opacity){
  233. if( (opacity==undefined)||(opacity==null)){opacity=0.3;};
  234. var thiz=this;
  235. this.close(uid);
  236. mask = document.createElement('div'); mask.id=uid;
  237. 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;`;
  238. document.body.appendChild(mask);
  239. return mask;
  240. },
  241. close:function(uid){
  242. var mask=document.querySelector("#"+uid) ;
  243. if( (mask!=undefined)&&(mask!=null) ){ mask.parentNode.removeChild(mask);};
  244. }
  245. }//---------------------------------
  246. var jdialog={
  247. /*
  248. //弹出输入窗口
  249. var html='<div id="edt" contenteditable="true" style="flex:1;border:1px solid lightgray;margin:3px;">aab</div>';
  250. jdlg.show("xx","注意",'请输入:',html,"确定,取消","250px","auto",function(caption){
  251. alert( document.querySelector('#edt').innerText );
  252. jdlg.close("xx");
  253. });
  254.  
  255. //确认对话框
  256. var html='<div id="edt" style="flex:1;margin:3px;padding-left:5px;">你确定删除吗?<br>xx</div>';
  257. jdlg.show("xx1","注意",'',html,"确定,取消","250px","auto",function(caption){
  258. alert( document.querySelector('#edt').innerText );
  259. jdlg.close("xx1");
  260. });
  261.  
  262. //提示框
  263. var html='<div id="edt" style="flex:1;margin:3px;padding-left:5px;">操作成功!<br>xx</div>';
  264. jdlg.show("xx2","注意",'',html,"确定","250px","auto",function(caption){
  265. alert( document.querySelector('#edt').innerText );
  266. jdlg.close("xx2");
  267. });
  268. */
  269.  
  270. uniqueAddDialogStyle:function(uid){
  271. var el=document.querySelector(uid);
  272. if('undefined,null'.indexOf(typeof(document.querySelector(uid)))!=-1) return;//已存在;
  273. el=document.createElement("style");el.id=uid;
  274. var css=`
  275. screenmask,.screenmask{
  276. display:flex;flex-direction:row;justify-content:center;align-items:center;
  277. z-index: 999999;
  278. position: absolute;left:0px;top:0px;right:0px;bottom:0px;
  279. background:rgba(100, 100, 100,.7);
  280. font-size: 16px;
  281. }
  282.  
  283. dlgform,.dlgform{
  284. display:flex;flex-direction:column;justify-content:flex-start;
  285. z-index: 9999;font-size: 16px;
  286. left:0; right:0; top:0; bottom:0;
  287. margin:auto;
  288.  
  289. background-color:white;padding:5px;
  290. border-radius:5px;
  291. font-size:1.1rem;
  292. }
  293. dlgheader,.dlgheader{
  294. display:flex;flex-direction:column;justify-content:center;align-items:flex-start;
  295. padding-left:15px;
  296. height:35px;flex-shrink:0;
  297. color:#3582d8;
  298. }
  299. lineborder{/* F8F8FF */
  300. display:flex;height:0px;border:1px solid #dd;border-bottom:0px;;transform:scale(0.9,0.1);
  301. }
  302. dlghint,.dlghint{
  303. padding-left:6px;
  304. height:25px;font-size:1.0rem;
  305. display:flex;flex-direction:column;justify-content:center;align-items:stretch;
  306. }
  307. dlgbody,.dlgbody{
  308. flex:1;
  309. margin:5px;margin-top:0px;
  310. display:flex;flex-direction:column;justify-content:center;align-items:stretch;
  311. }
  312. dlgfooter,.dlgfooter{
  313. display:flex;flex-direction:row;justify-content:center;align-items:center;
  314. flex-shrink:0;
  315. height:38px;
  316. }
  317. dlgbutton,.dlgbutton{
  318. flex:1;
  319. display:flex;flex-direction:row;justify-content:center;align-items:center;
  320. background-color:#3582d8;
  321. color:white;
  322. margin:3px;
  323. border-radius:3px;
  324. height:30px;flex-shrink:0;
  325. }
  326. `;
  327. el.innerHTML=css;
  328. document.getElementsByTagName("head")[0].appendChild(el);
  329. },
  330. show:function(uid,caption,hintHtml,bodyHtml,btncaptions,width,height,Fn_click_caption ){
  331. this.uniqueAddDialogStyle('dlgcss'+uid);
  332. var btnstr='';
  333. var captionarr=btncaptions.split(",");
  334. for(var i=0;i<captionarr.length;i++){
  335. if(captionarr[i].trim()=='')continue;
  336. btnstr=btnstr+'<div class="dlgbutton" >'+captionarr[i]+'</div>';
  337. }
  338. var btnHtml=btnstr;
  339. hintHtml=(hintHtml.trim()=='')?'':`<dlghint>${hintHtml}</dlghint>`;
  340. var thiz=this;
  341. var html =`
  342. <dlgform style="width:${width};height:${height};">
  343. <dlgheader class="">${caption}</dlgheader>
  344. <lineborder></lineborder>
  345. ${hintHtml}
  346. <dlgbody class="" >${bodyHtml}</dlgbody>
  347. <div class="dlgfooter">${btnHtml}</div>
  348. </dlgform>
  349. `;
  350. var screenmask=jmask.show('dlgmask'+uid);
  351. screenmask.innerHTML=html;
  352. //绑定事件
  353. var list= screenmask.querySelectorAll('.dlgbutton');
  354. for (const button of list) {
  355. button.addEventListener('click', function(event) {
  356. var caption=event.target.innerText.trim();
  357. Fn_click_caption(caption);
  358. thiz.close('dlgmask'+uid);//确保关闭
  359. })
  360. };
  361.  
  362. return screenmask;
  363. },
  364. close:function(uid){
  365. var mask=document.querySelector("#"+uid) ;
  366. if( (mask!=undefined)&&(mask!=null) ){ mask.parentNode.removeChild(mask);};
  367. }
  368. }
  369.  
  370. jinputdialog={
  371. show(uid,caption,hintHtml,defautInput,width,height,fn_btncaption_inputvalue){
  372. //弹出输入窗口
  373. var contentHtml=`<div id="inputedit${uid}" contenteditable="true" style="flex:1;border:1px solid lightgray;margin:3px;">${defautInput}</div>`;
  374. jdialog.show(uid,caption,hintHtml,contentHtml,"确定,取消",width,height,function(btncaption){
  375. fn_btncaption_inputvalue(btncaption, document.querySelector(`#inputedit${uid}`).innerText.trim() );
  376. jdialog.close(`inputedit${uid}`);
  377. });//------
  378. }
  379. }
  380. jconfirmdialog={
  381. show(uid,caption,confirmText,width,height,fn_btncaption){
  382. //弹出输入窗口
  383. var contentHtml=`<div id="inputedit${uid}" style="flex:1;border:margin:15px;padding:5px;shrink:0;">${confirmText}</div>`;
  384. jdialog.show(uid,caption,'',contentHtml,"确定,取消",width,height,function(btncaption){
  385. fn_btncaption(btncaption );
  386. jdialog.close(`inputedit${uid}`);
  387. });//------
  388. }
  389. }
  390. jmsgdialog={
  391. show(uid,caption,confirmText,width,height,fn_btncaption){
  392. //弹出输入窗口
  393. var contentHtml=`<div id="inputedit${uid}" style="flex:1;border:margin:15px;padding:5px;">${confirmText}</div>`;
  394. jdialog.show(uid,caption,'',contentHtml,"确定",width,height,function(btncaption){
  395. if(fn_btncaption!=undefined) fn_btncaption(btncaption );
  396. jdialog.close(`inputedit${uid}`);
  397. });//------
  398. }
  399. }
  400. /*
  401. //确认对话框
  402. var html='<div id="edt" style="flex:1;margin:3px;padding-left:5px;">你确定删除吗?<br>xx</div>';
  403. jdlg.show("xx1","注意",'',html,"确定,取消","250px","auto",function(caption){
  404. alert( document.querySelector('#edt').innerText );
  405. jdlg.close("xx1");
  406. });
  407.  
  408. //提示框
  409. var html='<div id="edt" style="flex:1;margin:3px;padding-left:5px;">操作成功!<br>xx</div>';
  410. jdlg.show("xx2","注意",'',html,"确定","250px","auto",function(caption){
  411. alert( document.querySelector('#edt').innerText );
  412. jdlg.close("xx2");
  413. });
  414. */
  415. var jfloatDiv={
  416. show(uid,css){
  417. this.close(uid);
  418. var el = document.createElement('div'); el.id=uid;
  419. if(css==undefined)css=`
  420. display:flex;flex-direction:column;justify-content:center;
  421. border:1px solid red;
  422. position: absolute;z-index:99999;
  423. background-color:white;
  424. right:0px;top:0px;
  425. `;
  426. el.style.cssText=css;
  427. document.body.appendChild(el);
  428. el.innerHTML=`
  429. hello
  430. `;
  431. return el;
  432. },
  433. close(uid){
  434. var el=document.querySelector("#"+uid) ;
  435. if( (el!=undefined)&&(el!=null) ){ el.parentNode.removeChild(el);};
  436. }
  437. }
  438. jtoast={
  439. show:function(msg,duration){//toast('这是一个弹框',2000)
  440. duration=isNaN(duration)?500:duration;
  441. var m = document.createElement('div');
  442. m.innerHTML = msg;
  443. 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;";
  444. document.body.appendChild(m);
  445. window.jktoast777=m;
  446. setTimeout(function() {
  447. var d = 0.5;
  448. m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
  449. m.style.opacity = '0';
  450. setTimeout(function() { document.body.removeChild(m) }, d * 1000);
  451. }, duration);
  452. },
  453. close:function(){
  454. document.body.removeChild( window.jktoast777 );
  455. }
  456. }
  457.  
  458. var jloading={
  459. close:function(uid){
  460. clearInterval(window.jkinterval777);
  461. jmask.close('mask'+uid);
  462. },
  463. show:function(uid,txt,timeout){
  464. window.jkinterval777=-1;
  465. if(txt==undefined)txt='waiting ';
  466. var obj=document.createElement('div');
  467. obj.innerText==txt+'∴';
  468. obj.id='hint'+uid;
  469. obj.style.cssText=`
  470. max-width:60%;min-width:100px;min-height:35px;border-radius: 4px;
  471. display:flex;flex-direction:row;justify-content:center;align-items:center;
  472. z-index:999999;
  473. background:gray;
  474. radius:8px;
  475. height:auto;padding:3px 15px;
  476. font-size:18px;color:white;
  477. `;
  478. obj.style.display='';
  479. window.jkinterval777=setInterval(function(){
  480. if(obj.innerText==''){ obj.innerText=txt+'∴';return;}
  481. if(obj.innerText==txt+'∴'){ obj.innerText=txt+'∵';return;}
  482. if(obj.innerText==txt+'∵'){ obj.innerText=txt+'∴';return;}
  483. return;//100
  484. },550);
  485. var mask=jmask.show('mask'+uid,0);
  486. mask.appendChild(obj);
  487. obj.innerText==txt+'∴';
  488. //默认3秒定时关闭
  489. if( typeof(timeou)!= "undefined" ) timeout=3*1000;
  490. setTimeout(function(){
  491. clearInterval(window.jkinterval777);
  492. jmask.close('mask'+uid);
  493. },timeout );
  494. }//show;
  495. }//--------------------------------------
  496. var jwidget={
  497. mask:jmask,
  498. toast:jtoast,
  499. loading:jloading,
  500. dialog:jdialog,
  501. inputdialog:jinputdialog,
  502. confirmdialog:jconfirmdialog,
  503. msgdialog:jmsgdialog,
  504. }