Bangumi-Index-ch

Bangumi 目录条目名中文化

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Bangumi-Index-ch
// @namespace    https://github.com/bangumi/scripts/liaune
// @author       upsuper,Liaune
// @description  Bangumi 目录条目名中文化
// @include     /^https?:\/\/((bgm|bangumi)\.tv|chii\.in)\/index\/\d+/
// @version     1.1
// @grant       none
// ==/UserScript==

(function() {

  var localStorage = window.localStorage,
  localPrefix = 'ChineseName_',
  // displayMode: 0 => Display Origin Name as subhead
  //            : 1 => Show Origin Name as tip
  displayMode = 0;

  function $(q) { return document.querySelector(q); }
  function $a(q) { return document.querySelectorAll(q); }
  function $c(t) { return document.createElement(t); }
  function $t(t) { return document.createTextNode(t); }
  String.prototype.u$format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (match, i) {
      return args[i] !== undefined ? args[i] : match;
    });
  };

  function isSubjectPage(str) {
    return /\/subject\/\d+$/.test(str);
  }
  function parseChineseName(text) {
    var re = /<li><span class="tip">中文名: <\/span>(.+?)<\/li>/;
    var result = re.exec(text);
    return result ? result[1] : '';
  }
  function setChineseName(id, ch) {
    localStorage[localPrefix + id] = ch;
    var event = new CustomEvent('_uChinese', {
      detail: {subject_id: id, chinese: ch}
    });
    window.dispatchEvent(event);
  }
  if (isSubjectPage(location)) {
    var id = location.href.split('/').pop();
    setChineseName(id, parseChineseName(document.body.innerHTML));
    return;
  }

  var titles;
  function displayChineseName($anchors, callback) {
    if (!titles) titles = {};
    function triggerDisplayCallback($anchor, ch) {
      setTimeout(function () { $anchor.u$displayCh(ch); });
    }
    function displayAll(id, ch) {
      var $anchors = titles[id];
      delete titles[id];
      setChineseName(id, ch);
      for (var i = 0; i < $anchors.length; i++)
      triggerDisplayCallback($anchors[i], ch);
    }
    function queryChineseName(id, url) {
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200)
          displayAll(id, parseChineseName(xhr.responseText));
      };
      xhr.open('GET', url, true);
      xhr.send(null);
    }
    for (var i = 0; i < $anchors.length; i++) {
      var $anchor = $anchors[i];
      if (!isSubjectPage($anchor.href))
        continue;
      var id = $anchor.href.split('/').pop();
      var ch = localStorage[localPrefix + id];
      $anchor.u$displayCh = callback;
      if (ch !== undefined) {
        triggerDisplayCallback($anchor, ch);
      } else {
        if (!titles[id]) {
          titles[id] = [];
          queryChineseName(id, $anchor.href);
        }
        titles[id].push($anchor);
      }
    }
  }

  function updateIndex() {
   var $anchors = $a('#browserItemList>li>div>h3>a');
    displayChineseName($anchors, function (ch) {
      if (!ch) return;
      if (displayMode) {
        this.innerHTML = ch;
        this.title = this.textContent;
      } else {
        let subhead = document.createElement('small');
        subhead.className='grey';
        subhead.innerHTML = this.textContent;
        this.parentNode.appendChild(subhead);
        this.innerHTML = ch;

      }
    });
  }
 updateIndex();


})();