Textarea Typograf

Replaces hyphens and quotation marks. Works only in the <textarea>. If you select a part of the text, only that part will be processed.

当前为 2020-09-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Textarea Typograf
  3. // @namespace https://github.com/glebkema/tampermonkey-textarea-typograf
  4. // @description Replaces hyphens and quotation marks. Works only in the <textarea>. If you select a part of the text, only that part will be processed.
  5. // @author glebkema
  6. // @copyright 2020, glebkema (https://github.com/glebkema)
  7. // @license MIT
  8. // @version 0.3.4
  9. // @match http://*/*
  10. // @match https://*/*
  11. // @grant none
  12. // @run-at context-menu
  13. // ==/UserScript==
  14.  
  15. // ==OpenUserJS==
  16. // @author glebkema
  17. // ==/OpenUserJS==
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. var element = document.activeElement;
  23. if (element && 'textarea' == element.tagName.toLowerCase() && element.value) {
  24. var start = element.selectionStart;
  25. var end = element.selectionEnd;
  26. if (start == end) {
  27. element.value = typograf(element.value);
  28. } else {
  29. var selected = element.value.substring(start, end);
  30. var length = element.value.length;
  31. element.value = element.value.substring(0, start) + typograf(selected) + element.value.substring(end, length);
  32. }
  33. } else {
  34. // console.info('Start editing a non-empty textarea before calling the script');
  35. }
  36.  
  37. // console.log(typograf('Еще она - не очень еще "еще" любила (Еще и еще). Еще вот еще, еще. И еще'));
  38. // console.log(typograf('Нее ее. Длиннее еен нее.'));
  39.  
  40. function typograf(text) {
  41. if (text) {
  42. // dash
  43. text = text.replace(/ - /gi, ' — ');
  44.  
  45. // quotes
  46. text = text.replace(/^"/gi, '«');
  47. text = text.replace(/"$/gi, '»');
  48. text = text.replace(/([\(\s])"/gi, '$1«');
  49. text = text.replace(/"([.,;\s\)])/gi, '»$1');
  50.  
  51. // words with a capital letter and yo
  52. text = checkWords(text, 'Ещё,Её,Моё,Неё,Своё,Твоё');
  53. text = checkWords(text, 'Объём,Остриём,Приём,Причём,Огнём,Своём,Твоём');
  54. text = checkWords(text, 'Василёк,Мотылёк,Огонёк,Пенёк,Ручеёк');
  55. text = checkWords(text, 'Затёк,Натёк,Потёк');
  56. text = checkWords(text, 'Грёза,Грёзы,Слёзы');
  57. }
  58. return text;
  59. }
  60.  
  61. function checkWords(text, words) {
  62. if ('string' === typeof words) {
  63. words = words.split(',');
  64. }
  65. for (var i = 0; i < words.length; i++) {
  66. let word = words[i].trim();
  67. if (word) {
  68. let find = word.replace('ё', 'е').replace('Ё', 'Е');
  69. text = replaceWords(text, find, word);
  70. }
  71. }
  72. return text;
  73. }
  74.  
  75. function replaceWords(text, find, replace) {
  76. // NB: \b doesn't work for russian words
  77. // 1) word starts with a capital letter
  78. var regex = new RegExp('(' + find + ')(?=[^а-яё]|$)', 'g');
  79. text = text.replace(regex, replace);
  80. // 2) word in lowercase
  81. regex = new RegExp('(?<=[^А-Яа-яЁё]|^)(' + find.toLowerCase() + ')(?=[^а-яё]|$)', 'g');
  82. return text.replace(regex, replace.toLowerCase());
  83. }
  84. })();