AniPlusPlus

Auto-skip the loading screen on AniPlus

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         AniPlusPlus
// @namespace    https://aniplus.co
// @version      2.0.0
// @license      MIT
// @description  Auto-skip the loading screen on AniPlus
// @match        https://aniplus.co/*
// @match        http://aniplus.co/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  const LOG = '[AniPlusPlus]';
  const GLOBAL_KEY = '__aniplusPlus';

  function state() {
    return window[GLOBAL_KEY] || (window[GLOBAL_KEY] = {});
  }

  function skipLoadingAd() {
    const el = document.querySelector('[class*="ad-window"]');
    if (!el) return false;

    const fiberKey = Object.keys(el).find((k) => k.startsWith('__reactFiber'));
    if (!fiberKey) return false;

    let fiber = el[fiberKey];
    while (fiber) {
      const onFinish = fiber.memoizedProps?.onFinish;
      if (fiber.type?.name === 'y' && typeof onFinish === 'function') {
        onFinish();
        console.log(LOG, 'loading skipped on', location.pathname);
        return true;
      }
      fiber = fiber.return;
    }

    return false;
  }

  function stopLoadingWatch(s) {
    if (s.loadingObserver) s.loadingObserver.disconnect();
    if (s.loadingInterval) clearInterval(s.loadingInterval);
    if (s.loadingTimeout) clearTimeout(s.loadingTimeout);
    s.loadingObserver = null;
    s.loadingInterval = null;
    s.loadingTimeout = null;
  }

  function watchForLoadingAd() {
    const s = state();
    stopLoadingWatch(s);

    if (skipLoadingAd()) return;

    s.loadingObserver = new MutationObserver(() => {
      if (skipLoadingAd()) stopLoadingWatch(s);
    });

    const start = () => {
      if (skipLoadingAd()) return;

      s.loadingObserver.observe(document.documentElement, {
        childList: true,
        subtree: true,
      });

      s.loadingInterval = setInterval(() => {
        if (skipLoadingAd()) stopLoadingWatch(s);
      }, 50);

      s.loadingTimeout = setTimeout(() => stopLoadingWatch(s), 30000);
    };

    if (document.documentElement) start();
    else document.addEventListener('DOMContentLoaded', start, { once: true });
  }

  function installHistoryHooks() {
    const s = state();
    if (s.historyHooked) return;
    s.historyHooked = true;

    const rerun = () => setTimeout(watchForLoadingAd, 0);
    const wrap = (original) =>
      function () {
        const result = original.apply(this, arguments);
        rerun();
        return result;
      };

    history.pushState = wrap(history.pushState);
    history.replaceState = wrap(history.replaceState);
    window.addEventListener('popstate', rerun);
  }

  installHistoryHooks();
  watchForLoadingAd();
})();