WOS-SID

获取SID

当前为 2025-03-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         WOS-SID
// @namespace    http://tampermonkey.net/
// @version      2025-03-19
// @description  获取SID
// @author       gzh
// @license MIT
// @match        https://www.tampermonkey.net/index.php?version=5.3.3&ext=iikm&updated=true
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none
// ==/UserScript==


// 提取 SID 后立即执行下载 ==
(function() {
  // 1. 定义全局变量
  let SID = null; // 存储提取的 SID

  // 2. 拦截 XHR 请求以提取 SID
  const originalXHR = window.XMLHttpRequest;
  window.XMLHttpRequest = class extends originalXHR {
    open(method, url) {
      this._url = url; // 记录请求 URL
      return super.open(method, url);
    }
    setRequestHeader(name, value) {
      // 检测请求头中的 SID
      if (name.toLowerCase().includes('sid')) {
        console.log('✅ 提取到 SID:', value);
        SID = value; // 存储 SID
        performDownload(); // 提取到 SID 后立即执行下载
      }
      return super.setRequestHeader(name, value);
    }
  };

  // 3. 安全点击函数(带重试机制)
  const safeClick = async (selector, retries = 3) => {
    for (let i = 0; i < retries; i++) {
      const element = document.querySelector(selector);
      if (element) {
        element.click();
        return true;
      }
      await new Promise(resolve => setTimeout(resolve, 1000));
    }
    return false;
  };

  // 4. 核心下载逻辑
  const performDownload = async () => {
    if (!SID) {
      console.error('❌ 未找到 SID,无法执行下载');
      return;
    }

    console.log(`[${new Date().toLocaleTimeString()}] 启动下载,SID: ${SID}`);
    try {
      // 分步点击
      if (!await safeClick(".fullRecord-export-option .mat-button-wrapper")) return;
      await new Promise(resolve => setTimeout(resolve, 500));

      if (!await safeClick("#exportToEnwDesktopButton")) return;
      await new Promise(resolve => setTimeout(resolve, 500));

      if (await safeClick("#FullRecordExportToEnwBtnover")) {
        console.log(`[${new Date().toLocaleTimeString()}] 下载完成`);
      }
    } catch (error) {
      console.error('下载失败:', error);
    }
  };

  // 5. 暴露全局函数
  window.performDownload = performDownload;

  // 6. 自动启动逻辑
  console.log('脚本已注入,等待提取 SID...');

  // 7. 监听页面加载完成事件
  if (document.readyState === 'complete') {
    console.log('页面已加载完成,等待 SID...');
  } else {
    window.addEventListener('load', () => {
      console.log('页面加载完成,等待 SID...');
    });
  }
})();