mylib

我的工具

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

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

  1. // ==UserScript==
  2. // @name mylib
  3. // @description 我的工具
  4. // @grant GM_setValue
  5. // @grant GM_getValue
  6. // ==/UserScript==
  7. ;(function(win){
  8. const My =function(){//构造函数,需配合实例方法,与this,prototype有关
  9. const e=document.documentElement.firstElementChild,sv=GM_setValue,gv=GM_getValue;
  10. if(e.tagName==='Z'){
  11. win.myzone=e;
  12. win.mybtns=e.querySelector('my-btns');
  13. }else {
  14. win.cl=win.console.log;win.al=win.alert; // win.gv=gv;win.sv=sv
  15. win.myzone=e.mybefore('z','',`class`,'rwf');
  16. win.myaddstyle(`.my-a{border-style: inset!important;}my-btns{display:block;z-index:9195129;position:fixed;height: min-content;}.my-btn{user-select: none;font-size: initial!important;}`,'my-btns');
  17. win.mybtns=win.myzone.myappend('my-btns','',`class`,'rwf');
  18. if(!gv('fixbar',0)||win.top!==win.self){//如果不固定,或在iframe
  19. Element.prototype.mybinddrag(win.mybtns);
  20. }
  21. if(win.top===win.self){
  22. win.mybtns.onmouseup=e=>{
  23. sv('left',win.mybtns.style.left);
  24. sv('top',win.mybtns.style.top);
  25. }
  26. }
  27. win.mybtns.style.left=gv('left',0);
  28. win.mybtns.style.top=gv('top',0);
  29. win.myaddbtns(function 拽(){}).children[0].oncontextmenu=e=>{//右键 固定/不固定 按钮组
  30. e.preventDefault();//取消默认
  31. sv('fixbar',gv('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) => {//my.addAs("https://www.bilibili.com/","https://www.baidu.com/")
  115. return urls.forEach(url=>{
  116. const u=new URL(url);
  117. win.mybtns.myappend('a',u.hostname,`href`,url,'class','my-a my-btn');
  118. });
  119. }
  120. Element.prototype.mybinddrag=(ele)=>{//鼠标拖动
  121. ele.onmousedown=function(ev){//if(ev.target.tagName==='TEXTAREA') return;
  122. const diffX=ev.clientX-ele.offsetLeft,diffY=ev.clientY-ele.offsetTop,iw=win.innerWidth,ih=win.innerHeight;
  123. document.onmousemove=function(ev){
  124. let moveX=ev.clientX-diffX,moveY=ev.clientY-diffY;
  125. moveX<0?moveX=0:moveX>iw-ele.offsetWidth?moveX=iw-ele.offsetWidth:0
  126. moveY<0?moveY=0:moveY>ih-ele.offsetHeight?moveY=ih-ele.offsetHeight:0;
  127. ele.style.left=moveX/iw*100+'%'//moveX + 'px';
  128. ele.style.top=moveY/ih*100+'%'//moveY + 'px'
  129. }
  130. document.onmouseup=function(ev){
  131. this.onmousemove=null;
  132. this.onmouseup=null;
  133. }
  134. }
  135. }
  136. win.mysleep=(time)=>{
  137. return new Promise((resolve) => setTimeout(resolve, time));
  138. }
  139. win.mywaitele=(selector,timeout=3e4)=>{//查找ele.30000
  140. return new Promise((resolve, reject) => {
  141. const intervalTime=500,startTime=Date.now(),interval=setInterval(() => {
  142. const element=document.querySelector(selector);
  143. if (element) {
  144. clearInterval(interval);
  145. resolve(element);//找到则返回函数
  146. } else if (Date.now() - startTime > timeout) {
  147. clearInterval(interval);
  148. reject(`Timeout waiting for element ${selector}`);//超时则返回字符串
  149. }//继续寻找
  150. }, intervalTime);
  151. });
  152. }
  153. win.mywaiteles=(selector,timeout=3e4)=>{//查找eles
  154. return new Promise((resolve, reject) => {
  155. const intervalTime=500,startTime=Date.now(),interval=setInterval(() => {
  156. const elements=document.querySelectorAll(selector);
  157. if (elements.length>0) {
  158. clearInterval(interval);
  159. resolve(elements);//找到则返回函数
  160. } else if (Date.now() - startTime > timeout) {
  161. clearInterval(interval);
  162. reject(`Timeout waiting for element ${selector}`);//超时则返回字符串
  163. }//继续寻找
  164. }, intervalTime);
  165. });
  166. }
  167. win.my=new My();
  168. })(window.unsafeWindow);