-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathminRead.js
More file actions
38 lines (32 loc) · 1.04 KB
/
minRead.js
File metadata and controls
38 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(function($) {
$.fn.minRead = function( options ) {
var settings = $.extend({}, $.fn.minRead.defaults, options);
return this.each( function() {
var element = $(this);
var time = calculateTime(element.text(), settings);
element.parent().find(settings.where).text(time + " min read");
if (settings.archive) {
var articleLink = element.find(settings.anchor);
var articleUrl = articleLink.attr("href");
// console.log(articleUrl);
$.get(articleUrl, function(data){
data = data.replace(/<img.+>/gi, "");
// console.log(data);
var text = $(data).find(settings.archiveText).text();
var archiveTime = calculateTime(text, settings);
element.find(settings.where).text(archiveTime + " min read");
});
}
});
};
function calculateTime(text, settings) {
return Math.ceil(text.split(' ').length / settings.wordsPerMinute) || 1;
}
$.fn.minRead.defaults = {
where : ".min-read",
wordsPerMinute : 180,
archive : false,
archiveText : ".text",
anchor : ".article-link"
};
})(jQuery);