fanyi

翻译库

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

  1. // ==UserScript==
  2. // @name fanyi
  3. // @namespace fireloong
  4. // @version 0.0.16
  5. // @description 翻译库
  6. // @author Itsky71
  7. // @grant none
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. let doc_version = window.location.pathname.split('/')[2];
  12.  
  13. if(doc_version === 'current') {
  14. doc_version = '7.1';
  15. } else if(doc_version === '5.x') {
  16. doc_version = '5.4';
  17. }
  18.  
  19. function fanyi(translates, id, dev = false, delay = false){
  20. let doc;
  21. if(typeof id === 'number'){
  22. switch(id){
  23. case 1:
  24. doc = '.toctree a,.ui-heading > h1,.ui-prose > .section p,.ui-prose > .section li,.ui-prose > .section h2>a,.ui-prose > .section h3>a,.ui-prose > .section h4>a,.ui-prose > .section dl>dt,.ui-prose > .section dl > dd,.hljs-comment';
  25. break;
  26. case 2:
  27. doc = '.ui-heading > h1,.ui-prose .section > h2 > a,.ui-prose p,.pagination-item > a,.pagination-item > span,.ui-prose li';
  28. break;
  29. default:
  30. break;
  31. }
  32. } else {
  33. doc = id;
  34. }
  35. const myFunc = (bb) => {
  36. $(bb).html(translates[$(bb).text()]);
  37. };
  38.  
  39. let n = 0;
  40. let jsonStr = '';
  41. $(doc).each(function(i,v){
  42. if(translates.hasOwnProperty($(this).text())) {
  43. if(typeof delay === 'number'){
  44. setTimeout(myFunc, delay, this);
  45. } else {
  46. $(this).html(translates[$(this).text()]);
  47. }
  48. } else if(dev) {
  49. console.log(n,v,$(this).text());
  50. jsonStr += '\''+$(this).text().replaceAll('\\','\\\\').replaceAll('\'','\\\'').replaceAll("\n",'\\n')+'\': \'\','+"\n";
  51. n++;
  52. }
  53. });
  54. if(dev){
  55. console.info(jsonStr);
  56. }
  57. }
  58.  
  59. // 精准查找,并翻译
  60. function fanyi_jing(translates){
  61. $.each(translates, function(k,v){
  62. $(k).html(function(){
  63. //const regex = new RegExp(v[0], 'g');
  64. return $(this).html().replace(v[0], v[1]);
  65. });
  66. });
  67. }

QingJ © 2025

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