Bilibili AntiBV

自动将地址栏 bv 链接还原为 av 链接,非重定向,不会导致页面刷新,且删除 searchstring 中的 spm_id_from,保留其余部分

当前为 2020-03-24 提交的版本,查看 最新版本

// ==UserScript==
// @name         Bilibili AntiBV
// @icon         https://www.bilibili.com/favicon.ico
// @namespace    https://moe.best/
// @version      1.1
// @description  自动将地址栏 bv 链接还原为 av 链接,非重定向,不会导致页面刷新,且删除 searchstring 中的 spm_id_from,保留其余部分
// @author       神代绮凛
// @include      /^https:\/\/www\.bilibili\.com\/video\/[Bb][Vv]/
// @require      https://cdn.jsdelivr.net/npm/[email protected]/src/simplequerystring.min.js
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

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

    // https://www.zhihu.com/question/381784377/answer/1098804685
    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 delSpmIdFromSS = () => {
        const ss = simpleQueryString.parse(location.search);
        delete ss.spm_id_from;
        const result = simpleQueryString.stringify(ss);
        return result ? `?${result}` : '';
    };

    const av = get('aid') || bv2av(get('bvid'));
    if (av) history.replaceState(null, '', `https://www.bilibili.com/video/av${av}${delSpmIdFromSS()}`);
})();