Greasy Fork is available in English.

Telegram Translations - Fix auto scroll

When switching entry, a function that focus and select the text will also scroll the translation textbox under the page header and cause issues. This script fixes this problem while keep the "focus and select" part of that function.

  1. // ==UserScript==
  2. // @name Telegram Translations - Fix auto scroll
  3. // @description When switching entry, a function that focus and select the text will also scroll the translation textbox under the page header and cause issues. This script fixes this problem while keep the "focus and select" part of that function.
  4. // @namespace RainSlide
  5. // @author RainSlide
  6. // @license blessing
  7. // @version 1.0
  8. // @icon https://translations.telegram.org/img/website_icon.svg
  9. // @match https://translations.telegram.org/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. "use strict";
  14.  
  15. $.fn.focusAndSelectAll = function() {
  16.  
  17. // Reference: $.fn.focusAndSelect() in https://translations.telegram.org/js/jquery-ex.js
  18. var field = this.get(0), len = this.value().length;
  19. if (field.classList.contains("key-add-suggestion-field")) {
  20. this.focusField();
  21. if (len > 0) {
  22. if (this.is('[contenteditable]')) {
  23. var range = document.createRange(), sel;
  24. range.selectNodeContents(field);
  25. sel = window.getSelection();
  26. sel.removeAllRanges();
  27. sel.addRange(range);
  28. } else if (field.setSelectionRange) {
  29. field.setSelectionRange(0, len);
  30. }
  31. }
  32. // Skip the scrollIntoView() part for .key-add-suggestion-field
  33. } else {
  34. return this.focusAndSelect(true);
  35. }
  36.  
  37. };