util.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. function getQueryStringByUrl(url, name) {
  35. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  36. let r = url.substring(url.indexOf('?') + 1).match(reg); //获取url中"?"符后的字符串并正则匹配
  37. let context = "";
  38. if (r != null)
  39. context = r[2];
  40. reg = null;
  41. r = null;
  42. return context == null || context === "" || context === "undefined" ? "" : decodeURI(context);
  43. }
  44. //直接读取浏览器url
  45. function getQueryString(name) {
  46. return getQueryStringByUrl(location.href, name);
  47. }
  48. function getRandomInt(min, max) {
  49. min = Math.ceil(min);
  50. max = Math.floor(max);
  51. return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
  52. }
  53. // 该函数用于创建一个<eleName k="attrs[k]">text</eleName>样式的页面元素
  54. function createEle(eleName, text, attrs) {
  55. let ele = document.createElement(eleName);
  56. // innerText 也就是 <p>text会被添加到这里</p>
  57. ele.innerText = text;
  58. // attrs 的类型是一个 map
  59. for (let k in attrs) {
  60. // 遍历 attrs, 给节点 ele 添加我们想要的属性
  61. ele.setAttribute(k, attrs[k]);
  62. }
  63. // 返回节点
  64. return ele;
  65. }
  66. function myCopy(inner_html) {
  67. let tmpId = "tmpId123123" + getRandomInt(1, 10000);
  68. let a = document.createElement('div');
  69. a.id = tmpId;
  70. a.innerHTML = inner_html
  71. document.querySelector('body').appendChild(a)
  72. let range = document.createRange();
  73. range.selectNode(document.querySelector("#" + tmpId));
  74. // 清除选择
  75. window.getSelection().removeAllRanges();
  76. window.getSelection().addRange(range);
  77. console.log('复制成功');
  78. document.execCommand('copy');
  79. // 清除选择
  80. window.getSelection().removeAllRanges();
  81. document.querySelector("#" + tmpId).remove();
  82. }
  83. //自动关闭提示框
  84. function myAlert(str, sec) {
  85. let bordercolor = "#336699";//提示窗口的边框颜色
  86. let sWidth, sHeight;
  87. //获取当前窗口尺寸
  88. sWidth = document.body.offsetWidth;
  89. sHeight = document.body.offsetHeight;
  90. //背景div
  91. const bgObj = document.createElement("div");
  92. bgObj.setAttribute('id', 'alertbgDiv');
  93. bgObj.style.position = "fixed";
  94. bgObj.style.top = "0";
  95. bgObj.style.background = "#E8E8E8";
  96. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  97. bgObj.style.opacity = "0.6";
  98. bgObj.style.left = "0";
  99. bgObj.style.width = sWidth + "px";
  100. bgObj.style.height = sHeight + "px";
  101. bgObj.style.zIndex = "10000";
  102. document.body.appendChild(bgObj);
  103. //创建提示窗口的div
  104. const msgObj = document.createElement("div");
  105. msgObj.setAttribute("id", "alertmsgDiv");
  106. msgObj.setAttribute("align", "center");
  107. msgObj.style.background = "white";
  108. msgObj.style.border = "1px solid " + bordercolor;
  109. msgObj.style.position = "fixed";
  110. msgObj.style.left = "50%";
  111. msgObj.style.font = "15px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  112. //窗口距离左侧和顶端的距离
  113. msgObj.style.marginLeft = "-225px";
  114. //窗口被卷去的高+(屏幕可用工作区高/2)-150
  115. msgObj.style.top = document.body.scrollTop + (window.screen.availHeight / 2) - 150 + "px";
  116. msgObj.style.textAlign = "center";
  117. msgObj.style.lineHeight = "25px";
  118. msgObj.style.zIndex = "10001";
  119. msgObj.style.minWidth = "300px";
  120. document.body.appendChild(msgObj);
  121. //提示信息标题
  122. const title = document.createElement("h4");
  123. title.setAttribute("id", "alertmsgTitle");
  124. title.setAttribute("align", "left");
  125. title.style.margin = "0";
  126. title.style.padding = "3px";
  127. title.style.background = bordercolor;
  128. title.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
  129. title.style.opacity = "0.75";
  130. title.style.border = "1px solid " + bordercolor;
  131. title.style.font = "12px Verdana, Geneva, Arial, Helvetica, sans-serif";
  132. title.style.color = "white";
  133. title.innerHTML = "提示信息";
  134. document.getElementById("alertmsgDiv").appendChild(title);
  135. //提示信息
  136. const txt = document.createElement("p");
  137. txt.setAttribute("id", "msgTxt");
  138. txt.style.margin = "16px 0";
  139. txt.innerHTML = str;
  140. document.getElementById("alertmsgDiv").appendChild(txt);
  141. //设置关闭时间
  142. window.setTimeout(() => {
  143. document.body.removeChild(document.getElementById("alertbgDiv"));
  144. document.getElementById("alertmsgDiv").removeChild(document.getElementById("alertmsgTitle"));
  145. document.body.removeChild(document.getElementById("alertmsgDiv"));
  146. }, sec * 1000);
  147. }
  148. function log(obj) {
  149. console.table(JSON.parse(JSON.stringify(obj)));
  150. }
  151. function addJSFile(js_url) {
  152. const eleScript = document.createElement("script");
  153. eleScript.type = "text/javascript";
  154. eleScript.src = js_url;
  155. document.getElementsByTagName("HEAD")[0].appendChild(eleScript);
  156. }
  157. function addJQ() {
  158. addJSFile("https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js");
  159. setInterval(() => {
  160. if (typeof (jQuery) == 'undefined') {
  161. addJSFile("");
  162. }
  163. }, 200)
  164. }
  165. function addLink(domSelector, link_str, linkUrl) {
  166. const eleScript = document.createElement("a");
  167. eleScript.innerHTML = link_str.replaceAll(" ", "&nbsp;");
  168. eleScript.href = linkUrl;
  169. document.querySelector(domSelector).appendChild(eleScript);
  170. }