util.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. }