myUtils

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

目前為 2022-05-21 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/435697/1052585/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. //my.domAppendSon('tag',document.body,'content','idk','true','data-s')
  13. static domAppendSon(tag,dom,content){
  14. if(!tag) return;
  15. const son=typeof tag=='string'?
  16. document.createElement(tag):tag instanceof HTMLElement?
  17. tag:void(0),len=arguments.length;
  18. if(dom instanceof HTMLElement||dom instanceof DocumentFragment)dom.append(son);
  19. if(content)son.append(content);
  20. if (len>3) {
  21. for(let i=3;i<len;i+=2){
  22. son.setAttribute([arguments[i]],arguments[i+1])
  23. }
  24. }return son;
  25. }
  26. //my.domAppendBro('div',document.body,'','suck','1','dick')
  27. static domAppendBro(tag,dom,content){
  28. if(!tag) return;
  29. const bro=typeof tag=='string'?
  30. document.createElement(tag):tag instanceof HTMLElement?
  31. tag:void(0),len=arguments.length;
  32. if(dom instanceof HTMLElement||dom instanceof DocumentFragment)dom.after(bro);
  33. if(content)bro.append(content);
  34. if (len>3) {
  35. for(let i=3;i<len;i+=2){
  36. bro.setAttribute([arguments[i]],arguments[i+1])
  37. }
  38. }return bro;
  39. }
  40. // my.domInsertBro('div',document.head,'','suck','1','dick')
  41. static domInsertBro(tag,dom,content){
  42. if(!tag) return;
  43. const bro=typeof tag=='string'?
  44. document.createElement(tag):tag instanceof HTMLElement?
  45. tag:void(0),len=arguments.length;
  46. if(dom instanceof HTMLElement||dom instanceof DocumentFragment)dom.before(bro);
  47. if(content)bro.append(content);
  48. if (len>3) {
  49. for(let i=3;i<len;i+=2){
  50. bro.setAttribute([arguments[i]],arguments[i+1])
  51. }
  52. }return bro;
  53. }
  54. //enable or disable style with className
  55. static switchStyle(...styleClassNames){
  56. styleClassNames.forEach(e=>{
  57. const style=document.querySelectorAll('style.'+e).forEach(e=>{
  58. e.type!='0'?e.type=0:e.type="";
  59. });
  60. })
  61. }
  62. //my.seconds2date(new Date().getTime())
  63. static seconds2date(seconds){
  64. const date=new Date(seconds),year=date.getFullYear(),month=date.getMonth()+1,day=date.getDate();
  65. const hour=date.getHours(),minute=date.getMinutes(),second=date.getSeconds(),milliseconds=date.getMilliseconds(),currentTime=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+":"+milliseconds;
  66. return currentTime
  67. }
  68. //跳出登录框后再执行
  69. static fuckTiebaLogin(userName,password){
  70. document.getElementById('TANGRAM__PSP_5__footerULoginBtn').click();
  71. document.getElementById('TANGRAM__PSP_5__userName').outerHTML+='';
  72. document.getElementById('TANGRAM__PSP_5__password').outerHTML+='';
  73. document.getElementById('TANGRAM__PSP_5__userName').value=userName;
  74. document.getElementById('TANGRAM__PSP_5__password').value=password;
  75. return document.getElementById('TANGRAM__PSP_5__submit').click();
  76. }
  77. static getValue(k,aDefault){
  78. const val = localStorage.getItem("XB_"+k)
  79. if (!val && 'undefined' != typeof aDefault) return aDefault;
  80. return val;
  81. }
  82. //setValue('k', 'v')
  83. static setValue(k, v){
  84. localStorage.setItem("XB_"+k, v);
  85. }
  86. //deleteValue('k')
  87. static deleteValue(k){
  88. if(k.indexOf("XB_")!=-1) localStorage.removeItem(k);
  89. }
  90. static listValues(){
  91. let list=[],j=0,k;
  92. for (let i=0; i<localStorage.length;i++) {
  93. k=localStorage.key(i);
  94. if(k.indexOf("XB_")!=-1) list[j++] = localStorage.key(i);
  95. }return list;
  96. }
  97. //dependency:myXbListValues
  98. static clearValues(){
  99. this.listValues().forEach(e=>{
  100. localStorage.removeItem(e)
  101. })
  102. }
  103. //get len of json obj
  104. static getJsonLen(jsonObj){
  105. let jsonLen=0, key;
  106. for (key in jsonObj) {
  107. if (jsonObj.hasOwnProperty(key)) jsonLen++;
  108. }return jsonLen;
  109. }
  110. //syn console.log
  111. static log(text){
  112. console.log(JSON.stringify(text))
  113. }
  114. //dependency:myAddStyle,myDomAppendSon
  115. //my.addBtns(()=>{},a=e=>{confirm(e.target.id)},function(e){prompt(e.target.outerHTML)},function test(e){return 1})
  116. static addBtns(...funcs){
  117. funcs.forEach(e=>{
  118. if(typeof e !='function') return console.log(e);
  119. });
  120. let myDiv=document.querySelector('#myBtnContainerDiv'),btns=[];
  121. if(!myDiv) myDiv=this.domAppendBro('div',document.body,'',`id`,'myBtnContainerDiv');
  122. if(!document.querySelector('style.mycss'))this.addStyle(`#myBtnContainerDiv{display:flex!important;z-index:1650729359811!important;position: fixed!important;right:0!important;top:0!important}`,'mycss');
  123. for(let i=0;i<funcs.length;i++){
  124. const fname=funcs[i].name||"noname",
  125. btn = this.domAppendSon('button',myDiv,fname,'class','myQuickBtn undefined');
  126. btn.addEventListener('click',funcs[i]),btns[i]=btn;
  127. }return btns
  128. }
  129. //prevent document.title from being changed
  130. static fixTitle(){
  131. Object.defineProperty(document,"title",{
  132. writable:false
  133. })
  134. }
  135. }