SteamDB_CN

SteamDB汉化插件

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

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