BV号⇆AV号

B站BV号与AV号互转脚本库

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/398808/785053/BV%E5%8F%B7%E2%87%86AV%E5%8F%B7.js

  1. // @name BV号⇆AV号
  2. // @description B站BV号与AV号互转脚本库
  3. // @language zh-CN
  4. const table = [...'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF'];
  5. const s = [11, 10, 3, 8, 4, 6];
  6. const xor = 177451812;
  7. const add = 8728348608;
  8. const bv2av = bv=>{
  9. let str = '';
  10. if (bv.length === 12) {
  11. str = bv;
  12. } else if (bv.length === 10) {
  13. str = `BV${bv}`;
  14. // 根据官方 API,BV 号开头的 BV1 其实可以省略
  15. // 不过单独省略个 B 又不行(
  16. } else if (bv.length === 9) {
  17. str = `BV1${bv}`;
  18. } else {
  19. return '¿你在想桃子?';
  20. };
  21. if (!str.match(/[Bb][Vv][fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF]{10}/gu)) {
  22. return '¿你在想桃子?';
  23. };
  24.  
  25. let result = 0;
  26. let i = 0;
  27. while (i < 6) {
  28. result += table.indexOf(str[s[i]]) * 58 ** i;
  29. i += 1;
  30. };
  31. return `av${result - add ^ xor}`;
  32. };
  33. const av2bv = av=>{
  34. let num = NaN;
  35. if (Object.prototype.toString.call(av) === '[object Number]') {
  36. num = av;
  37. } else if (Object.prototype.toString.call(av) === '[object String]') {
  38. num = parseInt(av.replace(/[^0-9]/gu, ''));
  39. };
  40. if (isNaN(num) || num <= 0) {
  41. // 网页版直接输出这个结果了
  42. return '¿你在想桃子?';
  43. };
  44.  
  45. num = (num ^ xor) + add;
  46. let result = [...'bv1 4 1 7 '];
  47. let i = 0;
  48. while (i < 6) {
  49. result[s[i]] = table[Math.floor(num / 58 ** i) % 58];
  50. i += 1;
  51. };
  52. return result.join('');
  53. };