util.js 5.0 KB

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