Advertisement
InfraRaven

translation fr

Nov 29th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. var from_language = "en", to_language = "fr";
  2.  
  3. async function translate(sourceText, sourceLang, targetLang) {
  4. var url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
  5. + sourceLang + "&tl=" + targetLang + "&dt=t&q=" + encodeURI(sourceText);
  6. var result = JSON.parse(await (await fetch(url)).text());
  7. var translatedText = result[0][0][0];
  8. return translatedText;
  9. }
  10.  
  11. OWOP.elements.chatInput.parentNode.removeChild(OWOP.elements.chatInput);
  12. var newChat = OWOP.elements.chatInput = document.createElement("textarea");
  13. newChat.id = "chat-input"; newChat.draggable = false; newChat.type = "text"; newChat.maxLength = 512; newChat.placeholder = "Chat here";
  14. newChat.style.display = "initial"; newChat.style.height = "16px";
  15. document.getElementById("chat").appendChild(newChat);
  16. function scrollChatToBottom(callback) {
  17. var dontScrollIfNotTop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  18.  
  19. var shouldScroll = !dontScrollIfNotTop || OWOP.elements.chatMessages.scrollHeight - OWOP.elements.chatMessages.scrollTop === OWOP.elements.chatMessages.clientHeight;
  20. if (callback) callback(); // add all elements here
  21. if (shouldScroll) OWOP.elements.chatMessages.scrollTop = OWOP.elements.chatMessages.scrollHeight;
  22. }
  23. function openChat() {
  24. OWOP.elements.chat.className = "active selectable";
  25. OWOP.elements.devChat.className = "active selectable";
  26. OWOP.elements.chatMessages.className = "active";
  27. scrollChatToBottom();
  28. }
  29. function closeChat() {
  30. OWOP.elements.chat.className = "";
  31. OWOP.elements.devChat.className = "";
  32. OWOP.elements.chatMessages.className = "";
  33. OWOP.elements.chatInput.blur();
  34. scrollChatToBottom();
  35. }
  36. var chatHistory = [];
  37. var historyIndex = 0;
  38. newChat.addEventListener("keydown", async function (event) {
  39. event.stopPropagation();
  40. if (historyIndex === 0) {
  41. chatHistory[0] = newChat.value;
  42. }
  43. var keyCode = event.which || event.keyCode;
  44. switch (keyCode) {
  45. case 27:
  46. closeChat();
  47. break;
  48. case 13:
  49. if (!event.shiftKey) {
  50. event.preventDefault();
  51. var text = newChat.value;
  52. historyIndex = 0;
  53. chatHistory.unshift(text);
  54. /*if (misc.storageEnabled) {
  55. if (text.startsWith("/adminlogin ")) {
  56. misc.localStorage.adminlogin = text.slice(12);
  57. } else if (text.startsWith("/modlogin ")) {
  58. misc.localStorage.modlogin = text.slice(10);
  59. } else if (text.startsWith("/nick")) {
  60. var nick = text.slice(6);
  61. if (nick.length) {
  62. misc.localStorage.nick = nick;
  63. } else {
  64. delete misc.localStorage.nick;
  65. }
  66. } else if (text.startsWith("/pass ") && misc.world) {
  67. var pass = text.slice(6);
  68. misc.worldPasswords[_networking.net.protocol.worldName] = pass;
  69. saveWorldPasswords();
  70. }
  71. }*/
  72. if (!event.ctrlKey) {
  73. text = OWOP.chat.sendModifier(text);
  74. }
  75. if (event.altKey) {
  76. var oldText = text;
  77. text = await translate(text, from_language, to_language);
  78. if(window.aaaaa) console.debug("translation result: ", text, ", from: ", oldText);
  79. }
  80. OWOP.net.protocol.sendMessage(text);
  81. newChat.value = '';
  82. newChat.style.height = "16px";
  83. event.stopPropagation();
  84. }
  85. break;
  86. case 38:
  87. // Arrow up
  88. if (event.shiftKey && historyIndex < chatHistory.length - 1) {
  89. historyIndex++;
  90. newChat.value = chatHistory[historyIndex];
  91. newChat.style.height = 0;
  92. newChat.style.height = Math.min(newChat.scrollHeight - 8, 16 * 4) + "px";
  93. }
  94. break;
  95. case 40:
  96. // Arrow Down
  97. if (event.shiftKey && historyIndex > 0) {
  98. historyIndex--;
  99. newChat.value = chatHistory[historyIndex];
  100. newChat.style.height = 0;
  101. newChat.style.height = Math.min(newChat.scrollHeight - 8, 16 * 4) + "px";
  102. }
  103. break;
  104. }
  105. });
  106. newChat.addEventListener("keyup", function (event) {
  107. event.stopPropagation();
  108. var keyCode = event.which || event.keyCode;
  109. if (keyCode == 13 && !event.shiftKey) {
  110. closeChat();
  111. }
  112. });
  113. newChat.addEventListener("input", function (event) {
  114. newChat.style.height = 0;
  115. newChat.style.height = Math.min(newChat.scrollHeight - 8, 16 * 4) + "px";
  116. });
  117. newChat.addEventListener("focus", function (event) {
  118. if (!OWOP.mouse.buttons) {
  119. openChat();
  120. } else {
  121. newChat.blur();
  122. }
  123. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement