myUtils

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

当前为 2022-04-27 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/435697/1044512/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,`type`,'text/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.append(son);
  19. if(content)son.append(document.createTextNode(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.parentNode.insertBefore(bro,dom.nextElementSibling);
  33. if(content)bro.append(document.createTextNode(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. //enable or disable style with className
  41. static switchStyle(...styleClassNames){
  42. styleClassNames.forEach(e=>{
  43. const style=document.querySelectorAll('style.'+e).forEach(e=>{
  44. e.type?e.type='':e.type="text/css";
  45. });
  46. })
  47. };
  48. //my.seconds2date(new Date().getTime())
  49. static seconds2date(seconds){
  50. const date=new Date(seconds),year=date.getFullYear(),month=date.getMonth()+1,day=date.getDate();
  51. const hour=date.getHours(),minute=date.getMinutes(),second=date.getSeconds(),milliseconds=date.getMilliseconds(),currentTime=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+":"+milliseconds;
  52. return currentTime
  53. };
  54. //跳出登录框后再执行
  55. static fuckTiebaLogin(userName,password){
  56. document.getElementById('TANGRAM__PSP_5__footerULoginBtn').click();
  57. document.getElementById('TANGRAM__PSP_5__userName').outerHTML+='';
  58. document.getElementById('TANGRAM__PSP_5__password').outerHTML+='';
  59. document.getElementById('TANGRAM__PSP_5__userName').value=userName;
  60. document.getElementById('TANGRAM__PSP_5__password').value=password;
  61. return document.getElementById('TANGRAM__PSP_5__submit').click();
  62. };
  63. static getValue(k,aDefault){
  64. const val = localStorage.getItem("XB_"+k)
  65. if (!val && 'undefined' != typeof aDefault) return aDefault;
  66. return val;
  67. };
  68. //setValue('k', 'v')
  69. static setValue(k, v){
  70. localStorage.setItem("XB_"+k, v);
  71. };
  72. //deleteValue('k')
  73. static deleteValue(k){
  74. if(k.indexOf("XB_")!=-1) localStorage.removeItem(k);
  75. };
  76. static listValues(){
  77. let list=[],j=0,k;
  78. for (let i=0; i<localStorage.length;i++) {
  79. k=localStorage.key(i);
  80. if(k.indexOf("XB_")!=-1) list[j++] = localStorage.key(i);
  81. }return list;
  82. };
  83. //dependency:myXbListValues
  84. static clearValues(){
  85. this.listValues().forEach(e=>{
  86. localStorage.removeItem(e)
  87. })
  88. };
  89. //get len of json obj
  90. static getJsonLen(jsonObj){
  91. let jsonLen=0, key;
  92. for (key in jsonObj) {
  93. if (jsonObj.hasOwnProperty(key)) jsonLen++;
  94. }return jsonLen;
  95. };
  96. //syn console.log
  97. static log(text){
  98. console.log(JSON.stringify(text))
  99. };
  100. //dependency:myAddStyle,myDomAppendSon
  101. //my.addBtns(()=>{},a=e=>{confirm(e.target.id)},function(e){prompt(e.target.outerHTML)},function test(e){return 1})
  102. static addBtns(...funcs){
  103. funcs.forEach(e=>{
  104. if(typeof e !='function') return console.log(e);
  105. });
  106. let myDiv=document.querySelector('#myBtnContainerDiv'),btns=[];
  107. if(!myDiv) myDiv=this.domAppendSon(document.body,'div','',`id`,'myBtnContainerDiv');
  108. this.addStyle(`#myBtnContainerDiv{z-index:1650729359811!important;position: fixed!important;right:0!important;top:0!important}`);
  109. for(let i=0;i<funcs.length;i++){
  110. let fname=funcs[i].name;
  111. if(!fname) fname="noname";
  112. const btn = this.domAppendSon(myDiv,'input','','value',fname,'type','button','class','myQuickBtn');
  113. btn.addEventListener('click',funcs[i]),btns[i]=btn;
  114. }return btns
  115. };
  116. //prevent document.title from being changed
  117. static fixTitle(){
  118. Object.defineProperty(document,"title",{
  119. writable:false
  120. });
  121. };
  122. }