(node, path = [], counter = { count: 0, total: 0, shouldCancel: () => false })
| 475 | }); |
| 476 | |
| 477 | async function scanBookmarks(node, path = [], counter = { count: 0, total: 0, shouldCancel: () => false }) { |
| 478 | try { |
| 479 | console.log(counter.shouldCancel()) |
| 480 | // 检查是否已取消 |
| 481 | if (counter.shouldCancel()) { |
| 482 | throw new Error('Scan cancelled'); |
| 483 | } |
| 484 | |
| 485 | if (node.children) { |
| 486 | const currentPath = [...path, node.title || getMessage('untitledFolder')]; |
| 487 | let hasBookmarks = false; |
| 488 | let hasBookmarksInSubfolders = false; |
| 489 | let bookmarksToCheck = []; |
| 490 | |
| 491 | // 遍历子节点,收集需要检查的书签 |
| 492 | for (const child of node.children) { |
| 493 | if (child.url) { |
| 494 | // 任何书签都应该将 hasBookmarks 设为 true |
| 495 | hasBookmarks = true; |
| 496 | // 只有需要检查的书签才加入检查列表 |
| 497 | if (!CONFIG.validProtocols.some(protocol => child.url.startsWith(protocol))) { |
| 498 | bookmarksToCheck.push({ |
| 499 | id: child.id, |
| 500 | title: child.title, |
| 501 | url: child.url, |
| 502 | path: currentPath |
| 503 | }); |
| 504 | } |
| 505 | } else { |
| 506 | const subfoldersHasBookmarks = await scanBookmarks(child, currentPath, counter); |
| 507 | hasBookmarksInSubfolders = hasBookmarksInSubfolders || subfoldersHasBookmarks; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // 检查收集到的书签 |
| 512 | if (bookmarksToCheck.length > 0) { |
| 513 | const { results } = await checkBookmarksInBatch(bookmarksToCheck, counter); |
| 514 | |
| 515 | if (results && Array.isArray(results)) { |
| 516 | results.forEach(({ bookmark, isValid, reason }) => { |
| 517 | if (!isValid) { |
| 518 | addInvalidBookmark(bookmark, reason); |
| 519 | } |
| 520 | }); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // 空文件夹判断:当前文件夹没有书签且子文件夹都是空的 |
| 525 | if (!hasBookmarks && !hasBookmarksInSubfolders && node.id && node.title) { |
| 526 | if (!isSpecialFolder(node.id)) { |
| 527 | // 避免重复添加 |
| 528 | if (!emptyFolders.some(folder => folder.id === node.id)) { |
| 529 | emptyFolders.push({ |
| 530 | id: node.id, |
| 531 | title: node.title, |
| 532 | path: currentPath |
| 533 | }); |
| 534 | addEmptyFolder({ |
no test coverage detected