From 6a1a4f2746b5d3597b9032978300e67949526398 Mon Sep 17 00:00:00 2001 From: math-gh <> Date: Mon, 4 Sep 2023 22:31:19 +0200 Subject: [PATCH] fixed readingtime --- xExtension-ReadingTime/static/readingtime.js | 117 ++++++++++--------- 1 file changed, 59 insertions(+), 58 deletions(-) diff --git a/xExtension-ReadingTime/static/readingtime.js b/xExtension-ReadingTime/static/readingtime.js index 88fc262..b6d3624 100644 --- a/xExtension-ReadingTime/static/readingtime.js +++ b/xExtension-ReadingTime/static/readingtime.js @@ -1,76 +1,77 @@ -/* globals $ */ - (function reading_time() { - 'use strict'; + 'use strict'; - var reading_time = { - flux_list: null, - flux: null, - textContent: null, - words_count: null, - read_time: null, - reading_time: null, + const reading_time = { + flux_list: null, + flux: null, + textContent: null, + words_count: null, + read_time: null, + reading_time: null, - init: function() { - var flux_list = document.querySelectorAll('[id^="flux_"]'); + init: function () { + 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) { - continue; - } + reading_time.flux = flux_list[i]; - 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 - 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; - 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"); - 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 ul = document.querySelector('#' + reading_time.flux.id + ' ul.horizontal-list'); + ul.insertBefore(li, ul.children[ul.children.length - 1]); + } + }, - const ul = document.querySelector("#" + reading_time.flux.id + " ul.horizontal-list"); - ul.insertBefore(li, ul.children[ul.children.length - 1]); - } - }, + flux_words_count: function flux_words_count(flux) { + // 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) - 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 + calc_read_time: function calc_read_time(wd_count, speed) { + reading_time.read_time = Math.round(wd_count / speed); - 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) { - 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; - }, - }; + return reading_time.read_time; + }, + }; - function add_load_more_listener() { - reading_time.init(); - document.body.addEventListener('freshrss:load-more', function (e) { - 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); - } + function add_load_more_listener() { + reading_time.init(); + document.body.addEventListener('freshrss:load-more', function (e) { + 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); + } }());