fixed readingtime

This commit is contained in:
math-gh 2023-09-04 22:31:19 +02:00
parent ddb1144a61
commit 6a1a4f2746

View file

@ -1,76 +1,77 @@
/* globals $ */
(function reading_time() { (function reading_time() {
'use strict'; 'use strict';
var reading_time = { const reading_time = {
flux_list: null, flux_list: null,
flux: null, flux: null,
textContent: null, textContent: null,
words_count: null, words_count: null,
read_time: null, read_time: null,
reading_time: null, reading_time: null,
init: function() { init: function () {
var flux_list = document.querySelectorAll('[id^="flux_"]'); const flux_list = document.querySelectorAll('[id^="flux_"]');
for (var i = 0; i < flux_list.length; i++) { for (let i = 0; i < flux_list.length; i++) {
if ('readingTime' in flux_list[i].dataset) {
continue;
}
if ("readingTime" in flux_list[i].dataset) { reading_time.flux = flux_list[i];
continue;
}
reading_time.flux = flux_list[i]; reading_time.words_count = reading_time.flux_words_count(flux_list[i]); // count the words
// change this number (in words) to your prefered reading speed:
reading_time.reading_time = reading_time.calc_read_time(reading_time.words_count, 300);
reading_time.words_count = reading_time.flux_words_count(flux_list[i]); // count the words flux_list[i].dataset.readingTime = reading_time.reading_time;
reading_time.reading_time = reading_time.calc_read_time(reading_time.words_count, 300); // change this number (in words) to your prefered reading speed
flux_list[i].dataset.readingTime = reading_time.reading_time; const li = document.createElement('li');
li.setAttribute('class', 'item date');
li.style.width = 'min-content';
li.style.minWidth = '40px';
li.style.overflow = 'hidden';
li.style.textAlign = 'right';
li.style.display = 'table-cell';
li.textContent = reading_time.reading_time + '\u2009m';
const li = document.createElement("li"); const ul = document.querySelector('#' + reading_time.flux.id + ' ul.horizontal-list');
li.setAttribute("class", "item date"); ul.insertBefore(li, ul.children[ul.children.length - 1]);
li.style.width = "min-content"; }
li.style.minWidth = "40px"; },
li.style.overflow = "hidden";
li.style.textAlign = "right";
li.style.display = "table-cell";
li.textContent = reading_time.reading_time + '\u2009m';
const ul = document.querySelector("#" + reading_time.flux.id + " ul.horizontal-list"); flux_words_count: function flux_words_count(flux) {
ul.insertBefore(li, ul.children[ul.children.length - 1]); // get textContent, from the article itself (not the header, not the bottom line):
} reading_time.textContent = flux.querySelector('.flux_content .content').textContent;
},
flux_words_count: function flux_words_count(flux) { // split the text to count the words correctly (source: http://www.mediacollege.com/internet/javascript/text/count-words.html)
reading_time.textContent = reading_time.textContent.replace(/(^\s*)|(\s*$)/gi, ''); // exclude start and end white-space
reading_time.textContent = reading_time.textContent.replace(/[ ]{2,}/gi, ' '); // 2 or more space to 1
reading_time.textContent = reading_time.textContent.replace(/\n /, '\n'); // exclude newline with a start spacing
reading_time.textContent = flux.querySelector('.flux_content .content').textContent; // get textContent, from the article itself (not the header, not the bottom line). return reading_time.textContent.split(' ').length;
},
// split the text to count the words correctly (source: http://www.mediacollege.com/internet/javascript/text/count-words.html) calc_read_time: function calc_read_time(wd_count, speed) {
reading_time.textContent = reading_time.textContent.replace(/(^\s*)|(\s*$)/gi,"");//exclude start and end white-space reading_time.read_time = Math.round(wd_count / speed);
reading_time.textContent = reading_time.textContent.replace(/[ ]{2,}/gi," ");//2 or more space to 1
reading_time.textContent = reading_time.textContent.replace(/\n /,"\n"); // exclude newline with a start spacing
return reading_time.textContent.split(' ').length; if (reading_time.read_time === 0) {
}, reading_time.read_time = '<1';
}
calc_read_time : function calc_read_time(wd_count, speed) { return reading_time.read_time;
reading_time.read_time = Math.round(wd_count/speed); },
if (reading_time.read_time === 0) { reading_time.read_time = '<1'; } };
return reading_time.read_time;
},
};
function add_load_more_listener() { function add_load_more_listener() {
reading_time.init(); reading_time.init();
document.body.addEventListener('freshrss:load-more', function (e) { document.body.addEventListener('freshrss:load-more', function (e) {
reading_time.init(); reading_time.init();
}); });
} }
if (document.readyState && document.readyState !== 'loading') {
add_load_more_listener();
} else if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', add_load_more_listener, false);
}
if (document.readyState && document.readyState !== 'loading') {
add_load_more_listener();
} else if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', add_load_more_listener, false);
}
}()); }());