MCPcopy Index your code
hub / github.com/ChinaGodMan/UserScripts / checkAndInsert

Function checkAndInsert

github-to-deepwiki/github-to-deepwiki.user.js:377–406  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

375let attemptCount = 0
376
377function checkAndInsert() {
378 if (checkTimer) {
379 clearTimeout(checkTimer)
380 checkTimer = null
381 }
382
383 insertCustomElement(document.querySelector(MOBILE_SELECTOR), 'beforeend')
384 const elements = document.querySelectorAll(DESKTOP_SELECTOR)
385 if (elements.length >= 2) {
386 const target = elements[elements.length - 2]
387 if (insertCustomElement(target)) {
388 console.log('插入成功,终止检测')
389 return true
390 }
391 }
392
393 if (attemptCount >= MAX_ATTEMPTS) {
394 console.warn(`已达到最大尝试次数 ${MAX_ATTEMPTS} 次`)
395 return false
396 }
397
398 currentDelay = Math.min(currentDelay * 1.5, MAX_DELAY)
399 attemptCount++
400
401 checkTimer = setTimeout(() => {
402 checkAndInsert()
403 }, currentDelay)
404
405 return false
406}
407
408function insertCustomElement(target) {
409 if (!target?.parentNode) return false

Callers 1

Calls 1

insertCustomElementFunction · 0.85

Tested by

no test coverage detected