Browse Source

Add readtime.js widget

This script displays a "read time" on WriteFreely post pages. It appends
the time next to the date on the post.
main
Matt Baer 3 years ago
commit
46980fd8bb
1 changed files with 10 additions and 0 deletions
  1. +10
    -0
      readtime.js

+ 10
- 0
readtime.js View File

@@ -0,0 +1,10 @@
var wpm = 200; // Average reading rate in words per minute (WPM)

var ps = document.querySelectorAll('p');
var wordCount = 0;
for (var i=0; i<ps.length; i++) {
wordCount += ps[i].innerText.split(/\s+/).length;
}
var $time = document.querySelector('#post-body .dt-published');
$time.style.display = 'inline-block';
$time.insertAdjacentHTML("afterend", '<span class="dt-published"> &middot; ' + Math.round(wordCount / wpm) + ' min read</span>');

Loading…
Cancel
Save