Greasy Fork is available in English.

Bilibili AntiBV

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

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

(function() {
    'use strict';

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

    // 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 av = get('aid') || bv2av(get('bvid'));
    if (av) history.replaceState(null, '', `https://www.bilibili.com/video/av${av}${purgeSearchString()}${window.location.hash}`);
})();