You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Firefox DevTools中循环打开新标签页并获取下载链接?

可行实现方案

核心思路

通过递归逐个处理每个下载按钮,打开新标签页后等待页面完全加载(包括动态渲染内容),定时检查目标元素是否出现,获取链接后关闭标签页,再处理下一个,避免同时操作多个标签页引发的反爬或页面引用丢失问题。

代码实现(Firefox控制台执行)

// 1. 替换为filecrypt页面上蓝色下载按钮的实际选择器
const downloadButtons = document.querySelectorAll('a.blue-download-button'); 
const zippyDownloadLinks = [];
let currentItemIndex = 0;

function handleNextButton() {
  // 所有按钮处理完毕,输出结果
  if (currentItemIndex >= downloadButtons.length) {
    console.log('全部下载链接已获取:', zippyDownloadLinks);
    return;
  }

  const targetButton = downloadButtons[currentItemIndex];
  // 若按钮不是a标签,需解析onclick或模拟点击,这里假设是带href的a标签
  const targetPageUrl = targetButton.href;

  // 打开新标签页
  const newTab = window.open(targetPageUrl, '_blank');
  if (!newTab) {
    console.error('弹窗被拦截,请允许当前网站弹出新窗口');
    currentItemIndex++;
    handleNextButton();
    return;
  }

  // 定时检查目标元素是否出现
  function checkDLButton() {
    if (newTab.closed) {
      console.error('标签页已关闭,跳过当前项');
      currentItemIndex++;
      handleNextButton();
      return;
    }

    try {
      const dlButton = newTab.document.getElementById('dlbutton');
      if (dlButton) {
        // 获取下载链接并存储
        const downloadLink = dlButton.href;
        zippyDownloadLinks.push(downloadLink);
        console.log(`已获取第${currentItemIndex + 1}个链接:`, downloadLink);
        
        // 关闭标签页,延迟处理下一个避免反爬
        newTab.close();
        currentItemIndex++;
        setTimeout(handleNextButton, 1200);
      } else {
        // 未找到元素,500ms后重试
        setTimeout(checkDLButton, 500);
      }
    } catch (err) {
      // 处理跨域或文档访问异常,延迟重试
      console.warn('访问标签页文档出错,稍后重试:', err);
      setTimeout(checkDLButton, 1000);
    }
  }

  // 页面加载完成后开始检查元素
  newTab.addEventListener('load', () => {
    // 给页面动态渲染留500ms缓冲时间
    setTimeout(checkDLButton, 500);
  });

  // 15秒超时保护,避免单个页面卡住整个流程
  setTimeout(() => {
    if (!newTab.closed) {
      console.error(`第${currentItemIndex + 1}个页面加载超时,跳过`);
      newTab.close();
      currentItemIndex++;
      handleNextButton();
    }
  }, 15000);
}

// 启动流程
handleNextButton();

关键注意事项

  • 选择器替换:必须根据filecrypt页面的实际HTML结构,修改document.querySelectorAll的参数(比如按钮是带特定class的<button>,就改成document.querySelectorAll('button.your-download-class'))。
  • 弹窗权限:确保Firefox允许当前filecrypt页面弹出新标签页,否则window.open会失败。
  • 反爬适配:如果zippyshare有等待验证机制,可适当延长setTimeout的延迟时间(比如把1200ms改成2000ms)。
  • 非a标签处理:若下载按钮不是<a>标签,需解析其onclick属性中的跳转链接,或用targetButton.click()模拟点击(注意模拟点击可能触发弹窗拦截)。

之前报错的原因

  1. 未等待动态内容:直接在页面load后获取元素,而zippyshare的dlbutton是动态生成的,此时还未渲染完成。
  2. 页面引用丢失:循环+setTimeout的方式会导致新标签页的引用被覆盖或销毁,回调中无法访问有效页面实例。
  3. 并发操作冲突:同时打开多个标签页可能触发浏览器反爬限制,或导致页面资源加载混乱。

内容的提问来源于stack exchange,提问作者Ξένη Γήινος

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:10:29