Greasy Fork

Greasy Fork is available in English.

自用bilibili脚本

吧啦吧啦

当前为 2023-04-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         自用bilibili脚本
// @namespace    mimiko/bilibili
// @version      0.0.14
// @description  吧啦吧啦
// @author       Mimiko
// @license      MIT
// @match        *://*.bilibili.com/*
// @grant        GM.addStyle
// grant        GM.registerMenuCommand
// grant        GM.xmlHttpRequest
// ==/UserScript==
// http://greasyfork.icu/zh-CN/scripts/436748-%E8%87%AA%E7%94%A8bilibili%E8%84%9A%E6%9C%AC
"use strict";
(() => {
  if (window.top !== window.self) return;
  // variable
  class Cache {
    list;
    constructor() {
      this.list = JSON.parse(localStorage.getItem("cache-recommend") || "[]");
    }
    add(id) {
      if (this.has(id)) return;
      this.list = [...this.list, id].slice(-1e3);
    }
    has(id) {
      return this.list.includes(id);
    }
    save() {
      localStorage.setItem("cache-recommend", JSON.stringify(this.list));
    }
  }
  const cache = new Cache();
  // function
  const getElement = (selector) =>
    new Promise((resolve) => {
      const fn = () => {
        if (document.hidden) return;
        const $el = document.querySelectorAll(selector);
        if (!$el?.length) return;
        window.clearInterval(timer);
        resolve([...$el]);
      };
      const timer = window.setInterval(fn, 50);
      fn();
    });
  const hideElement = (...listSelector) =>
    GM.addStyle(
      listSelector
        .map((selector) => `${selector} { display: none !important; }`)
        .join("\n")
    );
  const removeElement = (selector) =>
    document.querySelectorAll(selector).forEach(($it) => $it.remove());
  const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
  // main
  const asIndex = async () => {
    (await getElement(".recommended-swipe"))[0].remove();
    GM.addStyle(`
    .bili-video-card.is-rcmd.hidden { opacity: 0.1; transition: opacity 0.3s; }
    .bili-video-card.is-rcmd.hidden:hover { opacity: 1; }
    `);
    const [$container] = await getElement(".recommend-container__2-line");
    const hide = async () => {
      observer.disconnect();
      const $listItem = await getElement(".bili-video-card.is-rcmd");
      $listItem.forEach(($it) => {
        const check = () => {
          const $a = $it.querySelector("a");
          if (!$a) return false;
          const id = $a.href.replace(/.*\/BV/, "").replace(/\/\?.*/, "");
          if (cache.has(id)) return false;
          cache.add(id);
          const $stats = $it.querySelector(".bili-video-card__stats");
          if (!$stats) return false;
          const text = $stats.textContent;
          if (!text) return false;
          if (!text.includes("万")) return false;
          const amount = parseFloat(text.split("万")[0]);
          if (amount < 3) return false;
          return true;
        };
        const result = check();
        if (!result) $it.classList.add("hidden");
      });
      cache.save();
      $listItem.sort(($a, $b) => {
        const aHidden = $a.classList.contains("hidden");
        const bHidden = $b.classList.contains("hidden");
        if (aHidden && !bHidden) return 1;
        if (!aHidden && bHidden) return -1;
        return 0;
      });
      $listItem.forEach(($it) => $container.appendChild($it));
      observer.observe($container, {
        childList: true,
      });
    };
    const observer = new MutationObserver(hide);
    await hide();
  };
  const main = async () => {
    if (window.location.pathname === "/") await asIndex();
  };
  main();
})();