-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadingDurationBookmarklet.js
220 lines (168 loc) · 5.38 KB
/
readingDurationBookmarklet.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*!
* readingDurationBookmarklet v0.1.0
* Dynamic Bookmarklet to detect the reading time of an article
*
* Copyright (c) 2017 - Tom Lutzenberger (lutzenbergerthomas at gmail dot com)
* https://github.com/tomlutzenberger/readingDurationBookmarklet
* https://tomlutzenberger.github.io/readingDurationBookmarklet/
*
* @license: Licensed under the MIT license
* https://github.com/tomlutzenberger/readingDurationBookmarklet/blob/master/LICENSE
*/
/*globals document*/
/*jslint esnext:true */
const readingDurationBookmarklet = () => {
'use strict';
// Constants to prevent using magic numbers
const ZERO = 0;
const FIRST = 0;
const AVG_WORDS_PER_MIN = 220;
const SEC_PER_MIN = 60;
const SEC_PER_HOUR = 3600;
const TWO_DIGIT_SEC = 10;
const defaultSite = { name: 'default', selector: 'article' };
/**
* @method getSite
* @description Get all officially supported sites and their article selectors
*
* @returns {Object[]}
*/
const getSite = () => {
return [{
name: 'dev.to',
selector: '#article-body'
}, {
name: 'medium.com',
selector: '.section-content'
}];
};
/**
* @method getIgnoredTags
* @description Get an array with all elements that should be ignored (stripped) from content
*
* @returns {String[]}
*/
const getIgnoredTags = () => {
return [
'code',
'pre'
];
};
/**
* @method detectSite
* @description Detect the site we are on. If not supported, use default site config
*
* @returns {Object}
*/
const detectSite = () => {
const site = getSite().filter((siteObj) => {
const pattern = new RegExp(siteObj.name, 'im');
return pattern.test(window.location.host);
});
return site.length > ZERO ? site[FIRST] : defaultSite;
};
/**
* @method getArticleContent
* @description Get article content as plain text or false if not found
*
* @param {String} selector - The selector of the content element
* @returns {(Boolean|String)}
*/
const getArticleContent = (selector) => {
let article = document.querySelector(selector);
if (article !== null) {
return cleanupContent(stripIgnoredTags(article));
}
return false;
};
/**
* @method stripIgnoredTags
* @description Strip all ignored tags from content
*
* @param {Element} content - (DOM-)Element with all the content
* @returns {Element}
*/
const stripIgnoredTags = (content) => {
const tagSelectorList = getIgnoredTags().join(',');
const nodeList = content.querySelectorAll(tagSelectorList);
if (nodeList.length > ZERO) {
nodeList.forEach((nodeElement) => {
nodeElement.remove();
});
}
return content;
};
/**
* @method cleanupContent
* @description Remove all unnecessary whitespaces and HTML tags and return plain text
*
* @param {Element} content - (DOM-)Element with all the content
* @returns {String}
*/
const cleanupContent = (content) => {
let cleanedContent = content.textContent.trim();
cleanedContent = cleanedContent.replace(/\s+/, ' ');
cleanedContent = cleanedContent.replace(/[^\w\s]/, ' ');
return cleanedContent;
};
/**
* @method countWords
* @description Count the words in the content text
*
* @param {String} content - Plain text content
* @returns {Integer}
*/
const countWords = (content) => {
return content.split(' ').length;
};
/**
* @method calculateReadDuration
* @description Calculate reading duration based on average words-per-minute
*
* @param {String} content - Plain text content
* @returns {Float}
*/
const calculateReadDuration = (content) => {
const durationSec = (countWords(content) / AVG_WORDS_PER_MIN) * SEC_PER_MIN;
return formatReadDuration(durationSec);
};
/**
* @method formatReadDuration
* @description Format duration to readable text
*
* @param {Float} seconds - Calculated amount of seconds to read
* @returns {String}
*/
const formatReadDuration = (seconds) => {
let minutesAmount = 0;
let minutes = 0;
let hours = 0;
if (seconds < SEC_PER_MIN) {
return '< 1min';
} else {
hours = parseInt(seconds / SEC_PER_HOUR);
minutesAmount = Math.round((seconds % SEC_PER_HOUR) / SEC_PER_MIN);
minutes = hours > ZERO && minutesAmount < TWO_DIGIT_SEC ? '0' + minutesAmount : minutesAmount;
return hours > ZERO ? `${hours}hrs ${minutes}min` : `${minutes}min`;
}
};
/**
* @method execute
* @description Execute script and alert result
*
* @returns {void}
*/
const execute = () => {
const site = detectSite();
const article = getArticleContent(site.selector);
let message = '';
if(!article) {
message = 'Something went wrong. Sorry!';
} else {
message = `${countWords(article)} Words, ${calculateReadDuration(article)}`;
}
alert(message);
};
return {execute};
};
readingDurationBookmarklet().execute();