mylib

我的工具

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/435697/1534947/mylib.js

  1. //==UserScript==
  2. // @name mylib
  3. // @description 我的工具
  4. // @grant GM_setValue
  5. // @grant GM_getValue
  6. // @grant GM.xmlHttpRequest
  7. //==/UserScript==
  8. ;(function(win){
  9. const My=function(){//构造函数,需配合实例方法,与this,prototype有关
  10. const head=document.documentElement.firstElementChild,gsv=GM_setValue,ggv=GM_getValue,gxhr=GM.xmlHttpRequest;
  11. if(head&&head.tagName==='Z'){
  12. win.myz=head;
  13. win.mybtns=head.querySelector('my-btns');
  14. }else{
  15. win.al=win.alert;win.cl=win.console.log;win.doc=win.document;
  16. win.dq=selector=>document.querySelector(selector);win.dqa=selector=>document.querySelectorAll(selector);
  17. win.si=setInterval;win.st=setTimeout;
  18. win.myz=head.mybefore('z','',`className`,'rwf');//myzone;rwf纯标识用
  19. win.myaddstyle(`.my-a{border: 1px solid black!important;font-size: medium;}my-btns{display:block;z-index:9195129;position:fixed;height: min-content;}.my-btn{user-select: none;font-size: initial!important;}`,'my-btns');
  20. win.mybtns=win.myz.myappend('my-btns','',`className`,'rwf');
  21. if(!ggv('fixbar',0)||win.top!==win.self) Element.prototype.mybinddrag(win.mybtns);//如果不固定,或在iframe
  22. if(win.top===win.self){
  23. win.mybtns.onmouseup=e=>{
  24. gsv('left',win.mybtns.style.left);
  25. gsv('top',win.mybtns.style.top);
  26. }
  27. }
  28. win.mybtns.style.left=ggv('left',0);win.mybtns.style.top=ggv('top',0);
  29. win.myaddbtns(function 拽(){},function X(){win.myz.remove()},).children[0].oncontextmenu=e=>{//右键 固定/不固定 按钮组
  30. e.preventDefault();//取消默认
  31. gsv('fixbar',ggv('fixbar')?0:1);
  32. location.reload();
  33. }
  34. }
  35. return My;
  36. }
  37. //My.静态方法 My.prototype实例方法(如本案例中my.prototype.constructor.toString())
  38. // My.prototype.test1=function(){};My.test2=function(){}
  39. win.myaddstyle=(css,className='rwf')=>win.myz.myappend('style',css,`className`,className);//function和lambda this指向不同,后者没有prototype、arguments,后者不能new/作为构造函数;
  40. Element.prototype.qs=Document.prototype.qs=function(selector){return this.querySelector(selector)||null}
  41. Element.prototype.qsa=Document.prototype.qsa=function(selector){return this.querySelectorAll(selector)||null}
  42. Element.prototype.myappend=function(tagName,innerHTML,...attributes){//myz.myappend('test','content','key','value').ael("click",()=>{alert(1)}).myappend('y','test','idk','true','data-s')
  43. const ele=document.createElement(tagName);
  44. ele.innerHTML=innerHTML;
  45. for(let i=0;i<attributes.length;i+=2) ele[attributes[i]]=attributes[i+1];
  46. this.appendChild(ele);
  47. return ele;
  48. }
  49. Element.prototype.myinsert=function(tagName, innerHTML,...attributes) {//myz.myinsert('test','content','key','value').ael('',()=>{alert(1)})
  50. const ele = document.createElement(tagName);
  51. ele.innerHTML = innerHTML;
  52. for (let i = 0; i < attributes.length; i += 2) ele[attributes[i]] = attributes[i + 1];
  53. const firstChild = this.firstChild;
  54. this.insertBefore(ele, firstChild);
  55. return ele;
  56. }
  57. Element.prototype.myafter=function(tagName,innerHTML,...attributes){//myz.myafter('test','content','key','value').ael("click",()=>{alert(1)})
  58. const ele=document.createElement(tagName);
  59. ele.innerHTML=innerHTML;
  60. for(let i=0;i<attributes.length;i+=2) ele[attributes[i]]=attributes[i+1];
  61. this.after(ele);
  62. return ele;
  63. }
  64. Element.prototype.mybefore=function(tagName,innerHTML,...attributes){//myz.mybefore('test','content','key','value').ael('',()=>{alert(1)})
  65. const ele=document.createElement(tagName);
  66. ele.innerHTML=innerHTML;
  67. for(let i=0;i<attributes.length;i+=2) ele[attributes[i]]=attributes[i+1];
  68. this.before(ele);
  69. return ele;
  70. }
  71. Element.prototype.ael=function(event,handler){
  72. if (!event) event = 'click';//不传事件,默认为'click
  73. this.addEventListener(event,handler);
  74. return this;
  75. }
  76. win.myaddbtns=(...args)=>{//myaddbtns(()=>{},e=>{confirm(e.target.id)},function(e){prompt(e.target.outerHTML)},function test(e){return 1})
  77. const len=args.length;
  78. for(let i=0;i<len;i++) win.mybtns.myappend('input','',`type`,'button','class','my-btn','value',args[i].name).ael('click',args[i]);
  79. return win.mybtns;
  80. }
  81. win.myeods=()=>win.myz.querySelectorAll('style').forEach(e =>{//enable or disable style in win.myz
  82. e.type === '0'? e.type='' : 0
  83. });
  84. win.mysohe=(...args)=>args.forEach(e=>{//show or hide elements
  85. if(typeof e!=="string") e.style.display==='none'?e.style.display='initial':'none';
  86. else win.dqa(e).forEach(e=>{e.style.display==='none'?e.style.display='initial':'none'});
  87. });
  88. win.myms2d=ms=>{//ms to date
  89. if(typeof ms!=="number") ms=new Date().getTime();//如果参数为空,输出当下时间
  90. const date=new Date(ms),y=date.getFullYear(),m=String(date.getMonth()+1).padStart(2,'0'),// 月份补0,保证两位数格式
  91. d=String(date.getDate()).padStart(2,'0'),// 日期补0
  92. h=String(date.getHours()).padStart(2,'0'),
  93. min=String(date.getMinutes()).padStart(2,'0'),
  94. s=String(date.getSeconds()).padStart(2,'0'),
  95. milliseconds=String(date.getMilliseconds()).padStart(3,'0');
  96. return `${y}年${m}月${d}日${h}:${min}:${s}:${milliseconds}`;
  97. }
  98. win.mytieba=(uname,pwd)=>{
  99. win.dq('[name="userName"]').value=uname;
  100. win.dq('[name="password"]').value=pwd;
  101. return win.dq('[value="登录"]').click();
  102. }
  103. win.myfixprop=function(obj,prop){//lock property;myfixprop(document,"title")
  104. Object.defineProperty(obj,prop,{writable:false});
  105. }
  106. win.myaddas=(...urls)=>urls.forEach(url=>{//myaddas("https://www.bilibili.com/","https://www.baidu.com/")
  107. gxhr({url:url}).then(resp=>resp.responseText).then(html=>{
  108. const parser=new DOMParser(),doc=parser.parseFromString(html,'text/html'),title=doc.title,u=new URL(url);
  109. win.mybtns.myappend('a',title,'href',u.href,`className`,'my-a my-btn');
  110. }).catch(err=>win.cl(err));
  111. });
  112. Element.prototype.mybinddrag=ele=>{//鼠标拖动
  113. ele.onmousedown=function(ev){//if(ev.target.tagName==='TEXTAREA') return;
  114. const diffX=ev.clientX-ele.offsetLeft,diffY=ev.clientY-ele.offsetTop,iw=win.innerWidth,ih=win.innerHeight;
  115. document.onmousemove=function(ev){
  116. let moveX=ev.clientX-diffX,moveY=ev.clientY-diffY;
  117. moveX<0?moveX=0:moveX>iw-ele.offsetWidth?moveX=iw-ele.offsetWidth:0
  118. moveY<0?moveY=0:moveY>ih-ele.offsetHeight?moveY=ih-ele.offsetHeight:0;
  119. ele.style.left=moveX/iw*100+'%'//moveX+'px';
  120. ele.style.top=moveY/ih*100+'%'//moveY+'px'
  121. }
  122. document.onmouseup=function(ev){this.onmousemove=this.onmouseup=null}
  123. }
  124. }
  125. win.mysleep=time=>new Promise(resolve=> setTimeout(resolve,time));
  126. win.mywaitele=(selector,timeout=3e4)=>{//查找ele,超时30000
  127. return new Promise((resolve,reject)=>{
  128. const 间隔=500,startTime=Date.now(),i=setInterval(()=>{
  129. const element=win.dq(selector);
  130. if (element){
  131. clearInterval(i);
  132. resolve(element);//找到
  133. } else if (Date.now() - startTime > timeout){
  134. clearInterval(i);
  135. reject(`Timeout waiting for element ${selector}`);//超时
  136. }//继续找
  137. },间隔);
  138. });
  139. }
  140. win.mywaiteles=(selector,timeout=3e4)=>new Promise((resolve,reject)=>{//查找eles
  141. const intervalTime=500,startTime=Date.now(),i=win.si(()=>{
  142. const elements=win.dqa(selector);
  143. if (elements.length>0){
  144. clearInterval(i);
  145. resolve(elements);
  146. } else if(Date.now() - startTime > timeout){
  147. clearInterval(i);
  148. reject(`Timeout waiting for element ${selector}`);
  149. }
  150. },intervalTime);
  151. });
  152. win.myshowlv=()=>{//当前页面第几层
  153. let level=0,currentWindow=win;
  154. do{level++;
  155. // al(level+currentWindow.location.href);
  156. if(currentWindow === currentWindow.top) break;
  157. currentWindow=currentWindow.parent;
  158. }while(currentWindow.parent);
  159. win.mybtns.myappend('a',`第${level}层`,'href',win.location.href,'className','my-a');
  160. }
  161. win.my=new My();
  162. })(window.unsafeWindow);