猴油脚本编写问题 代码运行时灵时不灵的 求助大佬 什么问题导致的 怎么修改?-灵析社区

嚯嚯嚯嚯嚯嚯

某音小店的一个打款页面,当点击搜索后 DOM 会发生改变 正常是这样的 ![](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241107/829bad67c914d1fe9928bfb4969793d0.png) 元素界面 ![](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241107/6144956e7e52cb8b7c22c9c9d7b88702.png) 当存在打款记录的时候 ![](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241107/e08bedcb6b27796d5de2b9f15dc47161.png) 元素界面 ![](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241107/ccef7a8a80841b4f14d8f6ca7b50c6b4.png) 我的猴油脚本代码如下 // 异步延迟函数 function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } // 修改元素样式 function modifyElement(element) { element.style.paddingLeft = '10px'; element.style.color = '#FF7F00'; element.className = 'styles_title__1X-Qt'; } // 监视节点和属性变化 function startObserving(element, callback) { const config = {attributes: true, childList: true, subtree: true}; const observer = new MutationObserver((mutationsList) => { for (const mutation of mutationsList) { if (mutation.type === 'childList') { callback(mutation); } else if (mutation.type === 'attributes') { callback(mutation); } } }); observer.observe(element, config); return () => { observer.disconnect(); }; } function main() { const parent = document.getElementById('orderAppContainer'); const search = parent.querySelector('button.ant-btn.ant-btn-primary'); // 在点击事件中增加一层异步处理,避免因为DOM未完全加载而导致的找不到元素的问题 search.addEventListener('click', async function () { await delay(1000); const input = parent.querySelector('#shop_order_no'); const value = input.getAttribute('value'); const regex = /[0-9]{19}/g; const match = regex.exec(value); if (match) { // 打款记录 const record = startObserving(parent, (mutation) => { const target = mutation.target; if (target.matches('span.styles_prompt__1kJY-')) { modifyElement(target); } else { const element = target.querySelector('span.styles_prompt__1kJY-'); if (element) { modifyElement(element); } } }); } }); } window.onload = function () { main(); }; 我想要的效果 ![](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241107/14711885b001c4813f73b7eb3b75425f.png) 遇到的问题是 第一次去搜索的时候 脚本没有正常运行 就是没有修改元素 但是第二次去搜索的时候 就正常的 为什么啊 怎么修改才能保证每次都正常运行 谢谢大佬 如题

阅读量:14

点赞量:0

问AI
我已经找到解决方法了,监视页面页面某一个属性 重新加载main 函数即可