Skip to content

Instantly share code, notes, and snippets.

@conceptblend
Created March 5, 2017 20:15
Show Gist options
  • Select an option

  • Save conceptblend/feb76bab74bf93296d2a0482a23413eb to your computer and use it in GitHub Desktop.

Select an option

Save conceptblend/feb76bab74bf93296d2a0482a23413eb to your computer and use it in GitHub Desktop.
Javascript used in bookmarklet to measure estimated reading time on page.
/* Code used in bookmarklet to measure estimated reading time on page. */
/* Make a bookmarklet here: http://ted.mielczarek.org/code/mozilla/bookmarklet.html */
var contentNode = "",
textContent = "",
selector = 'body',
words = [],
estimatedReadTime = 0,
avgWPM = 275;
function formatTime(minutes) {
var mins = Math.floor(minutes),
secs = Math.round((minutes - mins) * 60);
return {
"rounded": Math.round(minutes).toString() + " minute" + (minutes > 1 ? "s" : ""),
"detail": mins.toString() + ":" + (secs < 10 ? "0" : "") + secs.toString()
};
}
selector = prompt("Enter CSS selector for article body:\n(Default: body)") || selector;
if (null !== selector) {
contentNode = document.querySelector(selector);
textContent = contentNode && contentNode.innerText;
if (contentNode && textContent) {
words = textContent.match(/[\w\d]+/gi);
estimatedReadTime = words.length / avgWPM;
var humanVersions = formatTime(estimatedReadTime),
message = "Estimated read time for "+words.length+" words in '"+selector+"':\n• About " + humanVersions.rounded + " ("+ humanVersions.detail +")";
alert(message);
console.log(message);
} else {
console.error("No content could be found using selector: '"+selector+"'");
}
} else {
console.error("No selector provided");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment