Word Counter

Count repetitive words on a web page and display them with a custom threshold.

  1. // ==UserScript==
  2. // @name Word Counter
  3. // @version 1.4
  4. // @namespace WordCounterScript
  5. // @description Count repetitive words on a web page and display them with a custom threshold.
  6. // @license MIT
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Create and style the toggle button
  15. var toggleButton = document.createElement('button');
  16. toggleButton.id = 'word-counter-toggle-button';
  17. toggleButton.style.position = 'fixed';
  18. toggleButton.style.top = '50%';
  19. toggleButton.style.right = '20px';
  20. toggleButton.style.transform = 'translateY(-50%)';
  21. toggleButton.style.width = '50px';
  22. toggleButton.style.height = '50px';
  23. toggleButton.style.backgroundColor = 'blue';
  24. toggleButton.style.color = 'white';
  25. toggleButton.style.fontSize = '18px';
  26. toggleButton.style.border = 'none';
  27. toggleButton.innerText = 'WC';
  28. toggleButton.setAttribute('aria-label', 'Toggle Word Counter');
  29. document.body.appendChild(toggleButton);
  30.  
  31. // Add event listener to toggle button
  32. toggleButton.addEventListener('click', function() {
  33. toggleWordCounter();
  34. });
  35.  
  36. // Function to toggle word counter visibility
  37. function toggleWordCounter() {
  38. var wordCounter = document.getElementById('word-counter');
  39. if (wordCounter.style.display === 'none') {
  40. wordCounter.style.display = 'block';
  41. countWords();
  42. } else {
  43. wordCounter.style.display = 'none';
  44. }
  45. }
  46.  
  47. // Function to count repetitive words
  48. function countWords() {
  49. let ignoredWords = ["a", "an", "the", "and", "but", "or", "for", "nor", "so", "yet", "after", "as", "at", "by", "for", "from", "in", "of", "on", "over", "to", "with", "about", "above", "below", "beneath", "under", "before", "during", "since", "within", "without", "throughout", "among", "beside", "between", "behind", "besides", "against", "along", "around", "except", "according", "across", "near", "after", "against", "ahead", "ago", "upon", "through", "into", "onto", "until", "underneath", "up", "off", "out", "of", "down", "behind", "near", "around", "between", "above", "below", "within", "without", "upon", "before", "after", "by", "behind", "near", "against", "during", "from", "for", "with", "within", "without", "on", "over", "under", "around", "throughout", "through", "at", "about", "above", "before", "behind", "beneath", "between", "beside", "beyond", "by", "down", "from", "in", "into", "near", "on", "off", "over", "under", "up", "upon", "through", "to", "with", "within", "without", "with", "via", "per", "about", "above", "across", "after", "against", "ahead", "along", "amid", "amidst", "among", "amongst", "around", "as", "at", "before", "behind", "below", "beneath", "beside", "besides", "between", "beyond", "but", "by", "despite", "down", "during", "except", "for", "from", "in", "inside", "into", "like", "near", "of", "off", "on", "onto", "out", "outside", "over", "past", "round", "since", "through", "throughout", "till", "to", "toward", "under", "underneath", "until", "unto", "up", "upon", "with", "within", "without", "yet", "a", "an", "the", "some", "any", "each", "every", "all", "both", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "last"];
  50.  
  51. // Get all words on the page
  52. let words = document.body.textContent.split(/\s+/);
  53.  
  54. // Count the occurrences of each word
  55. let wordCount = {};
  56. words.forEach(word => {
  57. let cleanedWord = word.toLowerCase().replace(/[^a-zA-Z0-9]/g, '');
  58. if (cleanedWord && !ignoredWords.includes(cleanedWord)) {
  59. if (wordCount[cleanedWord]) {
  60. wordCount[cleanedWord]++;
  61. } else {
  62. wordCount[cleanedWord] = 1;
  63. }
  64. }
  65. });
  66.  
  67. // Filter words with counts higher than X
  68. let threshold = parseInt(prompt("Enter the threshold count:"));
  69. let repetitiveWords = Object.entries(wordCount).filter(([word, count]) => count > threshold);
  70.  
  71. // Display the repetitive words and their counts
  72. let resultDiv = document.getElementById('word-counter');
  73. resultDiv.innerHTML = `<h3>Repeated Words (Threshold: ${threshold})</h3>`;
  74. if (repetitiveWords.length === 0) {
  75. resultDiv.innerHTML += '<p>No repetitive words found.</p>';
  76. } else {
  77. repetitiveWords.forEach(([word, count]) => {
  78. resultDiv.innerHTML += `<p>${word}: ${count} occurrences</p>`;
  79. });
  80. }
  81. }
  82.  
  83. // Create the result dialog box
  84. var wordCounterDiv = document.createElement('div');
  85. wordCounterDiv.id = 'word-counter';
  86. wordCounterDiv.style.display = 'none';
  87. wordCounterDiv.style.position = 'fixed';
  88. wordCounterDiv.style.top = '50%';
  89. wordCounterDiv.style.left = '50%';
  90. wordCounterDiv.style.transform = 'translate(-50%, -50%)';
  91. wordCounterDiv.style.padding = '20px';
  92. wordCounterDiv.style.backgroundColor = '#fff';
  93. wordCounterDiv.style.border = '2px solid #333';
  94. wordCounterDiv.style.borderRadius = '5px';
  95. wordCounterDiv.style.zIndex = '9999';
  96. document.body.appendChild(wordCounterDiv);
  97.  
  98. // Create and style the close button for the result dialog
  99. var closeButton = document.createElement('button');
  100. closeButton.innerHTML = '✖';
  101. closeButton.style.position = 'absolute';
  102. closeButton.style.top = '10px';
  103. closeButton.style.right = '10px';
  104. closeButton.style.border = 'none';
  105. closeButton.style.background = 'none';
  106. closeButton.style.fontSize = '18px';
  107. closeButton.style.cursor = 'pointer';
  108. closeButton.setAttribute('aria-label', 'Close Word Counter');
  109. wordCounterDiv.appendChild(closeButton);
  110.  
  111. // Add event listener to close button
  112. closeButton.addEventListener('click', function() {
  113. wordCounterDiv.style.display = 'none';
  114. });
  115. })();