util.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. function getOneFromArray(arr) {
  54. return arr[getRandomInt(0, arr.length)];
  55. }
  56. // 该函数用于创建一个<eleName k="attrs[k]">text</eleName>样式的页面元素
  57. function createEle(eleName, text, attrs) {
  58. let ele = document.createElement(eleName);
  59. // innerText 也就是 <p>text会被添加到这里</p>
  60. ele.innerText = text;
  61. // attrs 的类型是一个 map
  62. for (let k in attrs) {
  63. // 遍历 attrs, 给节点 ele 添加我们想要的属性
  64. ele.setAttribute(k, attrs[k]);
  65. }
  66. // 返回节点
  67. return ele;
  68. }
  69. function myCopy(inner_html) {
  70. let tmpId = "tmpId123123" + getRandomInt(1, 10000);
  71. let a = document.createElement('div');
  72. a.id = tmpId;
  73. a.innerHTML = inner_html
  74. document.querySelector('body').appendChild(a)
  75. let range = document.createRange();
  76. range.selectNode(document.querySelector("#" + tmpId));
  77. // 清除选择
  78. window.getSelection().removeAllRanges();
  79. window.getSelection().addRange(range);
  80. console.log('复制成功');
  81. document.execCommand('copy');
  82. // 清除选择
  83. window.getSelection().removeAllRanges();
  84. document.querySelector("#" + tmpId).remove();
  85. }
  86. //自动关闭提示框
  87. function myAlert(str, sec) {
  88. let bordercolor = "#336699";//提示窗口的边框颜色
  89. let sWidth, sHeight;
  90. //获取当前窗口尺寸
  91. sWidth = document.body.offsetWidth;
  92. sHeight = document.body.offsetHeight;
  93. //背景div
  94. const bgObj = document.createElement("div");
  95. bgObj.setAttribute('id', 'alertbgDiv');
  96. bgObj.style.position = "fixed";
  97. bgObj.style.top = "0";
  98. bgObj.style.background = "#E8E8E8";
  99. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  100. bgObj.style.opacity = "0.6";
  101. bgObj.style.left = "0";
  102. bgObj.style.width = sWidth + "px";
  103. bgObj.style.height = sHeight + "px";
  104. bgObj.style.zIndex = "10000";
  105. document.body.appendChild(bgObj);
  106. //创建提示窗口的div
  107. const msgObj = document.createElement("div");
  108. msgObj.setAttribute("id", "alertmsgDiv");
  109. msgObj.setAttribute("align", "center");
  110. msgObj.style.background = "white";
  111. msgObj.style.border = "1px solid " + bordercolor;
  112. msgObj.style.position = "fixed";
  113. msgObj.style.left = "50%";
  114. msgObj.style.font = "15px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  115. //窗口距离左侧和顶端的距离
  116. msgObj.style.marginLeft = "-225px";
  117. //窗口被卷去的高+(屏幕可用工作区高/2)-150
  118. msgObj.style.top = document.body.scrollTop + (window.screen.availHeight / 2) - 150 + "px";
  119. msgObj.style.textAlign = "center";
  120. msgObj.style.lineHeight = "25px";
  121. msgObj.style.zIndex = "10001";
  122. msgObj.style.minWidth = "300px";
  123. document.body.appendChild(msgObj);
  124. //提示信息标题
  125. const title = document.createElement("h4");
  126. title.setAttribute("id", "alertmsgTitle");
  127. title.setAttribute("align", "left");
  128. title.style.margin = "0";
  129. title.style.padding = "3px";
  130. title.style.background = bordercolor;
  131. title.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
  132. title.style.opacity = "0.75";
  133. title.style.border = "1px solid " + bordercolor;
  134. title.style.font = "12px Verdana, Geneva, Arial, Helvetica, sans-serif";
  135. title.style.color = "white";
  136. title.innerHTML = "提示信息";
  137. document.getElementById("alertmsgDiv").appendChild(title);
  138. //提示信息
  139. const txt = document.createElement("p");
  140. txt.setAttribute("id", "msgTxt");
  141. txt.style.margin = "16px 0";
  142. txt.innerHTML = str;
  143. document.getElementById("alertmsgDiv").appendChild(txt);
  144. //设置关闭时间
  145. window.setTimeout(() => {
  146. document.body.removeChild(document.getElementById("alertbgDiv"));
  147. document.getElementById("alertmsgDiv").removeChild(document.getElementById("alertmsgTitle"));
  148. document.body.removeChild(document.getElementById("alertmsgDiv"));
  149. }, sec * 1000);
  150. }
  151. function log(obj) {
  152. console.table(JSON.parse(JSON.stringify(obj)));
  153. }
  154. function addJSFile(js_url) {
  155. const eleScript = document.createElement("script");
  156. eleScript.type = "text/javascript";
  157. eleScript.src = js_url;
  158. document.getElementsByTagName("HEAD")[0].appendChild(eleScript);
  159. }
  160. function addJQ() {
  161. addJSFile("https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js");
  162. setInterval(() => {
  163. if (typeof (jQuery) == 'undefined') {
  164. addJSFile("");
  165. }
  166. }, 200)
  167. }
  168. /**
  169. * 复制
  170. * 需要添加:addJSFile("//cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.11/clipboard.min.js")
  171. * @param text
  172. * @param uri
  173. */
  174. function copyTextToClipboard(text,uri) {
  175. let copyFrom, agent, body;
  176. copyFrom = document.createElement("a");
  177. copyFrom.setAttribute("id","target");
  178. copyFrom.setAttribute("href",uri);
  179. copyFrom.innerHTML = text;
  180. agent = document.createElement("button");
  181. body = document.getElementsByTagName('body')[0];
  182. body.appendChild(copyFrom);
  183. body.appendChild(agent);
  184. // 麻烦:无法传入'.btn'元素 我们可以创建一个btn作为代理
  185. let clipboard = new ClipboardJS(agent, {
  186. target: function() {
  187. return document.querySelector('#target');
  188. }
  189. });
  190. clipboard.on('success', function(e) {
  191. console.log(e);
  192. });
  193. clipboard.on('error', function(e) {
  194. console.log(e);
  195. });
  196. agent.click();
  197. // copyFrom.focus();
  198. // copyFrom.select(); // 问题所在 无法对copyFrom对象使用select()方法
  199. // document.execCommand('copy'); // 采用Clipboard.js方案
  200. body.removeChild(copyFrom);
  201. body.removeChild(agent);
  202. }
  203. function addLink(domSelector, link_str, linkUrl) {
  204. const eleScript = document.createElement("a");
  205. eleScript.innerHTML = link_str.replaceAll(" ", "&nbsp;");
  206. eleScript.href = linkUrl;
  207. document.querySelector(domSelector).appendChild(eleScript);
  208. }