11'use strict'
22
3- const SetReadTime = ( ) => {
4- const timeDisplays = document . querySelectorAll ( '[wt-readtime-element="display"]' ) ;
5- const container = document . querySelector ( '[wt-readtime-element="article"]' ) ;
6-
7- let valid = true ;
8- let err = [ ]
9-
10- if ( ! timeDisplays || timeDisplays . length === 0 ) {
11- err . push ( `%c display element is missing` ) ;
12- valid = false ;
13- }
14- if ( ! container ) {
15- err . push ( `%c article element is missing` ) ;
16- valid = false ;
3+ class ReadTime {
4+ constructor ( _container ) {
5+ this . articleContainer = _container ;
6+ this . timeDisplays = document . querySelectorAll ( '[wt-readtime-element="display"]' ) ;
7+ this . text = this . articleContainer ? this . articleContainer . innerText : "" ;
8+ this . words = this . text . trim ( ) . split ( / \s + / ) . length ;
9+ this . wpm = this . articleContainer . getAttribute ( "wt-readtime-words" ) || 225 ;
10+ this . suffix = this . articleContainer . getAttribute ( 'wt-readtime-suffix' ) || "" ;
11+ this . smallsuffix = this . articleContainer . getAttribute ( 'wt-readtime-smallsuffix' ) || "" ;
12+ this . rawTime = this . words / this . wpm ;
13+ this . init ( ) ;
1714 }
1815
19- if ( ! valid ) {
20- console . log ( "%cThanks for using Webflow Trickery" , "color: blue; font-size: 18px; padding: 8px; font-weight:500" ) ;
21- console . log ( "%cIt seems like you're missing an element that is required for the integration." , "color: red; font-size: 12px; padding: 8px; font-weight:500" ) ;
22- for ( let e of err ) {
23- console . log ( e , "color: red; font-size: 12px; padding: 8px; font-weight:500" )
24- }
25- return ;
16+ init = ( ) => {
17+ if ( this . timeDisplays && this . timeDisplays . length > 0 ) this . SetReadTime ( ) ;
2618 }
2719
28- const text = container . innerText ;
29- const words = text . trim ( ) . split ( / \s + / ) . length ;
30- const wpm = container . getAttribute ( "wt-readtime-words" ) || 225 ;
31- const suffix = container . getAttribute ( 'wt-readtime-suffix' ) ;
32- const smallsuffix = container . getAttribute ( 'wt-readtime-smallsuffix' ) ;
33- const rawTime = words / wpm ;
34-
35- for ( let display of timeDisplays ) {
36- if ( rawTime < 1 ) {
37- display . innerText = smallsuffix ? smallsuffix : "less than a minute." ;
38- }
39- else if ( rawTime == 1 ) {
40- display . innerText = "a minute." ;
41- }
42- else {
43- display . innerText = suffix ? `${ Math . ceil ( rawTime ) } ${ suffix } ` : `${ Math . ceil ( rawTime ) } minutes.` ;
20+ SetReadTime = ( ) => {
21+ for ( let _display of this . timeDisplays ) {
22+ if ( this . rawTime < 1 )
23+ _display . innerText = this . smallsuffix ? this . smallsuffix : "less than a minute." ;
24+ else if ( this . rawTime == 1 )
25+ _display . innerText = "a minute." ;
26+ else
27+ _display . innerText = this . suffix ? `${ Math . ceil ( this . rawTime ) } ${ this . suffix } ` : `${ Math . ceil ( this . rawTime ) } minutes.` ;
4428 }
4529 }
4630}
4731
48- window . addEventListener ( 'DOMContentLoaded' , SetReadTime ( ) ) ;
32+ const InitializeReadTime = ( ) => {
33+ window . trickeries = window . trickeries || [ ] ;
34+ let articleContainers = document . querySelectorAll ( '[wt-readtime-element="article"]' ) ;
35+ if ( ! articleContainers || articleContainers . length === 0 ) return ;
36+ articleContainers . forEach ( article => {
37+ let instance = new ReadTime ( article ) ;
38+ window . trickeries . push ( { 'ReadTime' : instance } ) ;
39+ } ) ;
40+ }
41+
42+ if ( / c o m p l e t e | i n t e r a c t i v e | l o a d e d / . test ( document . readyState ) ) {
43+ InitializeReadTime ( ) ;
44+ } else {
45+ window . addEventListener ( 'DOMContentLoaded' , InitializeReadTime )
46+ }
0 commit comments