Content Time Estimator

Estimate reading time for content.

  1. // ==UserScript==
  2. // @name Content Time Estimator
  3. // @namespace http://yournamespace.com/
  4. // @version 0.2
  5. // @description Estimate reading time for content.
  6. // @author icycoldveins
  7. // @include *://*/*
  8. // @grant GM_addStyle
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Style for the reading time box
  16. GM_addStyle(`
  17. #reading-time-box {
  18. position: fixed;
  19. bottom: 10px;
  20. right: 10px;
  21. padding: 5px 10px;
  22. background-color: #000;
  23. color: #FFF;
  24. border-radius: 5px;
  25. font-size: 14px;
  26. z-index: 10000;
  27. display: none;
  28. }
  29. `);
  30.  
  31. const estimateReadingTime = () => {
  32. // Grab all the paragraphs on the page
  33. let paragraphs = document.querySelectorAll('p');
  34. let totalWords = 0;
  35.  
  36. paragraphs.forEach(paragraph => {
  37. totalWords += paragraph.innerText.split(' ').length;
  38. });
  39.  
  40. // Average reading speed is roughly 200-250 words per minute
  41. let readingTime = Math.ceil(totalWords / 225);
  42. return readingTime;
  43. };
  44.  
  45. const displayReadingTime = () => {
  46. let readingTime = estimateReadingTime();
  47.  
  48. if (readingTime > 1) { // Show the estimator only if reading time is more than a minute
  49. let timeBox = document.createElement('div');
  50. timeBox.id = 'reading-time-box';
  51. timeBox.innerHTML = `Estimated Reading Time: ${readingTime} mins`;
  52. document.body.appendChild(timeBox);
  53. timeBox.style.display = 'block';
  54. }
  55. };
  56.  
  57. window.addEventListener('load', displayReadingTime);
  58.  
  59. })();