🏠 Home 

Bangumi-Episode-Chinese

Show Chinese episode name in episode page.


Install this script?
  1. // ==UserScript==
  2. // @name Bangumi-Episode-Chinese
  3. // @namespace org.binota.scripts.bangumi.bec
  4. // @description Show Chinese episode name in episode page.
  5. // @include /^https?:\/\/(bgm\.tv|bangumi\.tv|chii\.in)\/ep\/\d+/
  6. // @version 0.1.1
  7. // @grant GM_xmlhttpRequest
  8. // ==/UserScript==
  9. 'use strict';
  10. const STORAGE_PREFIX = `binota_bec_`;
  11. var $ = function(query) {
  12. return document.querySelector(query);
  13. };
  14. var $a = function(query) {
  15. return document.querySelectorAll(query);
  16. };
  17. var subject = $('h1.nameSingle a').href.match(/\/subject\/(\d+)/)[1];
  18. var episode = window.location.href.match(/\/ep\/(\d+)/)[1];
  19. var storage = new (function(driver) {
  20. this._storage = driver;
  21. this.set = function(key, value) {
  22. this._storage.setItem(`${STORAGE_PREFIX}${key}`, value);
  23. return value;
  24. };
  25. this.get = function(key) {
  26. return this._storage.getItem(`${STORAGE_PREFIX}${key}`);
  27. };
  28. this.remove = function(key) {
  29. this._storage.removeItem(`${STORAGE_PREFIX}${key}`);
  30. return key;
  31. };
  32. })(localStorage);
  33. var episodes = new (function(storage, id) {
  34. var subject = (JSON.parse(storage.get(id)) || {});
  35. this.getTitle = function(episode) {
  36. return subject[episode] || '';
  37. };
  38. this.setTitle = function(episode, title) {
  39. return subject[episode] = title.trim();
  40. };
  41. this.save = function() {
  42. return storage.set(id, JSON.stringify(subject));
  43. };
  44. })(storage, subject);
  45. var writeTitle = function() {
  46. var title = episodes.getTitle(episode);
  47. $('h2.title').innerHTML += ` <small><a class="l" onclick="(function(){localStorage.removeItem('${STORAGE_PREFIX}${subject}');window.location.reload();})()" href="#">[刷新中文名缓存]</small>`;
  48. if(title !== '') {
  49. $('h2.title').innerHTML = $('h2.title').innerHTML.replace('<small', ` / ${title} <small`);
  50. document.title = document.title.replace(/ \/ /, ` | ${title} / `);
  51. }
  52. }
  53. var writeEpisodeList = function() {
  54. //Write title of episode list
  55. var list = $a('.sideEpList li a');
  56. for(let i = 0; i < list.length; i++) {
  57. let liId = (list[i].href.match(/ep\/(\d+)/) || [-1, -1])[1];
  58. let liTitle = episodes.getTitle(liId);
  59. if(liTitle !== '') list[i].innerHTML += ' / ' + liTitle;
  60. }
  61. }
  62. //check cache:
  63. if(storage.get(subject)) {
  64. writeTitle();
  65. writeEpisodeList();
  66. } else {
  67. //Query API
  68. GM_xmlhttpRequest({
  69. method: 'GET',
  70. url: `http://api.bgm.tv/subject/${subject}?responseGroup=large`,
  71. onload: function(response) {
  72. var data = JSON.parse(response.response);
  73. //write cache
  74. for(let ep of data.eps) {
  75. if(ep.id == episode) {
  76. writeTitle();
  77. }
  78. episodes.setTitle(ep.id, ep.name_cn);
  79. }
  80. episodes.save();
  81. writeEpisodeList();
  82. }
  83. });
  84. }