SteamDB_CN

SteamDB汉化插件

当前为 2021-12-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name SteamDB_CN
  3. // @name:zh-CN SteamDB汉化
  4. // @namespace https://blog.chrxw.com
  5. // @version 1.10
  6. // @description SteamDB汉化插件
  7. // @description:zh-cn SteamDB汉化插件
  8. // @author Chr_
  9. // @match https://steamdb.info/*
  10. // @supportURL https://steamcn.com/t339531-1-1
  11. // @license AGPL-3.0
  12. // @icon https://blog.chrxw.com/favicon.ico
  13. // @resource data https://gitee.com/chr_a1/gm_scripts/raw/master/SteamDB/lang_zh_CN.json
  14. // @grant GM_getResourceText
  15. // @grant GM_registerMenuCommand
  16. // ==/UserScript==
  17.  
  18.  
  19. (function () {
  20. 'use strict';
  21. const DEBUG = window.localStorage['dbg_mode'] == '开';
  22. const OUTPUT = window.localStorage['out_word'] == '开';
  23.  
  24. GM_registerMenuCommand(`调试汉化文本:【${DEBUG ? '开' : '关'}】`, () => {
  25. window.localStorage['dbg_mode'] = DEBUG ? '关' : '开';
  26. window.location.reload();
  27. });
  28.  
  29. GM_registerMenuCommand(`在控制台输出未匹配文本:【${OUTPUT ? '开' : '关'}】`, () => {
  30. window.localStorage['out_word'] = OUTPUT ? '关' : '开';
  31. window.location.reload();
  32. });
  33.  
  34. let Locales;
  35.  
  36. if (DEBUG) {
  37. const template = '{"DOC":{"更新时间":"调试模式","贡献名单":["调试模式"]},\n"STATIC":\n{\n\n},\n"INPUT":\n{\n\n},\n"DYNAMIC":\n{\n\n}\n}';
  38. const box = document.createElement('div');
  39. box.style.cssText = 'display:flex;';
  40. const text = document.createElement('textarea');
  41. text.style.cssText = 'width:90%;height:250px;resize:vertical;';
  42. box.appendChild(text);
  43. const action = document.createElement('div');
  44. action.style.cssText = 'width:10%;height:100%';
  45. box.appendChild(action);
  46. const btnSave = document.createElement('button');
  47. btnSave.innerText = '保存并刷新';
  48. btnSave.addEventListener('click', () => {
  49. const raw = text.value.trim();
  50. if (!raw) {
  51. alert('翻译文本不能为空!!!');
  52. } else {
  53. try {
  54. JSON.parse(raw);
  55. window.localStorage['sdb_lang'] = raw;
  56. window.location.reload();
  57. } catch (e) {
  58. alert('翻译文本不是有效的JSON格式!!!');
  59. }
  60. }
  61. });
  62. btnSave.style.cssText = 'width:100%;height:50px;';
  63. action.appendChild(btnSave);
  64. const btnReset = document.createElement('button');
  65. btnReset.textContent = '清空文本';
  66. btnReset.addEventListener('click', () => {
  67. window.localStorage['sdb_lang'] = template;
  68. window.location.reload();
  69. });
  70. btnReset.style.cssText = 'width:100%;height:50px;';
  71. action.appendChild(btnReset);
  72.  
  73. const father = document.getElementById('main');
  74. father.insertBefore(box, father.firstChild);
  75. const customLang = window.localStorage['sdb_lang'] ?? template;
  76. text.value = customLang;
  77. Locales = JSON.parse(customLang);
  78. } else {
  79. Locales = JSON.parse(GM_getResourceText("data"));
  80. }
  81.  
  82. //计时
  83. var Start = new Date().getTime();
  84.  
  85. {//静态元素
  86. for (const [css, dic] of Object.entries(Locales.STATIC)) {
  87. if (OUTPUT) { console.log(`〖${css}〗`); }
  88. const elements = document.querySelectorAll(css);
  89. if (elements.length > 0) {
  90. for (let i = 0; i < elements.length; i++) {
  91. const element = elements[i];
  92. if (element.childElementCount === 0) {//节点内部无其他元素
  93. const raw = element.innerText.trim();
  94. if (!raw || raw.length <= 2) { continue; }
  95. const txt = dic[raw];
  96. if (txt) {
  97. element.innerText = txt;
  98. } else if (OUTPUT) {
  99. console.log(`"${raw}": "",`);
  100. }
  101. } else {//节点内部有其他元素
  102. const nodes = element.childNodes;
  103. for (let j = 0; j < nodes.length; j++) {
  104. const node = nodes[j];
  105. if (node.nodeType === Node.TEXT_NODE) {
  106. const raw = node.textContent;
  107. if (!raw || raw.length <= 2) { continue; }
  108. const txt = dic[raw];
  109. if (txt) {
  110. node.textContent = txt;
  111. } else if (OUTPUT) {
  112. console.log(`"${raw}": "",`);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. } else {
  119. if (OUTPUT) { console.warn(`CSS选择器未匹配到任何元素: ${css}`); }
  120. }
  121. }
  122. }
  123.  
  124. {//输入框
  125. const inputs = Locales.INPUT;
  126. if (OUTPUT) { console.log('〖输入框〗'); }
  127. const elements = document.querySelectorAll("input");
  128. for (let i = 0; i < elements.length; i++) {
  129. const element = elements[i];
  130. const raw = element.placeholder;
  131. if (!raw) { continue; }
  132. const txt = inputs[raw];
  133. if (txt) {
  134. element.placeholder = txt;
  135. } else if (OUTPUT) {
  136. console.log(`"${raw}": "",`);
  137. }
  138. }
  139. }
  140.  
  141.  
  142.  
  143. const { script: { version } } = GM_info;
  144. const { DOC: { "更新时间": update, "贡献名单": contribution } } = Locales;
  145.  
  146. // call your function
  147. var End = new Date().getTime();
  148. console.log('执行耗时', `${End - Start} ms`);
  149. console.log('=================================')
  150. console.log(`插件版本: ${version}`);
  151. console.log(`更新时间: ${update}`);
  152. console.log(`贡献名单: ${contribution.join(', ')}`);
  153. // // 创建一个观察器实例并传入回调函数
  154. // const observer = new MutationObserver((mutationsList, observer) => {
  155. // // Use traditional 'for loops' for IE 11
  156. // for (let mutation of mutationsList) {
  157. // console.log(mutation);
  158.  
  159. // }
  160.  
  161. // });
  162.  
  163. // // 以上述配置开始观察目标节点
  164. // observer.observe(document.body, { childList: true, subtree: true });
  165.  
  166. // // 之后,可停止观察
  167. // observer.disconnect();
  168.  
  169. })();