555视频网:自动切换线路

播放视频时,有多条线路,播放失败时自动点击“切换线路”按钮。2分钟后自动停止。

// ==UserScript==
// @name         555视频网:自动切换线路
// @version      1.1
// @description  播放视频时,有多条线路,播放失败时自动点击“切换线路”按钮。2分钟后自动停止。
// @match        https://www.wuwu559.space/*
// @match        https://www.bsfl50rg.wiki/*
// @match        https://www.k8uqvj1.wiki/*
// @match        https://www.ehk1d9lcx.wiki/*
// @match        https://www.8gqbz9wvsz.wiki/*
// @match        https://www.q7hkffjn.shop/*
// @match        https://www.gyempb9zy8.wiki/*
// @match        https://www.5iskfe97.wiki/
// @match        https://www.unmbn1c7.wiki/*
// @match        https://www.52kiq6dg.wiki/*
// @match        https://www.w1xycfoy.life/*
// @match        https://www.5iskfe97.wiki/*
// @namespace   https://greasyfork.org/users/1171320
// @author         yzcjd
// @author2       ChatGPT4 辅助
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  const MAX_DURATION = 2 * 60 * 1000; // 最长持续时间:2分钟
  const startTime = Date.now();
  let lastClickTime = 0;

  function clickLineSwitchButton() {
    const now = Date.now();
    if (now - startTime > MAX_DURATION) {
      console.log('[自动切换线路] 已达2分钟上限,停止尝试');
      observer.disconnect();
      clearInterval(timer);
      return;
    }

    const btn = Array.from(document.querySelectorAll('a.btn.bg-line'))
      .find(el => el.textContent.includes('切换线路') && el.offsetParent !== null);

    if (btn && now - lastClickTime > 3000) { // 防止快速重复点击
      console.log('[自动切换线路] 检测到按钮,立即点击');
      btn.click();
      lastClickTime = now;
    }
  }

  // 使用 MutationObserver 实时检测页面变动
  const observer = new MutationObserver(() => {
    clickLineSwitchButton();
  });

  observer.observe(document.body, {
    childList: true,
    subtree: true,
  });

  // 定时器作为保险机制(防止 MutationObserver 被绕过)
  const timer = setInterval(() => {
    clickLineSwitchButton();
  }, 2000);
})();