mylib

我的工具

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

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

  1. function myAddStyle(css,className='') {
  2. const style = document.createElement('style');
  3. style.type='text/css';
  4. style.textContent = css;
  5. style.className=arguments[1];
  6. const h0 =document.querySelector('html');
  7. h0.insertBefore(style,h0.children[0]);
  8. }
  9. //enable or disable style with className
  10. function mySwitchStyle(...styleClassNames) {
  11. styleClassNames.forEach(e=>{
  12. const style=document.querySelector('style.'+e);
  13. style.type==0?style.type="text/css":style.type=0
  14. })
  15. }
  16. //seconds2date(new Date().getTime())
  17. function seconds2date(seconds){
  18. const date = new Date(seconds)
  19. const year = date.getFullYear();
  20. const month = date.getMonth() + 1;
  21. const day = date.getDate();
  22. const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  23. const minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  24. const second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  25. const currentTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  26. return currentTime
  27. }
  28. //myDocumentBodyAppendDom(document.body,'tag','content','idk','true','data-s')
  29. function myDomAppendSon(dom,tag,content) {
  30. const son = document.createElement(tag)
  31. son.appendChild(document.createTextNode(content));
  32. const len = arguments.length;
  33. if (len>3) {
  34. for(let i=3;i<len;i+=2){
  35. son.setAttribute([arguments[i]],arguments[i+1])
  36. }
  37. }
  38. dom.appendChild(son);
  39. return son;
  40. }
  41. //myDomAppendBro(document.body,'div','','suck','1','dick')
  42. function myDomAppendBro(dom,tag,content){
  43. const bro = document.createElement(tag);
  44. bro.appendChild(document.createTextNode(content));
  45. const len = arguments.length;
  46. if (len>3) {
  47. for(let i=3;i<len;i+=2){
  48. bro.setAttribute([arguments[i]],arguments[i+1])
  49. }
  50. }
  51. dom.parentNode.insertBefore(bro,dom.nextElementSibling);
  52. return bro;
  53. }
  54. //跳出登录框后再执行
  55. function myFuckTiebaLogin(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. function myXbGetValue(k, aDefault) {
  64. let val = localStorage.getItem("XB_"+k)
  65. if (null === val && 'undefined' != typeof aDefault) return aDefault;
  66. return val;
  67. }
  68. //myXbSetValue('k', 'v')
  69. function myXbSetValue(k, v) {
  70. localStorage.setItem("XB_"+k, v);
  71. }
  72. //myXbSetValue('k')
  73. function myXbDeleteValue(k) {
  74. if(k.indexOf("XB_")!=-1) localStorage.removeItem(k);
  75. }
  76. function myXbListValues() {
  77. let list=[],j=0;
  78. for (let i = 0; i < localStorage.length; i++) {
  79. let k=localStorage.key(i);
  80. if(k.indexOf("XB_")!=-1) list[j++] = localStorage.key(i);
  81. }return list;
  82. }
  83. //dependency:myXbListValues
  84. function myXbClearValues() {
  85. return myXbListValues().forEach(e=>{
  86. localStorage.removeItem(e)
  87. })
  88. }
  89. //get len of json obj
  90. function myGetJsonLen(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. function myLog(text){
  98. console.log(JSON.stringify(text))
  99. }
  100. //dependency:myAddStyle,myDomAppendSon
  101. //myAddBtns(()=>{},a=e=>{confirm(e.target.id)},function(e){prompt(e.target.outerHTML)},function test(e){return 1})
  102. function myAddBtns(...funcs){
  103. funcs.forEach(e=>{
  104. if(typeof e !='function') return console.log(e);
  105. });
  106. let myDiv=document.querySelector('#myBtnContainerDiv');
  107. if(!myDiv) myDiv=myDomAppendSon(document.body,'div','','id','myBtnContainerDiv');
  108. myAddStyle(`#myBtnContainerDiv{z-index:1650729359811!important;position: fixed!important;right:0!important;top:0!important}`);
  109. const btns=[];
  110. for(let i=0;i<funcs.length;i++){
  111. let fname=funcs[i].name;
  112. if(!fname) fname="noname";
  113. const btn = myDomAppendSon(myDiv,'input','','value',fname,'type','button','class','myQuickBtn');
  114. btn.addEventListener('click',funcs[i]),btns[i]=btn;
  115. }return btns
  116. }
  117. //prevent document.title from being changed
  118. function myFixTitle(){
  119. Object.defineProperty(document,"title",{
  120. writable:false
  121. });
  122. }