mylib

我的工具

当前为 2024-08-05 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/435697/1422609/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.mbefore('z','',`class`,'rwf');
  16. win.myaddStyle(`my-a{border-style: inset}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.mappend('my-btns','',`class`,'rwf');
  18. if(!gv('fixbar',0)||win.top!==win.self){//如果不固定,或在iframe
  19. Element.prototype.mbindDrag(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.mappend('style',css,`class`,className);
  42. }
  43. Element.prototype.mappend = function(tagName, innerHTML, ...attributes) {//z.mappend('x','content','idk','true','data-s').on("click",()=>{alert(1)}).mappend('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.setAttribute(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.mafter = 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.setAttribute(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.mbefore = 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.setAttribute(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.mappend('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. 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.map(url => {
  116. const match = url.match(/:\/\/(www\.)?([^\/]+)/);
  117. if (match && match[2]) {
  118. const domainParts = match[2].split('.'),name=domainParts.length > 1 ? domainParts.slice(0, -1).join('.') : url=domainParts[0];
  119. win.mybtns.mappend('a',name,`href`,url,'class','my-a');
  120. }
  121. return null;
  122. });
  123. }
  124. Element.prototype.mbindDrag=(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.mywaitForElement=(selector,timeout=3e4)=>{//查找ele.30000
  144. return new Promise((resolve, reject) => {
  145. const intervalTime=500;
  146. const startTime=Date.now();
  147. const interval=setInterval(() => {
  148. const element=document.querySelector(selector);
  149. if (element) {
  150. clearInterval(interval);
  151. resolve(element);//找到则返回函数
  152. } else if (Date.now() - startTime > timeout) {
  153. clearInterval(interval);
  154. reject(`Timeout waiting for element ${selector}`);//超时则返回字符串
  155. }//继续寻找
  156. }, intervalTime);
  157. });
  158. }
  159. win.mywaitForElements=(selector,timeout=3e4)=>{//查找eles
  160. return new Promise((resolve, reject) => {
  161. const intervalTime=500,startTime=Date.now(),interval=setInterval(() => {
  162. const elements=document.querySelectorAll(selector);
  163. if (elements) {
  164. clearInterval(interval);
  165. resolve(elements);//找到则返回函数
  166. } else if (Date.now() - startTime > timeout) {
  167. clearInterval(interval);
  168. reject(`Timeout waiting for element ${selector}`);//超时则返回字符串
  169. }//继续寻找
  170. }, intervalTime);
  171. });
  172. }
  173. win.my=new My();
  174. })(window.unsafeWindow);