Bilibili AntiBV

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

当前为 2021-01-10 提交的版本,查看 最新版本

// ==UserScript==
// @name         Bilibili AntiBV
// @icon         https://www.bilibili.com/favicon.ico
// @namespace    https://moe.best/
// @version      1.7.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;

    // 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) {
        const babKey = `bab-${av}`;
        if (sessionStorage.getItem(babKey)) {
            console.warn('[Bilibili AntiBV] abort');
            return;
        }
        sessionStorage.setItem(babKey, 1);
        setTimeout(() => sessionStorage.removeItem(babKey), 5000);
        history.replaceState(null, '', `https://www.bilibili.com/video/av${av}${purgeSearchString()}${window.location.hash}`);
    }
})();