PageSourceSearch

https://workforcenavigator.org/site/templates/scripts/readingtime.js

js workforcenavigator.org collected 2026-10-07 01:03:11 UTC 7,011 bytes, 264 lines download raw bytes

1/*!
2Name: Reading Time
3Dependencies: jQuery
4Author: Michael Lynch
5Author URL: http://michaelynch.com
6Date Created: August 14, 2013
7Date Updated: April 30, 2018
8Licensed under the MIT license
9*/
10
11;(function($) {
12
13	$.fn.readingTime = function(options) {
14
15		// define default parameters
16		const defaults = {
17			readingTimeTarget: '.eta',
18			readingTimeAsNumber: false,
19			wordCountTarget: null,
20			wordsPerMinute: 270,
21			round: true,
22			lang: 'en',
23			lessThanAMinuteString: '',
24			prependTimeString: '',
25			prependWordString: '',
26			remotePath: null,
27			remoteTarget: null,
28			success: function() {},
29			error: function() {}
30		};
31
32		const plugin = this;
33		const el = $(this);
34
35		let wordsPerSecond;
36		let lessThanAMinute;
37		let minShortForm;
38
39		let totalWords;
40		let totalReadingTimeSeconds;
41
42		let readingTimeMinutes;
43		let readingTimeSeconds;
44		let readingTime;
45		let readingTimeObj;
46
47		// merge defaults and options
48		plugin.settings = $.extend({}, defaults, options);
49
50		// define vars
51		const s = plugin.settings;
52
53		const setTime = function(o) {
54
55			if(o.text !== '') {
56
57				//split text by spaces to define total words
58				totalWords = o.text.trim().split(/\s+/g).length;
59
60				//define words per second based on words per minute (s.wordsPerMinute)
61				wordsPerSecond = s.wordsPerMinute / 60;
62
63				//define total reading time in seconds
64				totalReadingTimeSeconds = totalWords / wordsPerSecond;
65
66				// define reading time
67				readingTimeMinutes = Math.floor(totalReadingTimeSeconds / 60);
68
69				// define remaining reading time seconds
70				readingTimeSeconds = Math.round(totalReadingTimeSeconds - (readingTimeMinutes * 60));
71
72				// format reading time
73				readingTime = `${readingTimeMinutes}:${readingTimeSeconds}`;
74
75				// if s.round
76				if(s.round) {
77
78					// if minutes are greater than 0
79					if(readingTimeMinutes > 0) {
80
81						// set reading time by the minute
82						$(s.readingTimeTarget).text(s.prependTimeString + readingTimeMinutes + ((!s.readingTimeAsNumber) ? ' ' + minShortForm : ''));
83
84					} else {
85
86						// set reading time as less than a minute
87						$(s.readingTimeTarget).text((!s.readingTimeAsNumber) ? s.prependTimeString + lessThanAMinute : readingTimeMinutes);
88					}
89
90				} else {
91
92					// set reading time in minutes and seconds
93					$(s.readingTimeTarget).text(s.prependTimeString + readingTime);
94				}
95
96				// if word count container isn't blank or undefined
97				if(s.wordCountTarget !== '' && s.wordCountTarget !== undefined) {
98
99					// set word count
100					$(s.wordCountTarget).text(s.prependWordString + totalWords);
101				}
102
103				readingTimeObj = {
104					wpm: s.wordsPerMinute,
105					words: totalWords,
106					eta: {
107						time: readingTime,
108						minutes: readingTimeMinutes,
109						seconds: totalReadingTimeSeconds
110					}
111				};
112
113				// run success callback
114				s.success.call(this, readingTimeObj);
115
116			} else {
117
118				// run error callback
119				s.error.call(this, {
120					error: 'The element does not contain any text'
121				});
122			}
123		};
124
125		// if no element was bound
126		if(!this.length) {
127
128			// run error callback
129			s.error.call(this, {
130				error: 'The element could not be found'
131			});
132
133			// return so chained events can continue
134			return this;
135		}
136
137		// Use switch instead of ifs
138		switch (s.lang) {
139			// if s.lang is Arabic
140			case 'ar':
141        lessThanAMinute = s.lessThanAMinuteString || "أقل من دقيقة";
142        minShortForm = 'دقيقة';
143        break;
144			// if s.lang is Czech
145			case 'cz':
146        lessThanAMinute = s.lessThanAMinuteString || "Méně než minutu";
147        minShortForm = 'min';
148        break;
149			// if s.lang is Danish
150			case 'da':
151        lessThanAMinute = s.lessThanAMinuteString || "Mindre end et minut";
152        minShortForm = 'min';
153        break;
154			// if s.lang is German
155      case 'de':
156        lessThanAMinute = s.lessThanAMinuteString || "Weniger als eine Minute";
157        minShortForm = 'min';
158        break;
159			// if s.lang is Spanish
160      case 'es':
161        lessThanAMinute = s.lessThanAMinuteString || "Menos de un minuto";
162        minShortForm = 'min';
163        break;
164			// if s.lang is French
165      case 'fr':
166        lessThanAMinute = s.lessThanAMinuteString || "Moins d'une minute";
167        minShortForm = 'min';
168        break;
169			// if s.lang is Hungarian
170      case 'hu':
171        lessThanAMinute = s.lessThanAMinuteString || "Kevesebb mint eg
171y perc";
172        minShortForm = 'perc';
173        break;
174			// if s.lang is Icelandic
175      case 'is':
176        lessThanAMinute = s.lessThanAMinuteString || "Minna en eina mínútu";
177        minShortForm = 'min';
178        break;
179			// if s.lang is Italian
180      case 'it':
181        lessThanAMinute = s.lessThanAMinuteString || "Meno di un minuto";
182        minShortForm = 'min';
183        break;
184			// if s.lang is Dutch
185      case 'nl':
186        lessThanAMinute = s.lessThanAMinuteString || "Minder dan een minuut";
187        minShortForm = 'min';
188        break;
189			// if s.lang is Norwegian
190      case 'no':
191        lessThanAMinute = s.lessThanAMinuteString || "Mindre enn ett minutt";
192        minShortForm = 'min';
193        break;
194			// if s.lang is Polish
195      case 'pl':
196        lessThanAMinute = s.lessThanAMinuteString || "Mniej niż minutę";
197        minShortForm = 'min';
198        break;
199			// if s.lang is Russian
200      case 'ru':
201        lessThanAMinute = s.lessThanAMinuteString || "Меньше минуты";
202        minShortForm = 'мин';
203        break;
204			// if s.lang is Slovak
205      case 'sk':
206        lessThanAMinute = s.lessThanAMinuteString || "Menej než minútu";
207        minShortForm = 'min';
208        break;
209			// if s.lang is Swedish
210      case 'sv':
211        lessThanAMinute = s.lessThanAMinuteString || "Mindre än en minut";
212        minShortForm = 'min';
213        break;
214			// if s.lang is Turkish
215      case 'tr':
216        lessThanAMinute = s.lessThanAMinuteString || "Bir dakikadan az";
217        minShortForm = 'dk';
218        break;
219			// if s.lang is Ukrainian
220      case 'uk':
221        lessThanAMinute = s.lessThanAMinuteString || "Менше хвилини";
222        minShortForm = 'хв';
223        break;
224			// if s.lang is Greek
225			case 'el':
226        lessThanAMinute = s.lessThanAMinuteString || 'Λιγότερο από λεπτό';
227        minShortForm = 'λεπτά';
228        break;
229			// default s.lang in english
230			default:
231        lessThanAMinute = s.lessThanAMinuteString || 'Less than a minute';
232        minShortForm = 'min';
233    }
234
235		// for each element
236		el.each(function(index) {
237
238			// if s.remotePath and s.remoteTarget aren't null
239			if(s.remotePath != null && s.remoteTarget != null) {
240
241				// get contents of remote file
242				$.get(s.remotePath, function(data) {
243					let wrapper = document.createElement('div');
244
245					wrapper.innerHTML = data;
246
247					// set time using the remote target found in the remote file
248					setTime({
249						text: $(wrapper).find(s.remoteTarget).text()
250					});
251				});
252
253			} else {
254
255				// set time using the targeted element
256				setTime({
257					text: el.text()
258				});
259			}
260		});
261
262		return true;
263	}
264})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.