global_module

@XiaoYingYo global_module

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/464780/1352660/global_module.js

  1. class global_module {
  2. static UrlCache = {};
  3.  
  4. /**
  5. *
  6. * @param {*} selector 目标元素选择器
  7. * @param {*} iframe 可选-iframe选择器 可以是字符串选择器或者元素
  8. * @param {*} callback 可选-回调函数
  9. * @param {*} time 可选-每次查询间隔时间
  10. * @param {*} timeout 可选-超时时间
  11. * @param {*} baseElement 可选-基础元素 可以是字符串选择器或者元素
  12. * @returns
  13. */
  14. static async waitForElement(selectors, iframe = null, callback = null, time = 100, timeout = 1000 * 30, baseElement = null, index = null, isWrapped = true, startTime = null) {
  15. if (time == null) {
  16. time = 100;
  17. }
  18. if (timeout == null) {
  19. timeout = 1000 * 30;
  20. }
  21. if (isWrapped == null) {
  22. isWrapped = true;
  23. }
  24. return new Promise(async (resolve) => {
  25. if (!startTime) {
  26. startTime = Date.now();
  27. }
  28. let iframeElement,
  29. base,
  30. elements = [];
  31. if (typeof selectors === 'string') {
  32. selectors = [selectors];
  33. }
  34. if (iframe) {
  35. try {
  36. iframeElement = iframe.getAttribute('id');
  37. iframeElement = iframe;
  38. } catch (e) {
  39. if (typeof iframe === 'string') {
  40. iframeElement = await global_module.waitForElement(iframe);
  41. } else {
  42. iframeElement = iframe;
  43. }
  44. }
  45. if (!iframeElement) {
  46. resolve(null);
  47. return;
  48. }
  49. for (let selector of selectors) {
  50. let foundElements = isWrapped ? $(iframeElement).contents().find(selector) : iframeElement.contentDocument.querySelectorAll(selector);
  51. if (foundElements.length > 0) {
  52. elements = foundElements;
  53. break;
  54. }
  55. }
  56. } else if (baseElement) {
  57. try {
  58. base = baseElement.getAttribute('id');
  59. base = baseElement;
  60. } catch (e) {
  61. if (typeof baseElement === 'string') {
  62. base = await global_module.waitForElement(baseElement);
  63. } else {
  64. base = baseElement;
  65. }
  66. }
  67. if (!base) {
  68. resolve(null);
  69. return;
  70. }
  71. for (let selector of selectors) {
  72. let foundElements = isWrapped ? base.find(selector) : base[0].querySelectorAll(selector);
  73. if (foundElements.length > 0) {
  74. elements = foundElements;
  75. break;
  76. }
  77. }
  78. } else {
  79. for (let selector of selectors) {
  80. let foundElements = null;
  81. try {
  82. foundElements = document.querySelectorAll(selector);
  83. } catch (e) {
  84. foundElements = $(selector);
  85. if (foundElements.length != 0) {
  86. foundElements = [foundElements.eq(0)[0]];
  87. }
  88. }
  89. if (foundElements.length > 0) {
  90. elements = foundElements;
  91. break;
  92. }
  93. }
  94. }
  95.  
  96. if (index != null) {
  97. elements = elements[index];
  98. if (!elements) {
  99. resolve(null);
  100. return;
  101. }
  102. }
  103.  
  104. if (elements.length > 0) {
  105. if (isWrapped) {
  106. elements = $(elements);
  107. }
  108. if (callback) {
  109. callback(elements);
  110. }
  111. resolve(elements);
  112. } else {
  113. if (timeout !== -1 && Date.now() - startTime >= timeout) {
  114. resolve(null);
  115. return;
  116. }
  117. setTimeout(async () => {
  118. resolve(await global_module.waitForElement(selectors, iframe, callback, time, timeout, baseElement, index, isWrapped, startTime));
  119. }, time);
  120. }
  121. });
  122. }
  123.  
  124. static copyText(text) {
  125. let textarea = document.createElement('textarea');
  126. textarea.value = text;
  127. document.body.appendChild(textarea);
  128. textarea.select();
  129. document.execCommand('copy');
  130. textarea.remove();
  131. }
  132.  
  133. static stripHtmlTags(html) {
  134. return html.replace(/<[^>]+>/g, '');
  135. }
  136.  
  137. static async Http(o) {
  138. return new Promise((resolve) => {
  139. if (typeof o === 'undefined' || typeof o !== 'object' || typeof o.url === 'undefined') {
  140. resolve(null);
  141. }
  142. if (typeof o.method === 'undefined') {
  143. o.method = 'GET';
  144. }
  145. if (typeof o.headers === 'undefined') {
  146. o.headers = {};
  147. }
  148. if (typeof o.data === 'undefined') {
  149. o.data = '';
  150. }
  151. if (typeof o.timeout === 'undefined') {
  152. o.timeout = 10000;
  153. }
  154. let res = { code: 0, ret: null };
  155. if (!GM_xmlhttpRequest) {
  156. let xhr = new XMLHttpRequest();
  157. xhr.open(o.method, o.url, true);
  158. for (let i in o.headers) {
  159. xhr.setRequestHeader(i, o.headers[i]);
  160. }
  161. xhr.timeout = o.timeout;
  162. xhr.onload = function () {
  163. res.code = xhr.status;
  164. res.ret = xhr.response;
  165. resolve(res);
  166. };
  167. xhr.onerror = function (err) {
  168. res.code = -1;
  169. res.ret = err;
  170. resolve(res);
  171. };
  172. xhr.send(o.data);
  173. } else {
  174. GM_xmlhttpRequest({
  175. method: o.method,
  176. url: o.url,
  177. headers: o.headers,
  178. data: o.data,
  179. timeout: o.timeout,
  180. onload: function (response) {
  181. res.code = response.status;
  182. res.ret = response;
  183. resolve(res);
  184. },
  185. onerror: function (err) {
  186. res.code = -1;
  187. res.ret = err;
  188. resolve(res);
  189. }
  190. });
  191. }
  192. });
  193. }
  194.  
  195. static async Ajax_Xhr(url, Way, data) {
  196. return new Promise(function (resolve) {
  197. let h = ['html', 'txt', 'json', 'xml', 'js', 'css'];
  198. if (global_module.UrlCache[url] == null) {
  199. if (h.indexOf(Way) == -1) {
  200. Way = 'GET';
  201. }
  202. let xhr = new XMLHttpRequest();
  203. xhr.open(Way, url, true);
  204. xhr.onreadystatechange = function () {
  205. if (xhr.readyState == 4 && xhr.status == 200) {
  206. let ret = xhr.responseText;
  207. let p = url;
  208. if (p.indexOf('?') != -1) p = p.substring(0, p.indexOf('?'));
  209. p = p.substring(p.lastIndexOf('.') + 1);
  210. if (h.indexOf(p) != -1) {
  211. global_module.UrlCache[url] = ret;
  212. }
  213. resolve(ret);
  214. return ret;
  215. }
  216. };
  217. xhr.send(data);
  218. } else {
  219. resolve(global_module.UrlCache[url]);
  220. return global_module.UrlCache[url];
  221. }
  222. })
  223. .then(function (result) {
  224. return result;
  225. })
  226. .catch(function (error) {
  227. console.log(error);
  228. });
  229. }
  230.  
  231. static decodeHtmlEntities(htmlString = '') {
  232. var tempElement = document.createElement('div');
  233. tempElement.innerHTML = htmlString;
  234. return tempElement.textContent;
  235. }
  236.  
  237. static GetUrlParm(href = null, name) {
  238. if (href == null) {
  239. href = location.href;
  240. }
  241. let parms = href.substring(href.indexOf('?') + 1);
  242. let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
  243. let r = parms.match(reg);
  244. if (r != null) {
  245. return decodeURIComponent(r[2]);
  246. }
  247. return null;
  248. }
  249.  
  250. static downloadSvgAsPng(svg, filename) {
  251. var canvas = document.createElement('canvas');
  252. var context = canvas.getContext('2d');
  253. var svgRect = svg.getBoundingClientRect();
  254. var width = svgRect.width;
  255. var height = svgRect.height;
  256. canvas.width = width;
  257. canvas.height = height;
  258. var image = new Image();
  259. var svgData = new XMLSerializer().serializeToString(svg);
  260. var svgUrl = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgData);
  261. image.onload = function () {
  262. context.drawImage(image, 0, 0, width, height);
  263. var link = document.createElement('a');
  264. link.href = canvas.toDataURL('image/png');
  265. link.download = filename;
  266. link.click();
  267. canvas = null;
  268. };
  269. image.src = svgUrl;
  270. }
  271.  
  272. static SetUrlParm(href, name, value) {
  273. if (href == null || href == '') {
  274. href = location.href;
  275. }
  276. let index = href.indexOf('?');
  277. if (index == -1) {
  278. href += '?' + name + '=' + value;
  279. } else {
  280. let parms = href.substring(href.indexOf('?') + 1);
  281. let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
  282. let r = parms.match(reg);
  283. if (r != null) {
  284. href = href.replace(r[0], r[0].replace(r[2], value));
  285. } else {
  286. href += '&' + name + '=' + value;
  287. }
  288. }
  289. return href;
  290. }
  291.  
  292. // 根据扩展名引入js或css
  293. static async LoadJsOrCss(url, scriptStr = '') {
  294. return new Promise(function (resolve) {
  295. if (Array.isArray(url)) {
  296. let count = 0;
  297. for (let i = 0; i < url.length; i++) {
  298. global_module.LoadJsOrCss(url[i]).then(() => {
  299. count++;
  300. if (count == url.length) {
  301. resolve();
  302. }
  303. });
  304. }
  305. return;
  306. }
  307. let p = url;
  308. let s = 0;
  309. if (p.indexOf('?') != -1) p = p.substring(0, p.indexOf('?'));
  310. p = p.substring(p.lastIndexOf('.') + 1);
  311. let t;
  312. if (p == 'js') {
  313. let script = document.createElement('script');
  314. script.type = 'text/javascript';
  315. script.id = url;
  316. if (scriptStr == '') {
  317. script.src = url;
  318. } else {
  319. script.innerHTML = scriptStr;
  320. s = 1;
  321. }
  322. t = script;
  323. } else if (p == 'css') {
  324. if (scriptStr == '') {
  325. let link = document.createElement('link');
  326. link.href = url;
  327. link.type = 'text/css';
  328. link.rel = 'stylesheet';
  329. link.id = url;
  330. t = link;
  331. } else {
  332. let style = document.createElement('style');
  333. style.type = 'text/css';
  334. style.id = url;
  335. style.innerHTML = scriptStr;
  336. t = style;
  337. s = 1;
  338. }
  339. }
  340. if (t != null) {
  341. t.onload = function () {
  342. resolve(t);
  343. };
  344. } else {
  345. resolve(null);
  346. }
  347. try {
  348. document.getElementsByTagName('head')[0].appendChild(t);
  349. } catch (e) {
  350. document.appendChild(t);
  351. }
  352. if (s != 0) {
  353. resolve(null);
  354. }
  355. })
  356. .then(function (r) {
  357. return r;
  358. })
  359. .catch(function (e) {
  360. console.log(e);
  361. });
  362. }
  363.  
  364. static RemoveJsOrCss(url) {
  365. $("[id='" + url + "']").remove();
  366. }
  367.  
  368. static IframeInjectScript(element, type = 'js', Str) {
  369. let Dom = $(element).contents().find('body');
  370. if (type == 'js') {
  371. $(element)[0].contentWindow.window.eval(Str);
  372. } else if (type == 'css') {
  373. Dom.append('<style>' + Str + '</style>');
  374. }
  375. }
  376.  
  377. static async waitPageLoad() {
  378. return new Promise(function (resolve) {
  379. if (document.readyState == 'complete') {
  380. resolve();
  381. } else {
  382. let S = setInterval(function () {
  383. if (document.readyState == 'complete') {
  384. clearInterval(S);
  385. resolve();
  386. }
  387. }, 200);
  388. }
  389. });
  390. }
  391.  
  392. static HOOKFunction(fun, before, after) {
  393. if (typeof fun == 'string') {
  394. return 'HOOK的对象必须是函数';
  395. }
  396. let old = fun;
  397. fun = function () {
  398. if (typeof before == 'function') {
  399. before();
  400. }
  401. let result = old.apply(this, arguments);
  402. if (typeof after == 'function') {
  403. after();
  404. }
  405. return result;
  406. };
  407. return fun;
  408. }
  409.  
  410. static getParentFolderPath(filePath) {
  411. var pathSeparator;
  412. if (filePath.indexOf('/') !== -1) {
  413. pathSeparator = '/';
  414. } else if (filePath.indexOf('\\') !== -1) {
  415. pathSeparator = '\\';
  416. } else if (filePath.indexOf('\\\\') !== -1) {
  417. pathSeparator = '\\\\';
  418. }
  419. var parts = filePath.split(pathSeparator);
  420. parts.pop();
  421. return parts.join(pathSeparator);
  422. }
  423.  
  424. static RandomIP() {
  425. let ip = '';
  426. for (let i = 0; i < 4; i++) {
  427. ip += Math.floor(Math.random() * 255) + '.';
  428. }
  429. ip = ip.substring(0, ip.length - 1);
  430. return ip;
  431. }
  432.  
  433. static RandomIPHeader() {
  434. let ip = this.RandomIP();
  435. let header = {
  436. 'X-Forwarded-For': ip,
  437. 'X-Originating-IP': ip,
  438. 'X-Remote-Addr': ip,
  439. 'X-Remote-IP': ip
  440. };
  441. return header;
  442. }
  443.  
  444. static MoveElement(obj, GoToX, speed = 10, scope = null) {
  445. return new Promise(async (resolve) => {
  446. if (obj == null) {
  447. resolve(false);
  448. return;
  449. }
  450. if (obj.length > 0) obj = obj[0];
  451. let event = document.createEvent('MouseEvents');
  452. if (scope == null) {
  453. scope = window;
  454. }
  455. event.initMouseEvent('mousedown', true, true, scope, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  456. obj.dispatchEvent(event);
  457. let x = 0;
  458. let timer = setInterval(function () {
  459. x += 5;
  460. let event = document.createEvent('MouseEvents');
  461. event.initMouseEvent('mousemove', true, true, scope, 0, 0, 0, x, 0, false, false, false, false, 0, null);
  462. obj.dispatchEvent(event);
  463. if (x >= GoToX) {
  464. clearInterval(timer);
  465. let event = document.createEvent('MouseEvents');
  466. event.initMouseEvent('mouseup', true, true, scope, 0, 0, 0, 260, 0, false, false, false, false, 0, null);
  467. obj.dispatchEvent(event);
  468. resolve(true);
  469. }
  470. }, speed);
  471. });
  472. }
  473.  
  474. static clickElement(element) {
  475. if (element == null) return false;
  476. let Event = document.createEvent('MouseEvents');
  477. Event.initEvent('click', true, true);
  478. element.dispatchEvent(Event);
  479. return true;
  480. }
  481.  
  482. static simulateMouseEvent(mouseEvent, win, element, eventType) {
  483. if (!mouseEvent) {
  484. mouseEvent = MouseEvent;
  485. }
  486. if (!win) {
  487. win = window;
  488. }
  489. let eventName = '';
  490. switch (eventType) {
  491. case 0:
  492. eventName = ['mouseover', 'mouseenter'];
  493. break;
  494. case 1:
  495. eventName = ['mouseout', 'mouseleave'];
  496. break;
  497. default:
  498. throw new Error('不支持的触发类型');
  499. }
  500. for (let i = 0; i < eventName.length; i++) {
  501. element.dispatchEvent(
  502. new MouseEvent(eventName[i], {
  503. bubbles: true,
  504. cancelable: true,
  505. view: win
  506. })
  507. );
  508. }
  509. }
  510.  
  511. static getLayerelement(layeri) {
  512. return $("div[id^='layui-layer" + layeri + "'][type][times='" + layeri + "']").eq(0);
  513. }
  514.  
  515. static getLayerelementIframe(layeri) {
  516. return global_module.getLayerelement(layeri).find('iframe').eq(0);
  517. }
  518.  
  519. static getLayerelementIframeBody(layeri) {
  520. return global_module.getLayerelementIframe(layeri).contents().find('body');
  521. }
  522.  
  523. static getLayerBtn(layeri, index) {
  524. return global_module.getLayerelement(layeri).find("a[class^='layui-layer-btn']").eq(index);
  525. }
  526.  
  527. static setLayerImgPath(layeri, Path) {
  528. if (Path.substr(-1) != '/' && Path.substr(-1) != '\\') {
  529. if (Path.indexOf('/') != -1) {
  530. Path += '/';
  531. } else {
  532. Path += '\\';
  533. }
  534. }
  535. let element = global_module.getLayerelement(layeri);
  536. let i = $(element).find('i');
  537. for (let m = 0; m < i.length; m++) {
  538. let className = $(i[m]).attr('class');
  539. if (className.indexOf('ico') == -1) {
  540. continue;
  541. }
  542. let bg = $(i[m]).css('background-image');
  543. let bgPath = bg.substring(bg.indexOf('(') + 1, bg.indexOf(')'));
  544. let fileName = bgPath.substring(bgPath.lastIndexOf('/') + 1);
  545. fileName = fileName.replace(/\"/g, '').replace(/\'/g, '');
  546. $(i[m]).css('background-image', 'url(' + Path + fileName + ')');
  547. }
  548. }
  549.  
  550. static async simulateKeydown(ele, key, interval = 200) {
  551. return new Promise(function (resolve) {
  552. if (key.length == 0) {
  553. resolve();
  554. return;
  555. }
  556. if (ele == null) {
  557. resolve();
  558. return;
  559. }
  560. key = key.toLowerCase();
  561. let keys = key.split('+');
  562. let event;
  563. if (typeof Event === 'function') {
  564. event = new Event('keydown');
  565. } else {
  566. event = document.createEvent('Event');
  567. event.initEvent('keydown', true, true);
  568. }
  569. event.key = keys[keys.length - 1];
  570. for (let i = 0; i < keys.length - 1; i++) {
  571. event[keys[i] + 'Key'] = true;
  572. }
  573. ele.dispatchEvent(event);
  574. setTimeout(() => {
  575. let event;
  576. if (typeof Event === 'function') {
  577. event = new Event('keyup');
  578. } else {
  579. event = document.createEvent('Event');
  580. event.initEvent('keyup', true, true);
  581. }
  582. event.key = keys[keys.length - 1];
  583. for (let i = 0; i < keys.length - 1; i++) {
  584. event[keys[i] + 'Key'] = false;
  585. }
  586. ele.dispatchEvent(event);
  587. resolve();
  588. }, interval);
  589. });
  590. }
  591.  
  592. static debounce(fn, delay) {
  593. let timeoutId;
  594. let promiseResolve;
  595. let promise = new Promise((resolve) => (promiseResolve = resolve));
  596. return function () {
  597. if (timeoutId) {
  598. clearTimeout(timeoutId);
  599. }
  600. timeoutId = setTimeout(async () => {
  601. timeoutId = null;
  602. if (fn.constructor.name === 'AsyncFunction') {
  603. let result = await fn.apply(this, arguments);
  604. promiseResolve(result);
  605. } else {
  606. let result = fn.apply(this, arguments);
  607. promiseResolve(result);
  608. }
  609. }, delay);
  610.  
  611. return promise;
  612. };
  613. }
  614.  
  615. static observeDomChanges(ele = null, callback, MutationObserverFunction = null) {
  616. if (MutationObserverFunction == null || typeof MutationObserverFunction != 'function') {
  617. MutationObserverFunction = MutationObserver;
  618. }
  619. const observer = new MutationObserverFunction(function (mutations) {
  620. mutations.forEach(function (mutation) {
  621. if (mutation.type === 'childList' && mutation.target.getAttribute('data-inserted-append') == null) {
  622. callback(mutation.target);
  623. }
  624. });
  625. });
  626. const options = {
  627. childList: true,
  628. subtree: true,
  629. characterData: true,
  630. attributeFilter: ['data-inserted-append']
  631. };
  632. if (ele == null) {
  633. ele = document;
  634. }
  635. observer.observe(ele, options);
  636. }
  637.  
  638. static getRandomString(len) {
  639. len = len || 32;
  640. let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  641. let maxPos = $chars.length;
  642. let pwd = '';
  643. for (let i = 0; i < len; i++) {
  644. pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  645. }
  646. return pwd;
  647. }
  648.  
  649. static getRandomNumber(len) {
  650. let $chars = '0123456789';
  651. let maxPos = $chars.length;
  652. let pwd = '';
  653. for (let i = 0; i < len; i++) {
  654. pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  655. }
  656. return pwd;
  657. }
  658.  
  659. static hideAndCreateDom(Element) {
  660. let dom = $(Element).eq(0);
  661. $(dom).hide();
  662. let clone = $(dom).clone();
  663. $(dom).after(clone);
  664. clone.show();
  665. return clone;
  666. }
  667.  
  668. static cloneAndHide(target, InsertMethod = 0, BaseElement = null) {
  669. if (InsertMethod == null) {
  670. InsertMethod = 0;
  671. }
  672. let clone = document.createElement(target.tagName);
  673. let attributes = target.attributes;
  674. for (let i = 0; i < attributes.length; i++) {
  675. clone.setAttribute(attributes[i].name, attributes[i].value);
  676. }
  677. let childNodes = target.childNodes;
  678. for (let i = 0; i < childNodes.length; i++) {
  679. clone.appendChild(childNodes[i].cloneNode(true));
  680. }
  681. target.style.display = 'none';
  682. let parent;
  683. if (BaseElement !== null) {
  684. parent = BaseElement;
  685. } else {
  686. parent = target.parentNode;
  687. }
  688. switch (InsertMethod) {
  689. case 0:
  690. parent.appendChild(clone);
  691. break;
  692. case 1:
  693. parent.insertBefore(clone, target.nextSibling);
  694. break;
  695. case 2:
  696. parent.insertBefore(clone, target);
  697. break;
  698. default:
  699. parent.appendChild(clone);
  700. }
  701. return clone;
  702. }
  703.  
  704. static async AnimateText(ele, text, Delay = 100) {
  705. return new Promise(function (resolve) {
  706. let currentText = '';
  707. let i = 0;
  708. let height = ele.parent().css('height');
  709. if (height == null) {
  710. resolve();
  711. return;
  712. }
  713. height = height.substring(0, height.length - 2) - 22 + 'px';
  714. let cursor;
  715. cursor = document.createElement('span');
  716. cursor.classList.add('cursor');
  717. cursor.style.backgroundColor = '#fff';
  718. cursor.style.width = '3px';
  719. cursor.style.height = height;
  720. cursor.style.display = 'inline-block';
  721. cursor.style.animation = 'blink 1s step-end infinite';
  722. let interval = setInterval(() => {
  723. currentText += text[i];
  724. ele.text(currentText);
  725. ele.append(cursor);
  726. i++;
  727. if (i === text.length) {
  728. clearInterval(interval);
  729. resolve();
  730. setTimeout(() => {
  731. cursor.remove();
  732. }, 200);
  733. }
  734. }, Delay);
  735. });
  736. }
  737.  
  738. static getMiddleString(str, start, end) {
  739. return str.substring(str.indexOf(start) + 1, str.indexOf(end));
  740. }
  741.  
  742. static Cookie = {
  743. clearAllCookies: function (domain) {
  744. var cookies = document.cookie.split('; ');
  745. for (var i = 0; i < cookies.length; i++) {
  746. var cookie = cookies[i];
  747. var eqPos = cookie.indexOf('=');
  748. var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
  749. var cookieDomain = location.hostname.replace(/^www\./i, '');
  750. if (cookieDomain === domain || cookieDomain.endsWith('.' + domain)) {
  751. document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=' + cookieDomain + ';path=/';
  752. }
  753. }
  754. },
  755. get: function (name) {
  756. let match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
  757. return match && decodeURIComponent(match[2]);
  758. },
  759. set: function (name, value, days) {
  760. if (days == null) {
  761. days = 1;
  762. }
  763. let d = new Date();
  764. d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * days);
  765. document.cookie = name + '=' + value + ';path=/;expires=' + d.toGMTString();
  766. },
  767. delete: function (name) {
  768. this.set(name, '', -1);
  769. },
  770. clear: function () {
  771. for (let key in this.get()) {
  772. this.delete(key);
  773. }
  774. let domain = location.hostname.replace(/^www\./i, '');
  775. this.clearAllCookies(domain);
  776. }
  777. };
  778.  
  779. static objectDepthEnumerate(_object, callback) {
  780. if (Array.isArray(_object)) {
  781. _object.forEach((item) => {
  782. if (callback && callback(null, item)) {
  783. return true;
  784. }
  785. if (this.objectDepthEnumerate(item, callback)) {
  786. return true;
  787. }
  788. });
  789. } else if (typeof _object === 'object' && _object !== null) {
  790. Object.keys(_object).forEach((key) => {
  791. if (callback && callback(key, _object[key])) {
  792. return true;
  793. }
  794. if (this.objectDepthEnumerate(_object[key], callback)) {
  795. return true;
  796. }
  797. });
  798. }
  799. }
  800.  
  801. static LocalStorage = {
  802. exportLocalStorage: function () {
  803. let localStorageData = {};
  804. for (let i = 0; i < localStorage.length; i++) {
  805. let key = localStorage.key(i);
  806. let value = localStorage.getItem(key);
  807. localStorageData[key] = value;
  808. }
  809. let jsonString = JSON.stringify(localStorageData, null, 2);
  810. return jsonString;
  811. },
  812. importLocalStorage: function (localStorageString) {
  813. let localStorageData = JSON.parse(localStorageString);
  814. for (let key in localStorageData) {
  815. localStorage.setItem(key, localStorageData[key]);
  816. }
  817. }
  818. };
  819.  
  820. static Mail = {
  821. getHost: function () {
  822. return 'https://www.mohmal.com/';
  823. },
  824. getInboxUrl: function () {
  825. return this.getHost() + 'zh/inbox';
  826. },
  827. getChangeUrl: function () {
  828. return this.getHost() + 'zh/change';
  829. },
  830. getMesssageUrl: function () {
  831. return this.getHost() + 'zh/message/';
  832. },
  833. Change: async function () {
  834. return new Promise(async (resolve) => {
  835. await global_module.Http({ method: 'GET', url: this.getChangeUrl() });
  836. let r = await global_module.Http({ method: 'GET', url: this.getInboxUrl() });
  837. resolve(this.getEMail(r.ret.responseText));
  838. });
  839. },
  840. getEMail: function (html) {
  841. let emailDom = $('<div></div>').html(html).eq(0).find('[data-email]').eq(0);
  842. if (!emailDom) {
  843. return null;
  844. }
  845. if (!emailDom.text()) {
  846. return null;
  847. }
  848. let email = emailDom.attr('data-email');
  849. return email;
  850. },
  851. analyzeMails: function (html) {
  852. let _that = this;
  853. return new Promise(async (resolve) => {
  854. let Tasks = [];
  855. let MailMsg = [];
  856. let msgIds = $(html).find('tr[data-msg-id]');
  857. for (let i = 0; i < msgIds.length; i++) {
  858. let that = $(msgIds[i]);
  859. let msgId = that.attr('data-msg-id');
  860. let sender = that.find('td[class="sender"]').eq(0).find('a').text();
  861. let time = that.find('td[class="time"]').eq(0).find('a').text();
  862. let url = _that.getMesssageUrl() + msgId;
  863. let task = function () {
  864. return new Promise(async (resolve) => {
  865. let res = await global_module.Http({ method: 'GET', url: url });
  866. let msgDom = $('<div></div>').html(res.ret.responseText).eq(0);
  867. let msgDomHtml = msgDom.html();
  868. let msgDomText = msgDom.text();
  869. let obj = { msgId, time, url, res, sender, html: msgDomHtml, text: msgDomText };
  870. MailMsg.push(obj);
  871. resolve();
  872. });
  873. };
  874. Tasks.push(task());
  875. }
  876. await Promise.all(Tasks);
  877. resolve(MailMsg);
  878. });
  879. },
  880. getMails: async function () {
  881. return new Promise(async (resolve) => {
  882. let res = await global_module.Http({ method: 'GET', url: this.getInboxUrl() });
  883. if (res.code === -1) {
  884. let r = await this.getMail();
  885. resolve(r);
  886. return;
  887. }
  888. if (res.code != 200) {
  889. new Error('Failed to get the verification code, please try again later!');
  890. resolve(null);
  891. return;
  892. }
  893. let html = $('<div></div>').html(res.ret.responseText);
  894. let NoMessages = html.find('tr.no-messages').eq(0);
  895. if (NoMessages.length !== 0) {
  896. resolve([]);
  897. return;
  898. }
  899. let r = await this.analyzeMails(html);
  900. resolve(r);
  901. });
  902. }
  903. };
  904.  
  905. static AnalogInput = {
  906. clickElement: function (el) {
  907. if (!el || (el && 'function' !== typeof el.click)) {
  908. return false;
  909. }
  910. el.click();
  911. return true;
  912. },
  913. doFocusElement: function (el, setValue) {
  914. if (setValue) {
  915. let existingValue = el.value;
  916. el.focus();
  917. el.value !== existingValue && (el.value = existingValue);
  918. } else {
  919. el.focus();
  920. }
  921. },
  922. normalizeEvent: function (el, eventName) {
  923. let ev;
  924. if ('KeyboardEvent' in window) {
  925. ev = new window.KeyboardEvent(eventName, {
  926. bubbles: true,
  927. cancelable: false
  928. });
  929. } else {
  930. ev = el.ownerDocument.createEvent('Events');
  931. ev.initEvent(eventName, true, false);
  932. ev.charCode = 0;
  933. ev.keyCode = 0;
  934. ev.which = 0;
  935. ev.srcElement = el;
  936. ev.target = el;
  937. }
  938. return ev;
  939. },
  940. setValueForElement: function (el) {
  941. let valueToSet = el.value;
  942. this.clickElement(el);
  943. this.doFocusElement(el, false);
  944. el.dispatchEvent(this.normalizeEvent(el, 'keydown'));
  945. el.dispatchEvent(this.normalizeEvent(el, 'keypress'));
  946. el.dispatchEvent(this.normalizeEvent(el, 'keyup'));
  947. el.value !== valueToSet && (el.value = valueToSet);
  948. },
  949. setValueForElementByEvent: function (el) {
  950. let valueToSet = el.value,
  951. ev1 = el.ownerDocument.createEvent('HTMLEvents'),
  952. ev2 = el.ownerDocument.createEvent('HTMLEvents');
  953. el.dispatchEvent(this.normalizeEvent(el, 'keydown'));
  954. el.dispatchEvent(this.normalizeEvent(el, 'keypress'));
  955. el.dispatchEvent(this.normalizeEvent(el, 'keyup'));
  956. ev2.initEvent('input', true, true);
  957. el.dispatchEvent(ev2);
  958. ev1.initEvent('change', true, true);
  959. el.dispatchEvent(ev1);
  960. el.blur();
  961. el.value !== valueToSet && (el.value = valueToSet);
  962. },
  963. doAllFillOperations: function (el, afterValSetFunc) {
  964. this.setValueForElement(el);
  965. if (typeof afterValSetFunc == 'function') {
  966. afterValSetFunc(el);
  967. }
  968. this.setValueForElementByEvent(el);
  969. },
  970. AnalogInput: function (el, op) {
  971. el.value == op ||
  972. this.doAllFillOperations(el, function (theEl) {
  973. if (!theEl.type && theEl.tagName.toLowerCase() === 'span') {
  974. theEl.innerText = op;
  975. return;
  976. }
  977. theEl.value = op;
  978. });
  979. },
  980. AnalogInput2: function (inputElement, inputString) {
  981. const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
  982. const inputEvent = new Event('input', { bubbles: true });
  983. inputElement.focus();
  984. inputElement.value = '';
  985. for (let i = 0; i < inputString.length; i++) {
  986. const char = inputString[i];
  987. nativeInputValueSetter.call(inputElement, inputElement.value + char);
  988. inputElement.dispatchEvent(inputEvent);
  989. }
  990. }
  991. };
  992. }
  993.  
  994. try {
  995. window['global_module'] = global_module;
  996. } catch (e) {}
  997. try {
  998. module.exports = global_module;
  999. } catch (e) {}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址