1 setTimeout(() => { 2 const WAIT = 150; 3 4 const $a = (sel) => Array.from(document.querySelectorAll(sel)); 5 const itemIdMap = JSON.parse(localStorage.getItem("itemIdMap") || "{}"); 6 const elements = $a("span.cu-icon-link .cu-icon-link-icon a"); 7 const tasks = elements.map((elem, ind) => { 8 return new Promise((resolve) => { 9 setTimeout(async () => { 10 const { href } = elem; 11 try { 12 if (itemIdMap[href]) { 13 updateDOM(elem, itemIdMap[href]); 14 resolve(); 15 return; 16 } 17 const response = await fetch(href); 18 const rawhtml = await response.text(); 19 let idStr = "未找到"; 20 const codeStartIndex = rawhtml.indexOf("<code>"); 21 if (codeStartIndex !== -1) { 22 const codeEndIndex = rawhtml.indexOf("</code>", codeStartIndex); 23 if (codeEndIndex !== -1) { 24 idStr = rawhtml.slice(codeStartIndex + 6, codeEndIndex); 25 } 26 } 27 28 itemIdMap[href] = idStr; 29 updateDOM(elem, idStr); 30 31 } catch (error) { 32 console.error(`抓取 ${href} 失败:`, error); 33 } finally { 34 resolve(); 35 } 36 }, ind * WAIT); 37 }); 38 }); 39 Promise.all(tasks).then(() => { 40 localStorage.setItem("itemIdMap", JSON.stringify(itemIdMap)); 41 console.log("结果已保存到 localStorage"); 42 }); 43 }, 5000); 44 45 function updateDOM(elem, idStr) { 46 elem.setAttribute("title", idStr); 47 const nextElem = elem.parentNode.nextElementSibling; 48 if (nextElem && nextElem.children.length > 0) { 49 nextElem.children[0].setAttribute("title", idStr); 50 } 51 }

浙公网安备 33010602011771号