util.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. function sleep(time) {
  2. return new Promise((resolve) => setTimeout(resolve, time));
  3. }
  4. async function getDom(sel) {
  5. for (let i = 0; i < 100; i++) {
  6. let dom = document.querySelector(sel);
  7. if (dom) {
  8. return dom;
  9. } else {
  10. await sleep(100);
  11. }
  12. }
  13. }
  14. async function getDomAll(sel) {
  15. for (let i = 0; i < 100; i++) {
  16. let dom = document.querySelectorAll(sel);
  17. if (dom.length > 0) {
  18. return dom;
  19. } else {
  20. await sleep(100);
  21. }
  22. }
  23. }
  24. function addNewStyle(newStyle) {
  25. let styleElement = document.getElementById('styles_js');
  26. if (!styleElement) {
  27. styleElement = document.createElement('style');
  28. styleElement.type = 'text/css';
  29. styleElement.id = 'styles_js';
  30. document.getElementsByTagName('head')[0].appendChild(styleElement);
  31. }
  32. styleElement.appendChild(document.createTextNode(newStyle));
  33. }
  34. //直接读取浏览器url
  35. function GetQueryString(name) {
  36. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  37. var r = location.href.substring(location.href.indexOf('?') + 1).match(reg); //获取url中"?"符后的字符串并正则匹配
  38. var context = "";
  39. if (r != null)
  40. context = r[2];
  41. reg = null;
  42. r = null;
  43. return context == null || context === "" || context === "undefined" ? "" : decodeURI(context);
  44. }
  45. function getRandomInt(min, max) {
  46. min = Math.ceil(min);
  47. max = Math.floor(max);
  48. return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
  49. }
  50. function myCopy(inner_html) {
  51. let tmpId = "tmpId123123" + getRandomInt(1, 10000);
  52. let a = document.createElement('div');
  53. a.id = tmpId;
  54. a.innerHTML = inner_html
  55. document.querySelector('body').appendChild(a)
  56. let range = document.createRange();
  57. range.selectNode(document.querySelector("#" + tmpId));
  58. // 清除选择
  59. window.getSelection().removeAllRanges();
  60. window.getSelection().addRange(range);
  61. console.log('复制成功');
  62. document.execCommand('copy');
  63. // 清除选择
  64. window.getSelection().removeAllRanges();
  65. document.querySelector("#" + tmpId).remove();
  66. }
  67. //自动关闭提示框
  68. function myAlert(str, sec) {
  69. let bordercolor = "#336699";//提示窗口的边框颜色
  70. let sWidth, sHeight;
  71. //获取当前窗口尺寸
  72. sWidth = document.body.offsetWidth;
  73. sHeight = document.body.offsetHeight;
  74. //背景div
  75. const bgObj = document.createElement("div");
  76. bgObj.setAttribute('id', 'alertbgDiv');
  77. bgObj.style.position = "absolute";
  78. bgObj.style.top = "0";
  79. bgObj.style.background = "#E8E8E8";
  80. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  81. bgObj.style.opacity = "0.6";
  82. bgObj.style.left = "0";
  83. bgObj.style.width = sWidth + "px";
  84. bgObj.style.height = sHeight + "px";
  85. bgObj.style.zIndex = "10000";
  86. document.body.appendChild(bgObj);
  87. //创建提示窗口的div
  88. const msgObj = document.createElement("div");
  89. msgObj.setAttribute("id", "alertmsgDiv");
  90. msgObj.setAttribute("align", "center");
  91. msgObj.style.background = "white";
  92. msgObj.style.border = "1px solid " + bordercolor;
  93. msgObj.style.position = "absolute";
  94. msgObj.style.left = "50%";
  95. msgObj.style.font = "15px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  96. //窗口距离左侧和顶端的距离
  97. msgObj.style.marginLeft = "-225px";
  98. //窗口被卷去的高+(屏幕可用工作区高/2)-150
  99. msgObj.style.top = document.body.scrollTop + (window.screen.availHeight / 2) - 150 + "px";
  100. msgObj.style.textAlign = "center";
  101. msgObj.style.lineHeight = "25px";
  102. msgObj.style.zIndex = "10001";
  103. msgObj.style.minWidth = "300px";
  104. document.body.appendChild(msgObj);
  105. //提示信息标题
  106. const title = document.createElement("h4");
  107. title.setAttribute("id", "alertmsgTitle");
  108. title.setAttribute("align", "left");
  109. title.style.margin = "0";
  110. title.style.padding = "3px";
  111. title.style.background = bordercolor;
  112. title.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
  113. title.style.opacity = "0.75";
  114. title.style.border = "1px solid " + bordercolor;
  115. title.style.font = "12px Verdana, Geneva, Arial, Helvetica, sans-serif";
  116. title.style.color = "white";
  117. title.innerHTML = "提示信息";
  118. document.getElementById("alertmsgDiv").appendChild(title);
  119. //提示信息
  120. const txt = document.createElement("p");
  121. txt.setAttribute("id", "msgTxt");
  122. txt.style.margin = "16px 0";
  123. txt.innerHTML = str;
  124. document.getElementById("alertmsgDiv").appendChild(txt);
  125. //设置关闭时间
  126. window.setTimeout(() => {
  127. document.body.removeChild(document.getElementById("alertbgDiv"));
  128. document.getElementById("alertmsgDiv").removeChild(document.getElementById("alertmsgTitle"));
  129. document.body.removeChild(document.getElementById("alertmsgDiv"));
  130. }, sec * 1000);
  131. }
  132. function log(obj) {
  133. console.table(JSON.parse(JSON.stringify(obj)));
  134. }