Greasy Fork is available in English.

bv2av

Change bv to av

// ==UserScript==
// @name         bv2av
// @namespace    http://tampermonkey.net/
// @version      1.12
// @description  Change bv to av
// @author       ouuan
// @match        *://*.bilibili.com/*
// @grant        none
// ==/UserScript==

// Algorithm from https://www.zhihu.com/question/381784377/answer/1099438784

(function() {
    'use strict';

    const table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF';
    let tr = {};
    for (let i = 0; i < 58; ++i) {
        tr[table[i]] = i;
    }

    const s = [11,10,3,8,4,6];
    const xor = 177451812;
    const add = 8728348608;

    function dec(x) {
        let r = 0;
        for (let i = 0; i < 6; ++i) {
            r += tr[x[s[i]]] * (58 ** i);
        }
        return 'av' + String((r - add) ^ xor);
    }

    function bv2av(x) {
        if (!x.match("www.bilibili.com")) {
            return x;
        }
        const bvs = x.match(/[bB][vV][fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF]{10}/g);
        if (bvs) {
            for (const bv of bvs) {
                x = x.replace(bv, dec(bv));
            }
        }
        if (x.match(/bilibili.com\/av\d+/)) {
            x = x.replace(/(av\d+)/, 'video/$1')
        }
        return x;
    }

    const url = window.location.href;
    const newUrl = bv2av(url);
    if (url != newUrl) {
        window.history.pushState(null, null, newUrl);
    }

    setInterval(function() {
        const as = document.querySelectorAll('a');
        for(const o of as) {
            if (o.href) {
                o.href = bv2av(o.href);
            }
        }
        const divs = document.querySelectorAll('div');
        for(const o of divs) {
            if (o.title) {
                o.title = bv2av(o.title);
            }
        }
    }, 500);
})();