很多小伙伴都发现之前的文章失效了,我让ChatGPT来帮忙写了代码,亲测有效!
下面是我让AI整理的我提出的需求。
具体的需求描述:
遍历在浏览器页面中所有的【勇士区域】,其特征为class="DigitalEntitySummary-module__container_3pUojes0Jk94VKwEcoGXyq"。
在每个【勇士区域】内,找到ID特征为id="content-image-B07DKC1Z9V"的子元素(其中"B07DKC1Z9V"是变量,我们称之为【bookName】)。
使用找到的【bookName】作为参数,定位并点击【usb按钮】。【usb按钮】的特征为id="DOWNLOAD_AND_TRANSFER_ACTION_" + bookName。
在【usb按钮】被点击后,定位并点击【小圆圈】,其特征为class="RadioButton-module_radio__1k8O3"。
等待1秒后,定位并点击【下载按钮】。【下载按钮】的特征为id="DOWNLOAD_AND_TRANSFER_ACTION_" + bookName + "_CONFIRM"。
【下载按钮】点击后,会弹出【成功窗口】,其特征为id="notification-success"。在【成功窗口】出现后,点击其子元素【关闭按钮】,其特征为id="notification-close"。
这一系列操作需要在页面的所有【勇士区域】上进行,且要确保不会重复下载相同的内容。
当一个页面的所有【勇士区域】都被处理过后,脚本需要自动翻页到下一个页面,直到所有的页面都被处理过。页面的状态可以通过class="page-item active"和class="page-item"来识别。
以上是你的需求,我提供了一个可以在Chrome控制台执行的JavaScript脚本来完成这些操作,该脚本使用了异步处理和定时器来确保各步骤的顺序执行以及等待新页面的加载。
下面是完整的代码。
let downloadedBooks = new Set();
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function findBookNames() {
const warriorAreas = document.querySelectorAll('.DigitalEntitySummary-module__container_3pUojes0Jk94VKwEcoGXyq');
const bookNames = [];
for (const warriorArea of warriorAreas) {
const imageContainer = warriorArea.querySelector('[id^="content-image-"]');
if (imageContainer) {
const bookName = imageContainer.id.split('-')[2];
if (!downloadedBooks.has(bookName)) {
bookNames.push(bookName);
downloadedBooks.add(bookName);
}
}
}
return bookNames;
}
async function clickElements(bookNames) {
for (const bookName of bookNames) {
const usbButton = document.querySelector(`#DOWNLOAD_AND_TRANSFER_ACTION_${bookName}`);
usbButton.click();
await delay(1000);
const radioButton = document.querySelector(`#download_and_transfer_list_${bookName}_0`);
radioButton.click();
await delay(1000);
const confirmButton = document.querySelector(`#DOWNLOAD_AND_TRANSFER_ACTION_${bookName}_CONFIRM`);
confirmButton.click();
await delay(1000);
const closeButton = document.querySelector('#notification-close');
closeButton.click();
await delay(1000);
}
}
async function processPage() {
const bookNames = findBookNames();
await clickElements(bookNames);
}
async function processAllPages() {
// 获取当前活跃的页面的索引
let activePageIndex = Array.from(document.querySelectorAll('.page-item')).findIndex(item => item.classList.contains('active'));
while (activePageIndex < document.querySelectorAll('.page-item').length - 1) {
// 处理当前页面
await processPage();
// 获取所有的页面链接元素
const pageItems = document.querySelectorAll('.page-item');
// 点击下一个页面链接
pageItems[activePageIndex + 1].click();
// 等待页面加载
await delay(5000);
// 更新活跃的页面的索引
activePageIndex = Array.from(document.querySelectorAll('.page-item')).findIndex(item => item.classList.contains('active'));
}
// 处理最后一个页面
await processPage();
}
processAllPages();
