util.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. function sleep(time) {
  2. return new Promise((resolve) => setTimeout(resolve, time));
  3. }
  4. Date.prototype.format = function (fmt) {
  5. var o = {
  6. "M+": this.getMonth() + 1, //月份
  7. "d+": this.getDate(), //日
  8. "h+": this.getHours(), //小时
  9. "m+": this.getMinutes(), //分
  10. "s+": this.getSeconds(), //秒
  11. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  12. "S": this.getMilliseconds() //毫秒
  13. };
  14. if (/(y+)/.test(fmt)) {
  15. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  16. }
  17. for (var k in o) {
  18. if (new RegExp("(" + k + ")").test(fmt)) {
  19. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  20. }
  21. }
  22. return fmt;
  23. }
  24. async function getDom(sel) {
  25. for (let i = 0; i < 100; i++) {
  26. let dom = document.querySelector(sel);
  27. if (dom) {
  28. return dom;
  29. } else {
  30. await sleep(100);
  31. }
  32. }
  33. }
  34. async function getDomAll(sel) {
  35. for (let i = 0; i < 100; i++) {
  36. let dom = document.querySelectorAll(sel);
  37. if (dom.length > 0) {
  38. return dom;
  39. } else {
  40. await sleep(100);
  41. }
  42. }
  43. }
  44. function addNewStyle(newStyle) {
  45. let styleElement = document.getElementById('styles_js');
  46. if (!styleElement) {
  47. styleElement = document.createElement('style');
  48. styleElement.type = 'text/css';
  49. styleElement.id = 'styles_js';
  50. document.getElementsByTagName('head')[0].appendChild(styleElement);
  51. }
  52. styleElement.appendChild(document.createTextNode(newStyle));
  53. }
  54. function getQueryStringByUrl(url, name) {
  55. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  56. let r = url.substring(url.indexOf('?') + 1).match(reg); //获取url中"?"符后的字符串并正则匹配
  57. let context = "";
  58. if (r != null)
  59. context = r[2];
  60. reg = null;
  61. r = null;
  62. return context == null || context === "" || context === "undefined" ? "" : decodeURI(context);
  63. }
  64. //直接读取浏览器url
  65. function getQueryString(name) {
  66. return getQueryStringByUrl(location.href, name);
  67. }
  68. function getRandomInt(min, max) {
  69. min = Math.ceil(min);
  70. max = Math.floor(max);
  71. return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
  72. }
  73. function getOneFromArray(arr) {
  74. return arr[getRandomInt(0, arr.length)];
  75. }
  76. // 该函数用于创建一个<eleName k="attrs[k]">text</eleName>样式的页面元素
  77. function createEle(eleName, text, attrs) {
  78. let ele = document.createElement(eleName);
  79. // innerText 也就是 <p>text会被添加到这里</p>
  80. ele.innerText = text;
  81. // attrs 的类型是一个 map
  82. for (let k in attrs) {
  83. // 遍历 attrs, 给节点 ele 添加我们想要的属性
  84. ele.setAttribute(k, attrs[k]);
  85. }
  86. // 返回节点
  87. return ele;
  88. }
  89. function myCopy(inner_html) {
  90. let tmpId = "tmpId123123" + getRandomInt(1, 10000);
  91. let a = document.createElement('div');
  92. a.id = tmpId;
  93. a.innerHTML = inner_html
  94. document.querySelector('body').appendChild(a)
  95. let range = document.createRange();
  96. range.selectNode(document.querySelector("#" + tmpId));
  97. // 清除选择
  98. window.getSelection().removeAllRanges();
  99. window.getSelection().addRange(range);
  100. console.log('复制成功');
  101. document.execCommand('copy');
  102. // 清除选择
  103. window.getSelection().removeAllRanges();
  104. document.querySelector("#" + tmpId).remove();
  105. }
  106. //自动关闭提示框
  107. function myAlert(str, sec) {
  108. let bordercolor = "#336699";//提示窗口的边框颜色
  109. let sWidth, sHeight;
  110. //获取当前窗口尺寸
  111. sWidth = document.body.offsetWidth;
  112. sHeight = document.body.offsetHeight;
  113. //背景div
  114. const bgObj = document.createElement("div");
  115. bgObj.setAttribute('id', 'alertbgDiv');
  116. bgObj.style.position = "fixed";
  117. bgObj.style.top = "0";
  118. bgObj.style.background = "#E8E8E8";
  119. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  120. bgObj.style.opacity = "0.6";
  121. bgObj.style.left = "0";
  122. bgObj.style.width = sWidth + "px";
  123. bgObj.style.height = sHeight + "px";
  124. bgObj.style.zIndex = "10000";
  125. document.body.appendChild(bgObj);
  126. //创建提示窗口的div
  127. const msgObj = document.createElement("div");
  128. msgObj.setAttribute("id", "alertmsgDiv");
  129. msgObj.setAttribute("align", "center");
  130. msgObj.style.background = "white";
  131. msgObj.style.border = "1px solid " + bordercolor;
  132. msgObj.style.position = "fixed";
  133. msgObj.style.left = "50%";
  134. msgObj.style.font = "15px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  135. //窗口距离左侧和顶端的距离
  136. msgObj.style.marginLeft = "-225px";
  137. //窗口被卷去的高+(屏幕可用工作区高/2)-150
  138. msgObj.style.top = document.body.scrollTop + (window.screen.availHeight / 2) - 150 + "px";
  139. msgObj.style.textAlign = "center";
  140. msgObj.style.lineHeight = "25px";
  141. msgObj.style.zIndex = "10001";
  142. msgObj.style.minWidth = "300px";
  143. document.body.appendChild(msgObj);
  144. //提示信息标题
  145. const title = document.createElement("h4");
  146. title.setAttribute("id", "alertmsgTitle");
  147. title.setAttribute("align", "left");
  148. title.style.margin = "0";
  149. title.style.padding = "3px";
  150. title.style.background = bordercolor;
  151. title.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
  152. title.style.opacity = "0.75";
  153. title.style.border = "1px solid " + bordercolor;
  154. title.style.font = "12px Verdana, Geneva, Arial, Helvetica, sans-serif";
  155. title.style.color = "white";
  156. title.innerHTML = "提示信息";
  157. document.getElementById("alertmsgDiv").appendChild(title);
  158. //提示信息
  159. const txt = document.createElement("p");
  160. txt.setAttribute("id", "msgTxt");
  161. txt.style.margin = "16px 0";
  162. txt.innerHTML = str;
  163. document.getElementById("alertmsgDiv").appendChild(txt);
  164. //设置关闭时间
  165. window.setTimeout(() => {
  166. document.body.removeChild(document.getElementById("alertbgDiv"));
  167. document.getElementById("alertmsgDiv").removeChild(document.getElementById("alertmsgTitle"));
  168. document.body.removeChild(document.getElementById("alertmsgDiv"));
  169. }, sec * 1000);
  170. }
  171. function log(obj) {
  172. console.table(JSON.parse(JSON.stringify(obj)));
  173. }
  174. function addJSFile(js_url) {
  175. const eleScript = document.createElement("script");
  176. eleScript.type = "text/javascript";
  177. eleScript.src = js_url;
  178. document.getElementsByTagName("HEAD")[0].appendChild(eleScript);
  179. }
  180. function addJQ() {
  181. addJSFile("https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js");
  182. setInterval(() => {
  183. if (typeof (jQuery) == 'undefined') {
  184. addJSFile("");
  185. }
  186. }, 200)
  187. }
  188. /**
  189. * 复制
  190. * 需要添加:addJSFile("//cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.11/clipboard.min.js")
  191. * @param text
  192. * @param uri
  193. */
  194. function copyTextToClipboard(text, uri) {
  195. let copyFrom, agent, body;
  196. copyFrom = document.createElement("a");
  197. copyFrom.setAttribute("id", "target");
  198. copyFrom.setAttribute("href", uri);
  199. copyFrom.innerHTML = text;
  200. agent = document.createElement("button");
  201. body = document.getElementsByTagName('body')[0];
  202. body.appendChild(copyFrom);
  203. body.appendChild(agent);
  204. // 麻烦:无法传入'.btn'元素 我们可以创建一个btn作为代理
  205. let clipboard = new ClipboardJS(agent, {
  206. target: function () {
  207. return document.querySelector('#target');
  208. }
  209. });
  210. clipboard.on('success', function (e) {
  211. console.log(e);
  212. });
  213. clipboard.on('error', function (e) {
  214. console.log(e);
  215. });
  216. agent.click();
  217. // copyFrom.focus();
  218. // copyFrom.select(); // 问题所在 无法对copyFrom对象使用select()方法
  219. // document.execCommand('copy'); // 采用Clipboard.js方案
  220. body.removeChild(copyFrom);
  221. body.removeChild(agent);
  222. }
  223. function addLink(domSelector, link_str, linkUrl) {
  224. const eleScript = document.createElement("a");
  225. eleScript.innerHTML = link_str.replaceAll(" ", "&nbsp;");
  226. eleScript.href = linkUrl;
  227. document.querySelector(domSelector).appendChild(eleScript);
  228. }