Skip to content

Instantly share code, notes, and snippets.

@bluelinden
Last active June 7, 2023 18:11
Show Gist options
  • Select an option

  • Save bluelinden/15ed12b532ffc65b61f44c4df49da7af to your computer and use it in GitHub Desktop.

Select an option

Save bluelinden/15ed12b532ffc65b61f44c4df49da7af to your computer and use it in GitHub Desktop.
// copyright 2023 Blue Linden - CC-BY-NC-SA
// for use on SNOsites newspaper websites; it ain't guaranteed to work anywhere else.
(function showReadingTime() {
let storyMeta = document.querySelector('#storymeta p');
let readingTimeIndicator = document.createElement('span');
readingTimeIndicator.setAttribute('id','readingTimeIndicator');
storyMeta.appendChild(readingTimeIndicator);
let divider = document.createElement('span');
divider.setAttribute('class','byline-divider');
divider.innerText = '|'
let readTextWrapper = document.createElement('span');
readTextWrapper.setAttribute('id','rtwrap');
let readText = document.createElement('span')
readText.innerText = " minute read"
let readTimeNumber = document.createElement('span');
readTimeNumber.setAttribute('id','readTimeNumber');
var text = document.querySelector('.storycontent').innerText
const wpm = 225;
const words = text.trim().split(/\s+/).length;
const time = Math.ceil(words / wpm);
readTimeNumber.innerText = time;
readingTimeIndicator.appendChild(divider);
readingTimeIndicator.appendChild(readTextWrapper);
readTextWrapper.appendChild(readTimeNumber);
readTextWrapper.appendChild(readText);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment