better_spell_novelai_dev

按alt+x 加 鼠标左键点击 ,对点击的文本进行unicode转换 ; 按alt+c 加 鼠标左键点击 ,对点击的文本进行json美化

  1. // ==UserScript==
  2. // @name better_spell_novelai_dev
  3. // @namespace http://leizingyiu.net/
  4. // @version 1.0
  5. // @description 按alt+x 加 鼠标左键点击 ,对点击的文本进行unicode转换 ; 按alt+c 加 鼠标左键点击 ,对点击的文本进行json美化
  6. // @author leizingyiu & Kimi
  7. // @match *://*/*
  8. // @grant none
  9. // @license GNU AGPLv3
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. // 定义功能函数
  16. const functions = {
  17. KeyX: unicodeToChar, // Unicode 转换
  18. KeyC: beautifyText, // JSON 美化
  19. };
  20.  
  21. // Unicode 转换函数
  22. function unicodeToChar(text) {
  23. return text.replace(/\\u([\dA-Fa-f]{4})/g, function (match, grp) {
  24. return String.fromCharCode(parseInt(grp, 16));
  25. });
  26. }
  27.  
  28. function _beautifyText(text) {
  29. try {
  30. const parsedJson = JSON.parse(text);
  31. return JSON.stringify(parsedJson, null, 2).replace(
  32. /(\[.*?\])/gs,
  33. (match) => {
  34. // 检查是否是最内层的数组(没有嵌套的数组)
  35. if (!match.includes("[") || !match.includes("]")) {
  36. return match.replace(/\n\s+/g, ""); // 去掉换行和缩进
  37. }
  38. return match; // 保留嵌套数组的格式
  39. },
  40. );
  41. } catch (error) {
  42. console.warn("输入的文本不是有效的 JSON,无法进行格式化。", error);
  43. return text;
  44. }
  45. }
  46.  
  47. function beautifyText(text) {
  48. try {
  49. const parsedJson = JSON.parse(text);
  50.  
  51. // 自定义格式化函数
  52. function customStringify(value, indent = 0) {
  53. const space = " ".repeat(indent); // 当前缩进
  54. const nextIndent = indent + 2; // 下一层缩进
  55.  
  56. if (Array.isArray(value)) {
  57. // 如果数组内部没有嵌套的 [] 或 {}
  58. if (value.every((item) => !/[\[\]\{\}]/.test(JSON.stringify(item)))) {
  59. return `[${value.join(", ")}]`; // 不换行
  60. } else {
  61. // 有嵌套结构,正常换行
  62. return `[${value.map((item) => `\n${" ".repeat(nextIndent)}${customStringify(item, nextIndent)}`).join(",\n")}\n${space}]`;
  63. }
  64. } else if (value && typeof value === "object") {
  65. // 如果对象内部没有嵌套的 [] 或 {}
  66. if (
  67. Object.values(value).every(
  68. (val) => !/[\[\]\{\}]/.test(JSON.stringify(val)),
  69. )
  70. ) {
  71. return `{${Object.entries(value)
  72. .map(([key, val]) => `"${key}": ${customStringify(val, indent)}`)
  73. .join(", ")}}`; // 不换行
  74. } else {
  75. // 有嵌套结构,正常换行
  76. return `{${Object.entries(value)
  77. .map(
  78. ([key, val]) =>
  79. `\n${" ".repeat(nextIndent)}"${key}": ${customStringify(val, nextIndent)}`,
  80. )
  81. .join(",\n")}\n${space}}`;
  82. }
  83. } else {
  84. // 基本类型直接返回
  85. return JSON.stringify(value);
  86. }
  87. }
  88.  
  89. return customStringify(parsedJson);
  90. } catch (error) {
  91. console.warn("输入的文本不是有效的 JSON,无法进行格式化。", error);
  92. return text;
  93. }
  94. }
  95.  
  96. // 通用处理函数
  97. function processNodeContent(event, transformFunction) {
  98. const targetNode = event.target;
  99. if (targetNode.nodeType === Node.ELEMENT_NODE && targetNode.textContent) {
  100. const originalText = targetNode.textContent;
  101. const transformedText = transformFunction(originalText);
  102. targetNode.textContent = transformedText;
  103. console.log("内容已处理:", transformFunction.name);
  104. }
  105. }
  106.  
  107. // 当前按下的键
  108. let activeKey = null;
  109.  
  110. // 监听键盘按下事件
  111. document.addEventListener("keydown", function (event) {
  112. if (event.altKey && functions[event.code]) {
  113. activeKey = event.code;
  114. console.log(
  115. `Alt + ${String.fromCharCode(event.keyCode)} 已按下,绑定点击事件`,
  116. );
  117. document.addEventListener("click", (e) =>
  118. processNodeContent(e, functions[activeKey]),
  119. );
  120. }
  121. });
  122.  
  123. // 监听键盘松开事件
  124. document.addEventListener("keyup", function (event) {
  125. if (event.altKey || activeKey) {
  126. console.log(
  127. `Alt + ${String.fromCharCode(event.keyCode)} 已松开,解绑点击事件`,
  128. );
  129. document.removeEventListener("click", (e) =>
  130. processNodeContent(e, functions[activeKey]),
  131. );
  132. activeKey = null;
  133. }
  134. });
  135. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址