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