util.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. async function getDomAll(sel) {
  16. for (let i = 0; i < 100; i++) {
  17. let dom = document.querySelectorAll(sel);
  18. console.log(dom);
  19. if (dom) {
  20. return dom;
  21. } else {
  22. await sleep(100);
  23. }
  24. }
  25. }
  26. function addNewStyle(newStyle) {
  27. let styleElement = document.getElementById('styles_js');
  28. if (!styleElement) {
  29. styleElement = document.createElement('style');
  30. styleElement.type = 'text/css';
  31. styleElement.id = 'styles_js';
  32. document.getElementsByTagName('head')[0].appendChild(styleElement);
  33. }
  34. styleElement.appendChild(document.createTextNode(newStyle));
  35. }
  36. //直接读取浏览器url
  37. function GetQueryString(name) {
  38. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  39. var r = location.href.substring(location.href.indexOf('?') + 1).match(reg); //获取url中"?"符后的字符串并正则匹配
  40. var context = "";
  41. if (r != null)
  42. context = r[2];
  43. reg = null;
  44. r = null;
  45. return context == null || context === "" || context === "undefined" ? "" : decodeURI(context);
  46. }
  47. function getRandomInt(min, max) {
  48. min = Math.ceil(min);
  49. max = Math.floor(max);
  50. return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
  51. }
  52. function myCopy(inner_html) {
  53. let tmpId = "tmpId123123" + getRandomInt(1, 10000);
  54. let a = document.createElement('div');
  55. a.id = tmpId;
  56. a.innerHTML = inner_html
  57. document.querySelector('body').appendChild(a)
  58. let range = document.createRange();
  59. range.selectNode(document.querySelector("#" + tmpId));
  60. // 清除选择
  61. window.getSelection().removeAllRanges();
  62. window.getSelection().addRange(range);
  63. console.log('复制成功');
  64. document.execCommand('copy');
  65. // 清除选择
  66. window.getSelection().removeAllRanges();
  67. document.querySelector("#" + tmpId).remove();
  68. }
  69. function log(obj) {
  70. console.table(JSON.parse(JSON.stringify(obj)));
  71. }