Greasy Fork

Greasy Fork is available in English.

自用bilibili脚本

吧啦吧啦

当前为 2022-01-21 提交的版本,查看 最新版本

// ==UserScript==
// @name         自用bilibili脚本
// @namespace    mimiko/bilibili
// @version      0.0.9
// @description  吧啦吧啦
// @author       Mimiko
// @license      MIT
// @match        *://*.bilibili.com/*
// @grant        GM.addStyle
// @grant        GM.registerMenuCommand
// grant        GM.xmlHttpRequest
// ==/UserScript==

(() => {
    if (window.top !== window.self)
        return;
    const Monkey = GM;
    const cache = new Map();
    const asBangumiMain = () => {
        asPlayerMain();
        hideEl('#review_module');
        Monkey.addStyle('.bpx-player-container { box-shadow: none !important; }');
    };
    const asCommonMain = () => {
        hideEl('#internationalHeader .nav-link-item:last-of-type');
    };
    const asIndexLive = async () => {
        hideEl('.player-area-ctnr');
        await getEl('video');
        removeEl('.player-area-ctnr');
    };
    const asIndexMain = async () => {
        hideEl('.contact-help');
        hideEl('#reportFirst2');
        hideEl('#reportFirst3');
        hideEl('#bili_live');
        hideEl('#bili_guochuang');
        hideEl('#bili_manga');
        await getEl('#elevator .item.sortable');
        hideElByText('#elevator .item.sortable', '直播');
        hideElByText('#elevator .item.sortable', '国创');
        hideElByText('#elevator .item.sortable', '漫画');
    };
    const asLive = () => {
        const path = window.location.pathname;
        if (path === '/')
            return asIndexLive();
        if (parseInt(path.substring(1), 10))
            return asRoomLive();
    };
    const asMain = () => {
        asCommonMain();
        const path = window.location.pathname;
        if (path === '/')
            return asIndexMain();
        if (path.startsWith('/video/BV'))
            return asVideoMain();
        if (path.startsWith('/bangumi/play/'))
            return asBangumiMain();
    };
    const asPlayerMain = async () => {
        hideEl('.bilibili-player-electric-panel');
        hideEl('.bilibili-player-ending-panel');
        hideEl('.bilibili-player-video-hint');
        await turnWide();
        await sleep(50);
        focusPlayer();
    };
    const asRoomLive = () => {
        removeEl('#my-dear-haruna-vm');
        hideEl('#room-background-vm');
    };
    const asVideoMain = () => {
        asPlayerMain();
        hideEl('.bilibili-player-video-popup-vote');
        hideEl('.bilibili-player-score');
        Monkey.addStyle('.bilibili-player { box-shadow: none !important; }');
    };
    const focusPlayer = async () => (await getEl('video', 'bwp-video')).scrollIntoView({
        behavior: 'smooth',
        block: 'center',
        inline: 'center',
    });
    const getEl = (...listSelector) => new Promise(resolve => {
        const fn = () => listSelector.some(selector => {
            const $el = document.querySelector(selector);
            if (!$el)
                return false;
            window.clearInterval(timer);
            resolve($el);
            return true;
        });
        const timer = window.setInterval(fn, 50);
        fn();
    });
    const hideEl = (selector) => Monkey.addStyle(`${selector} { display: none !important; }`);
    const hideElByText = (selector, text) => {
        const $listNode = Array.from(document.querySelectorAll(selector))
            .filter($it => $it.textContent?.trim() === text);
        $listNode.forEach($it => $it.style.display = 'none');
    };
    const main = () => {
        route();
        watch();
    };
    const removeEl = (selector) => document.querySelectorAll(selector).forEach($it => $it.remove());
    const route = () => {
        const host = window.location.host.split('.')[0];
        if (host === 'www')
            return asMain();
        if (host === 'live')
            return asLive();
    };
    const sleep = (time) => new Promise(resolve => setTimeout(resolve, time));
    const turnWide = async () => {
        const isWide = document.body.classList.contains('player-mode-widescreen');
        if (isWide)
            return;
        const $btn = await getEl('.bilibili-player-video-btn-widescreen', '.squirtle-video-widescreen');
        $btn.click();
    };
    const watch = () => {
        cache.set('location', window.location.href);
        window.setInterval(() => {
            if (window.location.href === cache.get('location'))
                return;
            cache.set('location', window.location.href);
            route();
        }, 1e3);
    };
    main();
})();