MCPcopy Create free account
hub / github.com/Yantesoft/LinuxdoAssistant / waitForElement

Function waitForElement

linuxdo-helper.user.js:708–743  ·  view source on GitHub ↗

* 等待指定元素出现在页面中 * @param {string} selector - CSS选择器 * @returns {Promise } 找到的元素

(selector)

Source from the content-addressed store, hash-verified

706 * @returns {Promise<HTMLElement>} 找到的元素
707 */
708 function waitForElement(selector) {
709 return new Promise((resolve, reject) => {
710 const element = document.querySelector(selector);
711
712 if (element) {
713 resolve(element);
714 return;
715 }
716
717 const observer = new MutationObserver(() => {
718 const element = document.querySelector(selector);
719
720 if (element) {
721 observer.disconnect();
722 resolve(element);
723 }
724 });
725
726 observer.observe(document.body, {
727 childList: true,
728 subtree: true
729 });
730
731 const timeout = randomByJitter(
732 getConfig().waitForElement,
733 RANDOM_DELAY_CONFIG.waitForElementJitter,
734 500
735 );
736
737 setTimeout(() => {
738 observer.disconnect();
739 console.log('【错误】未找到元素:', selector);
740 reject(new Error('未找到:' + selector));
741 }, timeout);
742 });
743 }
744
745 /**
746 * 获取页面中的原始链接列表

Callers 2

findChatButtonFunction · 0.85
startAutoScrollFunction · 0.85

Calls 2

randomByJitterFunction · 0.85
getConfigFunction · 0.85

Tested by

no test coverage detected