Bangumi-Index-ch

Bangumi 目录条目名中文化

  1. // ==UserScript==
  2. // @name Bangumi-Index-ch
  3. // @namespace https://github.com/bangumi/scripts/liaune
  4. // @author upsuper,Liaune
  5. // @description Bangumi 目录条目名中文化
  6. // @include /^https?:\/\/((bgm|bangumi)\.tv|chii\.in)\/index\/\d+/
  7. // @version 1.1
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.  
  13. var localStorage = window.localStorage,
  14. localPrefix = 'ChineseName_',
  15. // displayMode: 0 => Display Origin Name as subhead
  16. // : 1 => Show Origin Name as tip
  17. displayMode = 0;
  18.  
  19. function $(q) { return document.querySelector(q); }
  20. function $a(q) { return document.querySelectorAll(q); }
  21. function $c(t) { return document.createElement(t); }
  22. function $t(t) { return document.createTextNode(t); }
  23. String.prototype.u$format = function () {
  24. var args = arguments;
  25. return this.replace(/{(\d+)}/g, function (match, i) {
  26. return args[i] !== undefined ? args[i] : match;
  27. });
  28. };
  29.  
  30. function isSubjectPage(str) {
  31. return /\/subject\/\d+$/.test(str);
  32. }
  33. function parseChineseName(text) {
  34. var re = /<li><span class="tip">中文名: <\/span>(.+?)<\/li>/;
  35. var result = re.exec(text);
  36. return result ? result[1] : '';
  37. }
  38. function setChineseName(id, ch) {
  39. localStorage[localPrefix + id] = ch;
  40. var event = new CustomEvent('_uChinese', {
  41. detail: {subject_id: id, chinese: ch}
  42. });
  43. window.dispatchEvent(event);
  44. }
  45. if (isSubjectPage(location)) {
  46. var id = location.href.split('/').pop();
  47. setChineseName(id, parseChineseName(document.body.innerHTML));
  48. return;
  49. }
  50.  
  51. var titles;
  52. function displayChineseName($anchors, callback) {
  53. if (!titles) titles = {};
  54. function triggerDisplayCallback($anchor, ch) {
  55. setTimeout(function () { $anchor.u$displayCh(ch); });
  56. }
  57. function displayAll(id, ch) {
  58. var $anchors = titles[id];
  59. delete titles[id];
  60. setChineseName(id, ch);
  61. for (var i = 0; i < $anchors.length; i++)
  62. triggerDisplayCallback($anchors[i], ch);
  63. }
  64. function queryChineseName(id, url) {
  65. var xhr = new XMLHttpRequest();
  66. xhr.onreadystatechange = function () {
  67. if (xhr.readyState == 4 && xhr.status == 200)
  68. displayAll(id, parseChineseName(xhr.responseText));
  69. };
  70. xhr.open('GET', url, true);
  71. xhr.send(null);
  72. }
  73. for (var i = 0; i < $anchors.length; i++) {
  74. var $anchor = $anchors[i];
  75. if (!isSubjectPage($anchor.href))
  76. continue;
  77. var id = $anchor.href.split('/').pop();
  78. var ch = localStorage[localPrefix + id];
  79. $anchor.u$displayCh = callback;
  80. if (ch !== undefined) {
  81. triggerDisplayCallback($anchor, ch);
  82. } else {
  83. if (!titles[id]) {
  84. titles[id] = [];
  85. queryChineseName(id, $anchor.href);
  86. }
  87. titles[id].push($anchor);
  88. }
  89. }
  90. }
  91.  
  92. function updateIndex() {
  93. var $anchors = $a('#browserItemList>li>div>h3>a');
  94. displayChineseName($anchors, function (ch) {
  95. if (!ch) return;
  96. if (displayMode) {
  97. this.innerHTML = ch;
  98. this.title = this.textContent;
  99. } else {
  100. let subhead = document.createElement('small');
  101. subhead.className='grey';
  102. subhead.innerHTML = this.textContent;
  103. this.parentNode.appendChild(subhead);
  104. this.innerHTML = ch;
  105.  
  106. }
  107. });
  108. }
  109. updateIndex();
  110.  
  111.  
  112. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址