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.7
  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) {
  75. console.log(`〖${css}〗`);
  76. }
  77. const elements = document.querySelectorAll(css);
  78.  
  79. if (elements.length > 0) {
  80. for (let i = 0; i < elements.length; i++) {
  81. const element = elements[i];
  82. if (element.childElementCount === 0) {//节点内部无其他元素
  83. const raw = element.innerText;
  84. if (!raw || raw.length <= 2) { continue; }
  85. const txt = dic[raw];
  86. if (txt) {
  87. element.innerText = txt;
  88. } else if (OUTPUT) {
  89. console.log(`"${raw}": "",`);
  90. }
  91. } else {//节点内部有其他元素
  92. const nodes = element.childNodes;
  93. for (let j = 0; j < nodes.length; j++) {
  94. const node = nodes[j];
  95. if (node.nodeType === Node.TEXT_NODE) {
  96. const raw = node.textContent;
  97. if (!raw || raw.length <= 2) { continue; }
  98. const txt = dic[raw];
  99. if (txt) {
  100. node.textContent = txt;
  101. } else if (OUTPUT) {
  102. console.log(`"${raw}": "",`);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111.  
  112. {//输入框
  113. const inputs = Object.entries(Locales.INPUT);
  114. if (OUTPUT) {
  115. console.log('〖输入框〗');
  116. }
  117. const elements = document.querySelectorAll("input");
  118. if (elements.length > 0) {
  119. for (let i = 0; i < elements.length; i++) {
  120. const element = elements[i];
  121. const raw = element.placeholder;
  122. if (!raw) { continue; }
  123. const txt = inputs[raw];
  124. if (txt) {
  125. element.placeholder = txt;
  126. } else if (OUTPUT) {
  127. console.log(`"${raw}": "",`);
  128. }
  129. }
  130. }
  131. }
  132.  
  133.  
  134.  
  135. const { script: { version } } = GM_info;
  136. const { DOC: { "更新时间": update, "贡献名单": contribution } } = Locales;
  137.  
  138. // call your function
  139. var End = new Date().getTime();
  140. console.log('执行耗时', `${End - Start} ms`);
  141. console.log('=================================')
  142. console.log(`插件版本: ${version}`);
  143. console.log(`更新时间: ${update}`);
  144. console.log(`贡献名单: ${contribution.join(', ')}`);
  145. // // 创建一个观察器实例并传入回调函数
  146. // const observer = new MutationObserver((mutationsList, observer) => {
  147. // // Use traditional 'for loops' for IE 11
  148. // for (let mutation of mutationsList) {
  149. // console.log(mutation);
  150.  
  151. // }
  152.  
  153. // });
  154.  
  155. // // 以上述配置开始观察目标节点
  156. // observer.observe(document.body, { childList: true, subtree: true });
  157.  
  158. // // 之后,可停止观察
  159. // observer.disconnect();
  160.  
  161. })();