util.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // 该函数用于创建一个<eleName k="attrs[k]">text</eleName>样式的页面元素
  51. function createEle(eleName, text, attrs) {
  52. let ele = document.createElement(eleName);
  53. // innerText 也就是 <p>text会被添加到这里</p>
  54. ele.innerText = text;
  55. // attrs 的类型是一个 map
  56. for (let k in attrs) {
  57. // 遍历 attrs, 给节点 ele 添加我们想要的属性
  58. ele.setAttribute(k, attrs[k]);
  59. }
  60. // 返回节点
  61. return ele;
  62. }
  63. function myCopy(inner_html) {
  64. let tmpId = "tmpId123123" + getRandomInt(1, 10000);
  65. let a = document.createElement('div');
  66. a.id = tmpId;
  67. a.innerHTML = inner_html
  68. document.querySelector('body').appendChild(a)
  69. let range = document.createRange();
  70. range.selectNode(document.querySelector("#" + tmpId));
  71. // 清除选择
  72. window.getSelection().removeAllRanges();
  73. window.getSelection().addRange(range);
  74. console.log('复制成功');
  75. document.execCommand('copy');
  76. // 清除选择
  77. window.getSelection().removeAllRanges();
  78. document.querySelector("#" + tmpId).remove();
  79. }
  80. //自动关闭提示框
  81. function myAlert(str, sec) {
  82. let bordercolor = "#336699";//提示窗口的边框颜色
  83. let sWidth, sHeight;
  84. //获取当前窗口尺寸
  85. sWidth = document.body.offsetWidth;
  86. sHeight = document.body.offsetHeight;
  87. //背景div
  88. const bgObj = document.createElement("div");
  89. bgObj.setAttribute('id', 'alertbgDiv');
  90. bgObj.style.position = "absolute";
  91. bgObj.style.top = "0";
  92. bgObj.style.background = "#E8E8E8";
  93. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  94. bgObj.style.opacity = "0.6";
  95. bgObj.style.left = "0";
  96. bgObj.style.width = sWidth + "px";
  97. bgObj.style.height = sHeight + "px";
  98. bgObj.style.zIndex = "10000";
  99. document.body.appendChild(bgObj);
  100. //创建提示窗口的div
  101. const msgObj = document.createElement("div");
  102. msgObj.setAttribute("id", "alertmsgDiv");
  103. msgObj.setAttribute("align", "center");
  104. msgObj.style.background = "white";
  105. msgObj.style.border = "1px solid " + bordercolor;
  106. msgObj.style.position = "absolute";
  107. msgObj.style.left = "50%";
  108. msgObj.style.font = "15px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  109. //窗口距离左侧和顶端的距离
  110. msgObj.style.marginLeft = "-225px";
  111. //窗口被卷去的高+(屏幕可用工作区高/2)-150
  112. msgObj.style.top = document.body.scrollTop + (window.screen.availHeight / 2) - 150 + "px";
  113. msgObj.style.textAlign = "center";
  114. msgObj.style.lineHeight = "25px";
  115. msgObj.style.zIndex = "10001";
  116. msgObj.style.minWidth = "300px";
  117. document.body.appendChild(msgObj);
  118. //提示信息标题
  119. const title = document.createElement("h4");
  120. title.setAttribute("id", "alertmsgTitle");
  121. title.setAttribute("align", "left");
  122. title.style.margin = "0";
  123. title.style.padding = "3px";
  124. title.style.background = bordercolor;
  125. title.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
  126. title.style.opacity = "0.75";
  127. title.style.border = "1px solid " + bordercolor;
  128. title.style.font = "12px Verdana, Geneva, Arial, Helvetica, sans-serif";
  129. title.style.color = "white";
  130. title.innerHTML = "提示信息";
  131. document.getElementById("alertmsgDiv").appendChild(title);
  132. //提示信息
  133. const txt = document.createElement("p");
  134. txt.setAttribute("id", "msgTxt");
  135. txt.style.margin = "16px 0";
  136. txt.innerHTML = str;
  137. document.getElementById("alertmsgDiv").appendChild(txt);
  138. //设置关闭时间
  139. window.setTimeout(() => {
  140. document.body.removeChild(document.getElementById("alertbgDiv"));
  141. document.getElementById("alertmsgDiv").removeChild(document.getElementById("alertmsgTitle"));
  142. document.body.removeChild(document.getElementById("alertmsgDiv"));
  143. }, sec * 1000);
  144. }
  145. function log(obj) {
  146. console.table(JSON.parse(JSON.stringify(obj)));
  147. }