Greasy Fork

Greasy Fork is available in English.

bv2av

Change bv to av

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

(function () {
  // https://socialsisteryi.github.io/bilibili-API-collect/docs/misc/bvid_desc.html#bv-av%E7%AE%97%E6%B3%95

  const XOR_CODE = 23442827791579n;
  const MASK_CODE = 2251799813685247n;
  const BASE = 58n;

  const data = 'FcwAPNKTMug3GV5Lj7EJnHpWsx4tb8haYeviqBz6rkCy12mUSDQX9RdoZf';

  function dec(bvid) {
    const bvidArr = Array.from(bvid);
    [bvidArr[3], bvidArr[9]] = [bvidArr[9], bvidArr[3]];
    [bvidArr[4], bvidArr[7]] = [bvidArr[7], bvidArr[4]];
    bvidArr.splice(0, 3);
    const tmp = bvidArr.reduce(
      (pre, bvidChar) => pre * BASE + BigInt(data.indexOf(bvidChar)),
      0n,
    );
    // eslint-disable-next-line no-bitwise
    return `av${Number((tmp & MASK_CODE) ^ XOR_CODE)}`;
  }

  function bv2av(x) {
    if (!x.match('www.bilibili.com')) return x;
    if (x.includes('/watchlater')) 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;
  }

  setInterval(() => {
    const url = window.location.href;
    const newUrl = bv2av(url);
    if (url !== newUrl) {
      window.history.replaceState(null, null, newUrl);
    }
  }, 1000);

  setInterval(() => {
    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);

  if ('navigation' in window) {
    window.navigation.addEventListener('navigate', (e) => {
      const { url } = e.destination;
      const newUrl = bv2av(url);
      if (url !== newUrl) {
        window.history.replaceState(null, null, newUrl);
      }
    });
  }
}());