mylib

我的工具

当前为 2025-01-09 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/435697/1517834/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.tagName==='Z'){
  12. win.myzone=head;
  13. win.mybtns=head.querySelector('my-btns');
  14. }else{
  15. win.al=win.alert;
  16. win.cl=win.console.log;
  17. win.myzone=head.mybefore('z','',`class`,'rwf');
  18. win.myaddstyle(`.my-a{border: 1px solid black!important;}my-btns{display:block;z-index:9195129;position:fixed;height: min-content;}.my-btn{user-select: none;font-size: initial!important;}`,'my-btns');
  19. win.mybtns=win.myzone.myappend('my-btns','',`class`,'rwf');
  20. if(!ggv('fixbar',0)||win.top!==win.self) Element.prototype.mybinddrag(win.mybtns);//如果不固定,或在iframe
  21. if(win.top===win.self){
  22. win.mybtns.onmouseup=e=>{
  23. gsv('left',win.mybtns.style.left);
  24. gsv('top',win.mybtns.style.top);
  25. }
  26. }
  27. win.mybtns.style.left=ggv('left',0);
  28. win.mybtns.style.top=ggv('top',0);
  29. win.myaddbtns(function 拽(){}).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(){}
  39. // My.test2=function(){}
  40. win.myaddstyle=function(css,className='rwf'){//function和lambda this指向不同,后者没有prototype、arguments,后者不能new/作为构造函数,
  41. return win.myzone.myappend('style',css,`class`,className);
  42. }
  43. Element.prototype.myappend=function(tagName,innerHTML,...attributes){//z.myappend('x','content','idk','true','data-s').on("click",()=>{alert(1)}).myappend('y','test','idk','true','data-s')
  44. const ele=document.createElement(tagName);
  45. ele.innerHTML=innerHTML;
  46. for(let i=0;i<attributes.length;i+=2) ele[attributes[i]]=attributes[i+1];
  47. this.appendChild(ele);
  48. ele.on=function (event,handler){
  49. this.addEventListener(event,handler);
  50. return this; // 支持链式调用
  51. };
  52. return ele;
  53. }
  54. Element.prototype.myafter=function(tagName,innerHTML,...attributes){
  55. const ele=document.createElement(tagName);
  56. ele.innerHTML=innerHTML;
  57. for(let i=0;i<attributes.length;i+=2) ele[attributes[i]]=attributes[i+1];
  58. this.after(ele);
  59. ele.on=function(event,handler){
  60. this.addEventListener(event,handler);
  61. return this; // 支持链式调用
  62. };
  63. return ele;
  64. }
  65. win.Element.prototype.mybefore=function(tagName,innerHTML,...attributes){
  66. const ele=document.createElement(tagName);
  67. ele.innerHTML=innerHTML;
  68. for(let i=0;i<attributes.length;i+=2) ele[attributes[i]]=attributes[i+1];
  69. this.before(ele);
  70. ele.on=function(event,handler){
  71. this.addEventListener(event,handler);
  72. return this; // 支持链式调用
  73. };
  74. return ele;
  75. }
  76. win.myaddbtns=(...args)=>{//my.addBtns(()=>{},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++){
  79. const btn=win.mybtns.myappend('input','',`type`,'button','class','my-btn','value',args[i].name);
  80. btn.addEventListener('click',args[i]);
  81. }
  82. return win.mybtns;
  83. }
  84. win.myeods=()=>{//enable or disable style in win.myzone
  85. win.myzone.querySelectorAll('style').forEach(e=>{
  86. e.type==='0'?e.type='':0
  87. });
  88. }
  89. win.mysohe=(...args)=>{//show or hide elements
  90. return args.forEach(e=>{
  91. if(typeof e!=="string") e.style.display==='none'?e.style.display='initial':'none';
  92. else{
  93. document.querySelectorAll(e).forEach(e=>{
  94. e.style.display==='none'?e.style.display='initial':'none'
  95. });
  96. }
  97. });
  98. }
  99. win.mys2d=(seconds)=>{//seconds2date//my.s2d(new Date().getTime())
  100. const date=new Date(seconds),year=date.getFullYear(),month=date.getMonth()+1,day=date.getDate(),
  101. hour=date.getHours(),minute=date.getMinutes(),second=date.getSeconds(),milliseconds=date.getMilliseconds(),currentTime=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+":"+milliseconds;
  102. return currentTime;
  103. }
  104. win.mytieba=(uname,pwd)=>{
  105. document.querySelector('[name="userName"]').value=uname;
  106. document.querySelector('[name="password"]').value=pwd;
  107. return document.querySelector('[value="登录"]').click();
  108. }
  109. win.myfixtitle=()=>{//disable title change
  110. Object.defineProperty(document,"title",{
  111. writable:false
  112. });
  113. }
  114. win.myaddas=(...urls)=>{//myaddas("https://www.bilibili.com/","https://www.baidu.com/")
  115. urls.forEach(url=>{
  116. gxhr({
  117. url:url
  118. }).then(resp=>resp.responseText).then(html=>{
  119. const parser=new DOMParser(),doc=parser.parseFromString(html,'text/html'),title=doc.title,u=new URL(url);
  120. win.mybtns.myappend('a',title,'href',u.href,`className`,'my-a my-btn');
  121. }).catch(err=>win.cl(err));
  122. });
  123. }
  124. Element.prototype.mybinddrag=(ele)=>{//鼠标拖动
  125. ele.onmousedown=function(ev){//if(ev.target.tagName==='TEXTAREA') return;
  126. const diffX=ev.clientX-ele.offsetLeft,diffY=ev.clientY-ele.offsetTop,iw=win.innerWidth,ih=win.innerHeight;
  127. document.onmousemove=function(ev){
  128. let moveX=ev.clientX-diffX,moveY=ev.clientY-diffY;
  129. moveX<0?moveX=0:moveX>iw-ele.offsetWidth?moveX=iw-ele.offsetWidth:0
  130. moveY<0?moveY=0:moveY>ih-ele.offsetHeight?moveY=ih-ele.offsetHeight:0;
  131. ele.style.left=moveX/iw*100+'%'//moveX + 'px';
  132. ele.style.top=moveY/ih*100+'%'//moveY + 'px'
  133. }
  134. document.onmouseup=function(ev){
  135. this.onmousemove=null;
  136. this.onmouseup=null;
  137. }
  138. }
  139. }
  140. win.mysleep=time=>{
  141. return new Promise((resolve)=> setTimeout(resolve,time));
  142. }
  143. win.mywaitele=(selector,timeout=3e4)=>{//查找ele.30000
  144. return new Promise((resolve,reject)=>{
  145. const intervalTime=500,startTime=Date.now(),interval=setInterval(()=>{
  146. const element=document.querySelector(selector);
  147. if (element){
  148. clearInterval(interval);
  149. resolve(element);//找到则返回函数
  150. } else if (Date.now() - startTime > timeout){
  151. clearInterval(interval);
  152. reject(`Timeout waiting for element ${selector}`);//超时则返回字符串
  153. }//继续寻找
  154. },intervalTime);
  155. });
  156. }
  157. win.mywaiteles=(selector,timeout=3e4)=>{//查找eles
  158. return new Promise((resolve,reject)=>{
  159. const intervalTime=500,startTime=Date.now(),interval=setInterval(()=>{
  160. const elements=document.querySelectorAll(selector);
  161. if (elements.length>0){
  162. clearInterval(interval);
  163. resolve(elements);//找到则返回函数
  164. } else if (Date.now() - startTime > timeout){
  165. clearInterval(interval);
  166. reject(`Timeout waiting for element ${selector}`);//超时则返回字符串
  167. }//继续寻找
  168. },intervalTime);
  169. });
  170. }
  171. win.my=new My();
  172. })(window.unsafeWindow);