myUtils

自分用、dom取得等の機能追加。

当前为 2022-06-07 提交的版本,查看 最新版本

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

  1. // ==UserScript==
  2. // @name myUtils
  3. // @description 自分用、dom取得等の機能追加。
  4. // @include *
  5. // @noframes
  6. // ==/UserScript==
  7. class my{
  8. //dependency:myDomAppendBro
  9. static addStyle(css,className=void(0)){
  10. this.domAppendBro('style',document.head,css,`class`,className)
  11. }
  12.  
  13. //my.domAppendSon('tag',document.body,'content','idk','true','data-s')
  14. static domAppendSon(tag,dom,content){
  15. if(!tag) return;
  16. const son=typeof tag=='string'?
  17. document.createElement(tag):tag instanceof HTMLElement||DocumentFragment?
  18. tag:void(0),len=arguments.length;
  19. if(dom instanceof HTMLElement||dom instanceof DocumentFragment)dom.append(son);
  20. if(content)son.append(content);
  21. if (len>3) {
  22. for(let i=3;i<len;i+=2){
  23. son.setAttribute([arguments[i]],arguments[i+1])
  24. }
  25. }return son;
  26. }
  27.  
  28. //my.domAppendBro('div',document.body,'','suck','1','dick')
  29. static domAppendBro(tag,dom,content){
  30. if(!tag) return;
  31. const bro=typeof tag=='string'?
  32. document.createElement(tag):tag instanceof HTMLElement||DocumentFragment?
  33. tag:void(0),len=arguments.length;
  34. if(dom instanceof HTMLElement||dom instanceof DocumentFragment)dom.after(bro);
  35. if(content)bro.append(content);
  36. if (len>3) {
  37. for(let i=3;i<len;i+=2){
  38. bro.setAttribute([arguments[i]],arguments[i+1])
  39. }
  40. }return bro;
  41. }
  42.  
  43. // my.domInsertBro('div',document.head,'','suck','1','dick')
  44. static domInsertBro(tag,dom,content){
  45. if(!tag) return;
  46. const bro=typeof tag=='string'?
  47. document.createElement(tag):tag instanceof HTMLElement||DocumentFragment?
  48. tag:void(0),len=arguments.length;
  49. if(dom instanceof HTMLElement||dom instanceof DocumentFragment)dom.before(bro);
  50. if(content)bro.append(content);
  51. if (len>3) {
  52. for(let i=3;i<len;i+=2){
  53. bro.setAttribute([arguments[i]],arguments[i+1])
  54. }
  55. }return bro;
  56. }
  57.  
  58. //enable or disable style with queryStr
  59. static switchStyle(...styleQueryStr){
  60. styleQueryStr.forEach(e=>{
  61. const style=document.querySelectorAll('style'+e).forEach(e=>{
  62. e.type!='0'?e.type=0:e.type="";
  63. });
  64. })
  65. }
  66.  
  67. //my.seconds2date(new Date().getTime())
  68. static seconds2date(seconds){
  69. const date=new Date(seconds),year=date.getFullYear(),month=date.getMonth()+1,day=date.getDate();
  70. const hour=date.getHours(),minute=date.getMinutes(),second=date.getSeconds(),milliseconds=date.getMilliseconds(),currentTime=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+":"+milliseconds;
  71. return currentTime
  72. }
  73.  
  74. //跳出登录框后再执行
  75. static fuckTiebaLogin(userName,password){
  76. document.getElementById('TANGRAM__PSP_5__footerULoginBtn').click();
  77. document.getElementById('TANGRAM__PSP_5__userName').outerHTML+='';
  78. document.getElementById('TANGRAM__PSP_5__password').outerHTML+='';
  79. document.getElementById('TANGRAM__PSP_5__userName').value=userName;
  80. document.getElementById('TANGRAM__PSP_5__password').value=password;
  81. return document.getElementById('TANGRAM__PSP_5__submit').click();
  82. }
  83.  
  84. static getValue(k,aDefault){
  85. const val=localStorage.getItem("XB_"+k)
  86. if (!val && 'undefined' != typeof aDefault) return aDefault;
  87. return val;
  88. }
  89.  
  90. //setValue('k', 'v')
  91. static setValue(k, v){
  92. localStorage.setItem("XB_"+k, v);
  93. }
  94.  
  95. //deleteValue('k')
  96. static deleteValue(k){
  97. if(k.indexOf("XB_")!=-1) localStorage.removeItem(k);
  98. }
  99.  
  100. static listValues(){
  101. let list=[],j=0,k;
  102. for (let i=0; i<localStorage.length;i++) {
  103. k=localStorage.key(i);
  104. if(k.indexOf("XB_")!=-1) list[j++]=localStorage.key(i);
  105. }return list;
  106. }
  107.  
  108. //dependency:myXbListValues
  109. static clearValues(){
  110. this.listValues().forEach(e=>{
  111. localStorage.removeItem(e)
  112. })
  113. }
  114.  
  115. //get len of json obj
  116. static getJsonLen(jsonObj){
  117. let jsonLen=0, key;
  118. for (key in jsonObj) {
  119. if (jsonObj.hasOwnProperty(key)) jsonLen++;
  120. }return jsonLen;
  121. }
  122.  
  123. //syn console.log
  124. static log(text){
  125. console.log(JSON.stringify(text))
  126. }
  127.  
  128. //dependency:myAddStyle,myDomAppendSon
  129. //my.addBtns(()=>{},"func1",e=>{confirm(e.target.id)},function(e){prompt(e.target.outerHTML)},function test(e){return 1})
  130. static addBtns(...params){
  131. const myDiv=document.querySelector('#my-btn-container')||this.domAppendBro('div',document.body,'',`id`,'my-btn-container',`class`,void(0)),btns=[];
  132. if(!document.querySelector('style.my-css'))this.addStyle(`#my-btn-container{display:flex;flex-wrap:wrap;z-index:1650729359811;position: fixed!important;right:0;top:0}.my-btn{user-select: none!important}`,'my-css undefined');
  133. for(let i=0;i<params.length;){
  134. if(typeof params[i]==="function"){
  135. const btn=this.domAppendSon('button',myDiv,params[i].name+'|','class','my-btn');
  136. btn.addEventListener('click',params[i++]);
  137. btns[btns.length]=btn;
  138. }else{
  139. const btn=this.domAppendSon('button',myDiv,params[i++]+'|','class','my-btn');
  140. btn.addEventListener('click',params[i]);++i;
  141. btns[btns.length]=btn;
  142. }
  143. }return btns
  144. }
  145.  
  146. //dependency:myAddStyle,myDomAppendSon
  147. //my.addAs("哔哩哔哩","https://www.bilibili.com/","百度","https://www.baidu.com/")
  148. static addAs(...args){
  149. const myDiv=document.querySelector('#my-btn-container')||this.domAppendBro('div',document.body,'',`id`,'my-btn-container',`class`,void(0));
  150. if(!document.querySelector('style.my-css')) this.addStyle(`#my-btn-container{display:flex;flex-wrap:wrap;z-index:1650729359811;position: fixed!important;right:0;top:0}.my-btn{user-select: none!important}`,'my-css undefined');
  151. for(let i=0;i<args.length;i+=2){
  152. this.domAppendSon('a',myDiv,args[i]+'|',`href`,args[i+1]);
  153. }
  154. }
  155.  
  156. //prevent document.title from being changed
  157. static fixTitle(){
  158. Object.defineProperty(document,"title",{
  159. writable:false
  160. })
  161. }
  162.  
  163. }