Bilibili AntiBV

自动在地址栏中将 bv 还原为 av,非重定向,不会导致页面刷新,顺便清除 search string 中所有无用参数

目前为 2021-02-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         Bilibili AntiBV
// @icon         https://www.bilibili.com/favicon.ico
// @namespace    https://moe.best/
// @version      1.8.0
// @description  自动在地址栏中将 bv 还原为 av,非重定向,不会导致页面刷新,顺便清除 search string 中所有无用参数
// @author       神代绮凛
// @include      /^https:\/\/www\.bilibili\.com\/(s\/)?video\/[BbAa][Vv]/
// @require      https://cdn.jsdelivr.net/npm/[email protected]/src/simplequerystring.min.js
// @license      WTFPL
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    let REDIRECT_S_LINK = GM_getValue('redirect_s_link', true);
    GM_registerMenuCommand('自动重定向 /s/video/xxx', () => {
        REDIRECT_S_LINK = confirm(`自动将 /s/video/xxx 重定向至 /video/xxx
“确定”开启,“取消”关闭
当前:${REDIRECT_S_LINK ? '开启' : '关闭'}`);
        GM_setValue('redirect_s_link', REDIRECT_S_LINK);
    });

    if (REDIRECT_S_LINK && location.pathname.startsWith('/s/video/')) {
        location.pathname = location.pathname.replace(/^\/s/, '');
        return;
    }

    const win = typeof unsafeWindow === 'undefined' ? window : unsafeWindow;

    const wrapHistory = menthod => {
        const fn = win.history[menthod];
        const e = new Event(menthod);
        return function () {
            setTimeout(() => window.dispatchEvent(e));
            return fn.apply(this, arguments);
        };
    };
    win.history.pushState = wrapHistory('pushState');

    // Get from __INITIAL_STATE__
    const get = key => {
        const is = win.__INITIAL_STATE__ || {};
        return is[key] || (is.videoData && is.videoData[key]) || win[key];
    };

    // https://www.zhihu.com/question/381784377/answer/1099438784
    const bv2av = bv => {
        if (!bv) return;

        const pos = [11, 10, 3, 8, 4, 6];
        const base = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF';
        const table = {};
        for (let i = 0; i < base.length; i++) table[base[i]] = i;

        let r = 0;
        for (let i = 0; i < pos.length; i++) r += table[bv[pos[i]]] * 58 ** i;
        return (r - 8728348608) ^ 177451812;
    };

    const purgeSearchString = () => {
        const { p, t } = simpleQueryString.parse(location.search);
        const result = simpleQueryString.stringify({ p, t });
        return result ? `?${result}` : '';
    };

    const replaceUrl = () => {
        const av = get('aid') || bv2av(get('bvid'));
        if (av) {
            const avUrl = `/video/av${av}${purgeSearchString()}${location.hash}`;
            const BABKey = `BAB-${avUrl}`;
            if (sessionStorage.getItem(BABKey)) {
                console.warn('[Bilibili AntiBV] abort');
                return;
            }
            sessionStorage.setItem(BABKey, 1);
            setTimeout(() => sessionStorage.removeItem(BABKey), 1000);
            history.replaceState(null, '', avUrl);
        }
    };

    replaceUrl();
    window.addEventListener('pushState', replaceUrl);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址