myUtils

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

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

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

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