R4 Utils

R4 Utils Library

目前为 2023-12-19 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/482597/1298686/R4%20Utils.js

  1. // ==UserScript==
  2. // @name R4 Utils
  3. // @description R4 Utils Library
  4. // @version 1.0.0
  5. // ==/UserScript==
  6. function R4Utils() {
  7. function fromHTML(html, trim = true) {
  8. // Process the HTML string.
  9. html = trim ? html : html.trim();
  10. if (!html) return null;
  11. // Then set up a new template element.
  12. const template = document.createElement('template');
  13. template.innerHTML = html;
  14. const result = template.content.children;
  15. // Then return either an HTMLElement or HTMLCollection,
  16. // based on whether the input HTML had one or more roots.
  17. if (result.length === 1) return result[0];
  18. return result;
  19. }
  20.  
  21. function transliterate(word) {
  22. let answer = "";
  23. const a = {};
  24.  
  25. a["Ё"] = "YO";
  26. a["Й"] = "I";
  27. a["Ц"] = "TS";
  28. a["У"] = "U";
  29. a["К"] = "K";
  30. a["Е"] = "E";
  31. a["Н"] = "N";
  32. a["Г"] = "G";
  33. a["Ш"] = "SH";
  34. a["Щ"] = "SCH";
  35. a["З"] = "Z";
  36. a["Х"] = "H";
  37. a["Ъ"] = "'";
  38. a["ё"] = "yo";
  39. a["й"] = "i";
  40. a["ц"] = "ts";
  41. a["у"] = "u";
  42. a["к"] = "k";
  43. a["е"] = "e";
  44. a["н"] = "n";
  45. a["г"] = "g";
  46. a["ш"] = "sh";
  47. a["щ"] = "sch";
  48. a["з"] = "z";
  49. a["х"] = "h";
  50. a["ъ"] = "'";
  51. a["Ф"] = "F";
  52. a["Ы"] = "I";
  53. a["В"] = "V";
  54. a["А"] = "A";
  55. a["П"] = "P";
  56. a["Р"] = "R";
  57. a["О"] = "O";
  58. a["Л"] = "L";
  59. a["Д"] = "D";
  60. a["Ж"] = "ZH";
  61. a["Э"] = "E";
  62. a["ф"] = "f";
  63. a["ы"] = "i";
  64. a["в"] = "v";
  65. a["а"] = "a";
  66. a["п"] = "p";
  67. a["р"] = "r";
  68. a["о"] = "o";
  69. a["л"] = "l";
  70. a["д"] = "d";
  71. a["ж"] = "zh";
  72. a["э"] = "e";
  73. a["Я"] = "Ya";
  74. a["Ч"] = "CH";
  75. a["С"] = "S";
  76. a["М"] = "M";
  77. a["И"] = "I";
  78. a["Т"] = "T";
  79. a["Ь"] = "'";
  80. a["Б"] = "B";
  81. a["Ю"] = "YU";
  82. a["я"] = "ya";
  83. a["ч"] = "ch";
  84. a["с"] = "s";
  85. a["м"] = "m";
  86. a["и"] = "i";
  87. a["т"] = "t";
  88. a["ь"] = "'";
  89. a["б"] = "b";
  90. a["ю"] = "yu";
  91.  
  92. for (const i in word) {
  93. if (word.hasOwnProperty(i)) {
  94. answer += a[word[i]] === undefined ? word[i] : a[word[i]];
  95. }
  96. }
  97. return answer;
  98. }
  99.  
  100. function slugify(str) {
  101. return String(str)
  102. .normalize("NFKD") // split accented characters into their base characters and diacritical marks
  103. .replace(/[\u0300-\u036f]/g, "") // remove all the accents, which happen to be all in the \u03xx UNICODE block.
  104. .trim() // trim leading or trailing whitespace
  105. .toLowerCase() // convert to lowercase
  106. .replace(/[^a-z0-9 -]/g, "") // remove non-alphanumeric characters
  107. .replace(/\s+/g, "-") // replace spaces with hyphens
  108. .replace(/-+/g, "-"); // remove consecutive hyphens
  109. }
  110.  
  111. return {
  112. fromHTML,
  113. transliterate,
  114. slugify,
  115. };
  116. }

QingJ © 2025

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