Counter

Показывает количество символов и слов в текстовом поле

Ekde 2024/09/04. Vidu La ĝisdata versio.

// ==UserScript==
// @name         Counter
// @namespace    https://shikimori.one
// @version      1.7
// @description  Показывает количество символов и слов в текстовом поле
// @author       LifeH
// @match        *://shikimori.org/*
// @match        *://shikimori.one/*
// @match        *://shikimori.me/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  function addCounter(textarea) {
    if (textarea.next("#Counter").length === 0) {
      let counter = $(
        '<div id="Counter" style="position: absolute; right: 5px; bottom: 15px; font-size: 12px; color: gray;"></div>'
      );
      textarea.after(counter);

      textarea.on("input", function () {
        let text = window.chrome ? textarea[0].value.replace(/(\r\n|\n|\r)/g, "  ") : textarea[0].value;
        let charCount = text.length;
        let wordCount = textarea[0].value.trim().split(/\s+/).filter(Boolean).length;

        counter.text(`Символов: ${charCount} | Слов: ${wordCount}`);
      });

      textarea.trigger("input");
    }
  }

  function observeAreas() {
    let areas = [
      $('textarea[name="review[body]"]'),
      $('textarea[name="comment[body]"]'),
      $('textarea[name="critique[text]"]'),
      $('textarea[name="club[description]"]'), // добавил
      $('textarea[name="club_page[text]"]'),   // добавил
      $('textarea[name="message[body]"]')      // добавил
    ];

    areas.forEach(function (textarea) {
      if (textarea.length && textarea.next("#Counter").length === 0) {
        addCounter(textarea);
      }
    });
  }

  $(document).on("input", function () {
    observeAreas();
  });
})();