SteamDB_CN

SteamDB汉化插件

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

  1. // ==UserScript==
  2. // @name SteamDB_CN
  3. // @name:zh-CN SteamDB汉化
  4. // @namespace https://blog.chrxw.com
  5. // @version 1.19
  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. document.querySelector("html").setAttribute("lang", "zh-CN");
  36.  
  37. let Locales;
  38.  
  39. if (DEBUG) {
  40. const template = `{"DOC":{"更新时间":"调试模式","贡献名单":["调试模式"]},\n"STATIC":\n{\n\n},\n"INPUT":\n{\n\n},\n"LABEL":\n{\n\n},\n"DYNAMIC":\n{\n\n}\n}`;
  41. const box = document.createElement("div");
  42. box.style.cssText = "display:flex;";
  43. const text = document.createElement("textarea");
  44. text.style.cssText = "width:90%;height:250px;resize:vertical;";
  45. box.appendChild(text);
  46. const action = document.createElement("div");
  47. action.style.cssText = "width:10%;height:100%";
  48. box.appendChild(action);
  49. const btnSave = document.createElement("button");
  50. btnSave.innerText = "保存并刷新";
  51. btnSave.addEventListener("click", () => {
  52. const raw = text.value.trim();
  53. if (!raw) {
  54. alert("翻译文本不能为空!!!");
  55. } else {
  56. try {
  57. JSON.parse(raw);
  58. window.localStorage["sdb_lang"] = raw;
  59. window.location.reload();
  60. } catch (e) {
  61. alert("翻译文本不是有效的JSON格式!!!");
  62. }
  63. }
  64. });
  65. btnSave.style.cssText = "width:100%;height:50px;margin-bottom:5px;";
  66. action.appendChild(btnSave);
  67. const btnReset = document.createElement("button");
  68. btnReset.textContent = "清空文本";
  69. btnReset.addEventListener("click", () => {
  70. window.localStorage["sdb_lang"] = template;
  71. window.location.reload();
  72. });
  73. btnReset.style.cssText = "width:100%;height:50px;margin-bottom:5px;";
  74. action.appendChild(btnReset);
  75. const btnOnline = document.createElement("button");
  76. btnOnline.textContent = "当前在线文本";
  77. btnOnline.addEventListener("click", () => {
  78. text.value = GM_getResourceText("data");
  79. });
  80. btnOnline.style.cssText = "width:100%;height:50px;margin-bottom:5px;";
  81. action.appendChild(btnOnline);
  82.  
  83. const father = document.getElementById("main");
  84. father.insertBefore(box, father.firstChild);
  85. const customLang = window.localStorage["sdb_lang"] ?? template;
  86. text.value = customLang;
  87. Locales = JSON.parse(customLang);
  88. } else {
  89. Locales = JSON.parse(GM_getResourceText("data"));
  90. }
  91.  
  92. //计时
  93. var Start = new Date().getTime();
  94.  
  95. {//静态元素
  96. for (const [css, dic] of Object.entries(Locales.STATIC)) {
  97. if (OUTPUT) { console.log(`〖${css}〗`); }
  98. const elements = document.querySelectorAll(css);
  99. if (elements.length > 0) {
  100. for (let i = 0; i < elements.length; i++) {
  101. const element = elements[i];
  102. if (element.childElementCount === 0) {//节点内部无其他元素
  103. const raw = element.innerText?.trim();
  104. if (!raw || raw.length <= 2) { continue; }
  105. const txt = dic[raw];
  106. if (txt) {
  107. element.innerText = txt;
  108. } else if (OUTPUT) {
  109. console.log(`"${raw}": "",`);
  110. }
  111. } else {//节点内部有其他元素
  112. const nodes = element.childNodes;
  113. for (let j = 0; j < nodes.length; j++) {
  114. const node = nodes[j];
  115. if (node.nodeType === Node.TEXT_NODE) {
  116. const raw = node.textContent?.trim();
  117. if (!raw || raw.length <= 2) { continue; }
  118. const txt = dic[raw];
  119. if (txt) {
  120. node.textContent = txt;
  121. } else if (OUTPUT) {
  122. console.log(`"${raw}": "",`);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. } else {
  129. if (OUTPUT) { console.warn(`CSS选择器未匹配到任何元素: ${css}`); }
  130. }
  131. }
  132. }
  133.  
  134. {//输入框
  135. const inputs = Locales.INPUT;
  136. if (OUTPUT) { console.log("〖输入框〗"); }
  137. const elements = document.querySelectorAll("input");
  138. for (let i = 0; i < elements.length; i++) {
  139. const element = elements[i];
  140. const raw = element.placeholder;
  141. if (!raw) { continue; }
  142. const txt = inputs[raw];
  143. if (txt) {
  144. element.placeholder = txt;
  145. } else if (OUTPUT) {
  146. console.log(`"${raw}": "",`);
  147. }
  148. }
  149. }
  150.  
  151. {//悬浮提示
  152. const labels = Locales.LABEL;
  153. if (OUTPUT) { console.log("〖提示文本〗"); }
  154. const elements = document.querySelectorAll("*[aria-label]");
  155. for (let i = 0; i < elements.length; i++) {
  156. const element = elements[i];
  157. const raw = element.getAttribute("aria-label");
  158. if (!raw) { continue; }
  159. const txt = labels[raw];
  160. if (txt) {
  161. element.setAttribute("aria-label", txt);
  162. } else if (OUTPUT) {
  163. console.log(`"${raw}": "",`);
  164. }
  165. }
  166. }
  167.  
  168. const { script: { version } } = GM_info;
  169. const { DOC: { "更新时间": update, "贡献名单": contribution } } = Locales;
  170.  
  171. // call your function
  172. var End = new Date().getTime();
  173. console.log("执行耗时", `${End - Start} ms`);
  174. console.log("=================================");
  175. console.log(`插件版本: ${version}`);
  176. console.log(`更新时间: ${update}`);
  177. console.log(`贡献名单: ${contribution.join(", ")}`);
  178. console.log("=================================");
  179. console.log("迷茫同学:\n『没有恶意 请问直接用谷歌翻译整个网页不香吗』")
  180.  
  181. GM_addStyle(".tabnav-tabs>a{min-width:80px;}");
  182. })();