util.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. //直接读取浏览器url
  35. function GetQueryString(name) {
  36. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  37. var r = location.href.substring(location.href.indexOf('?') + 1).match(reg); //获取url中"?"符后的字符串并正则匹配
  38. var context = "";
  39. if (r != null)
  40. context = r[2];
  41. reg = null;
  42. r = null;
  43. return context == null || context === "" || context === "undefined" ? "" : decodeURI(context);
  44. }
  45. function getRandomInt(min, max) {
  46. min = Math.ceil(min);
  47. max = Math.floor(max);
  48. return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
  49. }
  50. function myCopy(inner_html) {
  51. let tmpId = "tmpId123123" + getRandomInt(1, 10000);
  52. let a = document.createElement('div');
  53. a.id = tmpId;
  54. a.innerHTML = inner_html
  55. document.querySelector('body').appendChild(a)
  56. let range = document.createRange();
  57. range.selectNode(document.querySelector("#" + tmpId));
  58. // 清除选择
  59. window.getSelection().removeAllRanges();
  60. window.getSelection().addRange(range);
  61. console.log('复制成功');
  62. document.execCommand('copy');
  63. // 清除选择
  64. window.getSelection().removeAllRanges();
  65. document.querySelector("#" + tmpId).remove();
  66. }
  67. function log(obj) {
  68. console.table(JSON.parse(JSON.stringify(obj)));
  69. }