Lute: Word Count

Add word count functionality to Lute's New Book page

  1. // ==UserScript==
  2. // @name Lute: Word Count
  3. // @version 20240825
  4. // @description Add word count functionality to Lute's New Book page
  5. // @match http://localhost:500*/book/new
  6. // @grant none
  7. // @namespace https://greasyfork.org/users/242246
  8. // ==/UserScript==
  9.  
  10. (function() {
  11. 'use strict';
  12.  
  13. const button = document.createElement('button');
  14. button.textContent = 'Count Words';
  15. button.style.marginTop = '5px';
  16. button.style.padding = "0.1em 0.2em";
  17. button.style.fontSize = '0.8em';
  18. button.type = 'button';
  19.  
  20. const countDisplay = document.createElement('span');
  21. countDisplay.style.marginLeft = '5px';
  22. countDisplay.style.fontSize = '0.9em';
  23.  
  24. function countWords(e) {
  25. e.preventDefault();
  26. const text = document.getElementById('text').value;
  27. const wordCount = text.trim().split(/\s+/).filter(word => word.length > 0).length;
  28. countDisplay.textContent = `${wordCount}`;
  29. }
  30.  
  31. button.addEventListener('click', countWords);
  32.  
  33. const textarea = document.getElementById('text');
  34. textarea.parentNode.insertBefore(button, textarea.nextSibling);
  35. button.parentNode.insertBefore(countDisplay, button.nextSibling);
  36. })();