-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordswift.js
executable file
·1578 lines (1115 loc) · 49.8 KB
/
wordswift.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const WordSwift = function(window, document, undefined) {
let W;
let c;
return {
init: () => {
W = WordSwift;
W.config = {
/* comma separated list of css selectors for each element to add
WordSwift functionality to */
wsSelectors: '.ws-article',
wsChildSelectors: 'div, h1, h2, span, blockquote, li, p',
/* comma separated list of css selectors for each element that is
a child of the element(s) targeted above, whose text content
should be excluded from WordSwift reading */
wsChildsExcluded: 'form, fieldset, legend, label, datalist, input, button, select, option, optgroup, textarea, script, noscript, template, figure, table, embed, object, video, audio, canvas',
/* @# */
includeParentText: true,
/* set default speed of WordSwift in words per minute */
wpm: 600,
/* set default number of words displayed at a time */
numDisplayWords: 2,
/* set default font size including unit type */
fontSize: '40px',
/* set whether HTML text styles (like underline) used */
applyWordStyles: 'yes',
/* @# */
stepSize: 1,
/* @# */
stepSpeed: 50,
};
/* settings applied from cookies (if set) or from defaults */
W.appliedSettings = {
wpm: null,
numDisplayWords: null,
fontSize: null,
applyWordStyles: null,
stepSize: null,
stepSpeed: null
};
c = WordSwift.config;
aS = WordSwift.appliedSettings;
W.rootDomain = W.getRootDomain();
W.setCookies();
W.setAppliedSettings();
W.wsCtnrs;
W.constructExampleArticle();
W.setWsTriggers();
W.targ = null;
W.wsText = [];
W.wsWords = [];
W.currWsWord = 0;
W.msPerWord = parseInt( ( ( 1000 * 60 ) / aS.wpm ) * aS.numDisplayWords, 10 );
W.clickDown = false;
W.initMouseXCoord = null;
W.prevKnobXCoord = null;
W.numSliderNotches = null;
W.playWsLastTime = 0;
W.playTO;
W.wasPlaying = false;
W.wordCounter = 0;
W.mDownOnButtonId = null;
W. wsRafLastTime = 0;
},
constructExampleArticle: function() {
const articleContainer = document.getElementById( 'wordswift-card' );
const frag = document.createDocumentFragment();
const article = document.createElement( 'div' );
article.style.position = 'absolute';
article.style.height = '0px';
article.style.width = '0px';
article.style.overflow = 'hidden';
article.id = 'wordswift-example-article';
article.innerHTML = '<h1>This speed reader app</h1> offers your site\'s visitors the ability to read your content at an accelerated pace. The faster your readers can consume your content, the more they will read. It works by eliminating some of the key factors for why so many of us read slower than we\'d like. Single words from an article are flashed on the screen, quickly and in succession, so as to eliminate the need for readers to move their eyes from word to word, which adds significantly to the time it takes to get through an article. Also, because the words are flashed so quickly, readers are also able to avoid subvocalization of words as they read. If you\'ve ever noticed yourself "speaking" words to yourself as you read, that is subvocalization and it is entirely unnecessary and serves only to slow you down. This app is designed to be very adaptable to various site. You are able configure it to target the elements on your page that contain your text content. You are also able to target specific elements within them whose text should be excluded. A good example of this is when sites sometimes you blockquotes as callouts. This text should not be included in the speed reader since the same text would be shown twice. Additionally, the words displayed in the speed reader are shown in a style based on what type of element they are from to give readers more context. For instance, words from <h1> through <h6> elements are displayed with an overline and underline <h2>Like This Title For Example</h2>. Words from <a> elements are displayed with an underline <a href="https://www.joedisalvo.com">like this</a>. For a full breakdown of these styles, click the info tab above. Users are also able to customize this speed reader too using the menu tab. They are able to change the speed at which the words are displayed in words per minute, the number of words displayed per frame, the font size, and whether or not the aforementioned word styles are applied or each word is just displayed as plain text.';
frag.appendChild(article);
articleContainer.appendChild(frag);
},
setCookies: function() {
// set Words Per Minute Cookie
if (W.readCookie('wordswift-wpm') === null) {
W.setCookie({cName: 'wordswift-wpm', cVal: c.wpm, cDays: 9999, cDomain: W.rootDomain});
}
if (W.readCookie('wordswift-numDisplayWords') === null) {
W.setCookie({cName: 'wordswift-numDisplayWords', cVal: c.numDisplayWords, cDays: 9999, cDomain: W.rootDomain});
}
if (W.readCookie('wordswift-fontSize') === null) {
W.setCookie({cName: 'wordswift-fontSize', cVal: c.fontSize, cDays: 9999, cDomain: W.rootDomain});
}
if (W.readCookie('wordswift-applyWordStyles') === null) {
W.setCookie({cName: 'wordswift-applyWordStyles', cVal: c.applyWordStyles, cDays: 9999, cDomain: W.rootDomain});
}
// W.setCookie({cName: 'wordswift-wpm', cVal: c.wpm, cDays: -9999, cDomain: W.rootDomain});
// W.setCookie({cName: 'wordswift-numDisplayWords', cVal: c.numDisplayWords, cDays: -9999, cDomain: W.rootDomain});
// W.setCookie({cName: 'wordswift-fontSize', cVal: c.fontSize, cDays: -9999, cDomain: W.rootDomain});
// W.setCookie({cName: 'wordswift-applyWordStyles', cVal: c.applyWordStyles, cDays: -9999, cDomain: W.rootDomain});
},
setAppliedSettings: function() {
let wpm = parseInt( W.readCookie( 'wordswift-wpm' ), 10 );
let numDisplayWords = parseInt(W.readCookie( 'wordswift-numDisplayWords' ), 10 );
let fontSize = W.readCookie( 'wordswift-fontSize' );
let applyWordStyles = W.readCookie( 'wordswift-applyWordStyles' );
aS.wpm = (wpm !== null) ? parseInt( wpm, 10 ) : parseInt( c.wpm, 10 );
aS.numDisplayWords = (numDisplayWords !== null) ? parseInt( numDisplayWords, 10 ) : parseInt( c.numDisplayWords, 10 );
aS.fontSize = (fontSize !== null) ? fontSize : c.fontSize;
aS.applyWordStyles = (applyWordStyles !== null) ? applyWordStyles : c.applyWordStyles;
},
updateAppliedSetting: function(setting, val) {
var cName = 'wordswift-' + setting;
W.setCookie({cName: cName, cVal: val, cDays: 9999, cDomain: W.rootDomain});
aS[setting] = val;
},
setWsTriggers: function() {
let triggerEls = document.querySelectorAll( '.ws-trigger-el' );
for (let i = 0; i < triggerEls.length; i++ ) {
W.addEvts( triggerEls[ i ], 'click', W.constructWsEls );
}
},
constructWsEls: function( e ) {
W.targ = e.target || e.srcElement || undefined;
// W.parentClone = W.targ.parentNode.parentNode.cloneNode( true );
W.parentClone = document.getElementById( 'wordswift-example-article' );
try {
W.wsEls = W.parentClone.querySelectorAll( c.wsChildSelectors ); //separate this into own mini function so only calced once
}
catch ( e ) {
if ( c.wsChildSelectors === '' ) {
W.wsEls = W.parentClone.querySelectorAll( '*' );
return;
}
// ??? Now not likely necessary
alert( e + ' You have provided an invalid selector for c.wsChildSelectors. Please edit the settings portion of the code so the value of c.wsChildSelectors is either an empty string (\'\') or a valid CSS selector or set of comma separated selectors.');
};
W.constructWsElsExcluded();
},
constructWsElsExcluded: function() {
try {
W.wsElsExcluded = W.parentClone.querySelectorAll( c.wsChildsExcluded ); //separate this into own mini function so only calced once
}
catch ( e ) {
if ( c.wsChildsExcluded !== '' ) {
alert( e + ' You have provided an invalid selector for c.wsChildSelectors. Please edit the settings portion of the code so the value of c.wsChildSelectors is either an empty string (\'\') or a valid CSS selector or set of comma separated selectors.');
}
else {
W.wsElsExcluded = [];
}
};
W.constructWsContent();
},
constructWsContent: function() {
var elems = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'span', 'li', 'q', 'em', 'blockquote', 'dt', 'dd', 'cite', 'code', 'ins', 'del', 'strong', 'sub', 'sup', 'abbr', 'samp', 'small', 'b', 'i', 's', 'u', 'kbd', 'address', 'dfn', 'data', 'time', 'var', 'mark', 'bdi', 'bdo' ];
var elemsLen = elems.length;
W.wsContent = [];
var scanWsNodes = function( theNode, isWsEl, includeDescendants, isExcluded ) {
// REMEMBER, NEED TO ONLY ADD THE TEXT OF ELS IF THEY ARE: SPECIFIED IN QSA, OR IF * or '' IS GIVEN AS ARG FOR QSA
// isWsEl and isIncluded should always start out being false when scanning an el (not text node though)
var isWsCtnrChild,
currChild,
currParent,
currParentTag,
nextAncestor,
spanClass,
theData;
var scanAncestors = function( ancestor, wsContentIndex ) {
var matched = false;
currParentTag = ancestor.tagName.toLowerCase();
nextAncestor = ancestor.parentNode;
// this value needs to be the saved cookie value of the user's preference
var cookiePref = '';
// // would if (typeof cookiePref === 'undefined') be better???
// if (cookiePref !== true && cookiePref !== false) {
// if (aS.applyWordStyles == 'no') {
// return;
// }
// } else if (cookiePref === false) {
// return;
// }
for ( var elemIndex = 0; elemIndex < elemsLen; elemIndex++ ) {
matched = addClasses( elems[ elemIndex ], ancestor, wsContentIndex, false );
if ( matched === true ) {
break;
}
}
if ( matched === true && nextAncestor !== W.parentClone ) {
scanAncestors( nextAncestor, wsContentIndex );
} else {
W.wsContent[ wsContentIndex ].styledParents = W.wsContent[ wsContentIndex ].styledParents.replace( /\s+$/g, '' );
}
}
var addClasses = function( elName, ancestor, wsContentIndex, matched ) {
if ( elName === currParentTag ) {
matched = true;
W.wsContent[ wsContentIndex ].styledParents += 'ws-' + currParentTag + ' ';
if ( currParentTag === 'h1' || currParentTag === 'h2' || currParentTag === 'h3'
|| currParentTag === 'h4' || currParentTag === 'h5' || currParentTag === 'h6' ) {
W.wsContent[ wsContentIndex ].styledParents += 'ws-h ';
}
if ( currParentTag === 'a' ) {
W.wsContent[ wsContentIndex ].isAnchorText = true;
W.wsContent[ wsContentIndex ].theHref = ancestor.href;
}
if ( currParentTag === 'span' ) {
if ( currParent.id !== '' ) {
W.wsContent[ wsContentIndex ].styledParents += 'ws-' + currParentTag + '-id-' + currParent.id + ' ';
}
if ( currParent.className !== '' ) {
spanClass = currParent.className.replace( /^\s+|\s+$/g, '' );
spanClass = spanClass.split(/[\s]+/g);
// ??? need to test this whole thing to see if it works. Also need to come up w/ original var names for this iterator and ones below
for ( var i = 0; i < spanClass.length; i++ ) {
W.wsContent[ wsContentIndex ].styledParents += 'ws-' + currParentTag + '-' + spanClass[ i ] + ' ';
}
}
}
}
return matched;
};
if ( currChild = theNode.firstChild ) {
do {
currParent = currChild.parentNode;
// following code is run for each child of the parentClone, and each child of those children, etc
// if we're dealing w/ a direct child of parentClone, then we mark that, and also, if no childSelectors or the * is provided, then text from all ancestors is included
if ( theNode === W.parentClone ) {
isWsCtnrChild = true;
includeDescendants = ( c.wsChildSelectors === '' || c.wsChildSelectors ==='*' ) ? true : false;
}
else {
isWsCtnrChild = false;
}
if ( currChild.nodeType === 3 ) {
if ( ( isWsEl === true && isExcluded === false )
|| ( isWsCtnrChild === true && c.includeParentText === true )
|| ( !isExcluded && includeDescendants ) ) {
if ( currChild.data.match( /[^\s]+/ ) ) {
theData = currChild.data.replace( /\n/g, ' ' ).replace( /[\s]{2,}/g, ' ' );
W.wsContent.push( {
theNode: currChild,
theEl: currParent.tagName.toLowerCase(),
theData: theData,
styledParents: '',
isAnchorText: false, // ??? could be a problem if the W.parent is an <a> ( odd but not inconceivable )
theHref: '',
isWsEl: isWsEl
} );
scanAncestors( currParent, W.wsContent.length - 1 );
}
else {
if ( typeof W.wsContent[ 0 ] !== 'undefined' ) {
W.wsContent[ W.wsContent.length - 1 ].theData += ' ';
}
}
}
}
if ( currChild.nodeType === 1 ) {
if ( currChild.firstChild ) { //may need to make this and all other like it !== null / undefined
// anytime we're dealing w/ an el and not text node, these should be reset to false
isExcluded = false;
isWsEl = false;
// if no selector provided, is assumed all elements should be included
// Should only need to do this in certain cases, like if includeDescendants is false
if ( c.wsChildSelectors !== '' && c.wsChildSelectors !== '*' ) {
for ( var i = 0; W.wsEls[ i ]; i++ ) {
if ( currChild === W.wsEls[ i ] ) {
isWsEl = true;
includeDescendants = true;
break;
}
}
}
else {
isWsEl = true;
}
for ( var i = 0; W.wsElsExcluded[ i ]; i++ ) {
if ( currChild === W.wsElsExcluded[ i ] ) {
isWsEl = false;
includeDescendants = false;
isExcluded = true;
break;
}
}
scanWsNodes( currChild, isWsEl, includeDescendants, isExcluded );
}
}
if ( currChild.nodeType === 8 ) {
}
if ( currChild.nodeType === 4 ) {
}
} while ( currChild = currChild.nextSibling )
}
};
// theNode, isWsEl, includeDescendants, isExcluded
scanWsNodes( W.parentClone, false, false, false );
W.constructWsPlainText();
},
constructWsPlainText: function() {
var wsLen = W.wsContent.length;
W.wsPlainText = '';
for ( var i = 0; i < wsLen; i++ ) {
W.wsPlainText += W.wsContent[ i ].theData;
}
W.wsPlainText = W.wsPlainText.replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '' ).split( /\s+/g );
W.constructWsWords();
},
constructWsWords: function() {
var wsLen = W.wsContent.length,
tempWords,
tempLen;
W.wsWords = [];
for ( var i = 0; i < wsLen; i++ ) {
tempWords = W.wsContent[ i ].theData.replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '' ).split( /\s+/g );
tempLen = tempWords.length;
for ( var j = 0; j < tempLen; j++ ) {
W.wsWords.push( {
word: tempWords[ j ],
styledParents: W.wsContent[ i ].styledParents,
isAnchorText: W.wsContent[ i ].isAnchorText,
theHref: W.wsContent[ i ].theHref
} );
}
}
W.correctWsWords();
},
correctWsWords: function() {
//??? need to really go over this function and check specifically for issues that could arise: errors with undefined word properties when this is trying to access word property using the incrementers / indexadjust / etc towards end of W.wsWords. Also, check to make sure there is no way false positives or false negatives could happen.
var wsWordsLen = W.wsWords.length,
tempWord = '',
noMatchCount = 0,
indexAdjust = 0;
for ( var i = 0; i < wsWordsLen; i++ ) {
if ( noMatchCount >= 10 ) {
return;
}
if ( W.wsWords[ i ].word !== W.wsPlainText[ i + indexAdjust ] ) {
tempWord = '';
noMatchCount++;
for ( var j = 1; j < 21; j++ ) {
tempWord = tempWord + W.wsWords[ i + j ].word;
if ( ( W.wsWords[ i ].word + tempWord ) === W.wsPlainText[ i ] ) {
W.wsWords[ i ].word = [ {
word: W.wsWords[ i ].word,
styledParents: W.wsWords[ i ].styledParents,
isAnchorText: W.wsWords[ i ].isAnchorText,
theHref: W.wsWords [ i ].theHref
} ];
for ( var k = 1; k <= j; k++ ) {
W.wsWords[ i ].word.push( {
word: W.wsWords[ i + k ].word,
styledParents: W.wsWords[ i + k ].styledParents,
isAnchorText: W.wsWords[ i + k ].isAnchorText,
theHref: W.wsWords[ i + k ].theHref
} );
}
W.wsWords.splice( ( i + 1 ), j );
wsWordsLen = W.wsWords.length;
noMatchCount = 0;
break;
}
}
// just a failsafe for if wsWords and wsPlainText get out of sync for some reason
// this attempts to find a place where the 2 match again and adjusts what indexes of each
// arrays are compared for remainder of function
if ( ( noMatchCount > 0 ) && ( W.wsWords[ i + 1 ].word !== W.wsPlainText[ i + 1 ] ) ) {
for ( var l = 0 - noMatchCount + 1; l < 10; l++ ) {
if ( W.wsWords[ i ].word === W.wsPlainText[ i + l ] ) {
indexAdjust += l;
noMatchCount = 0;
}
}
}
}
}
W.createWsEls();
},
createWsEls: function() {
let theHTML,
wpm,
numDisplayWords,
fontSize,
applyWordStyles,
applyWordStylesYes,
applyWordStylesNo,
wsNoWordStylesClass;
wsNoWordStylesClass = ( aS.applyWordStyles === 'no' ) ? 'ws-no-word-styles' : '' ;
applyWordStylesYes = ( aS.applyWordStyles === 'yes' ) ? 'selected="selected"' : '';
applyWordStylesNo = ( aS.applyWordStyles === 'no' ) ? 'selected="selected"' : '';
theHTML = [
'<div id="ws-container">',
'<div id="content-ctnr" class="content-ctnr">',
'<div class="ws-tab-ctnr clearfix">',
'<button id="ws-close" class="ws-tab icon-close"><span class="btn-label">Close</span></button>',
'<button id="ws-settings-b" class="ws-tab icon-list"><span class="btn-label">Settings</span></button>',
'<button id="ws-info-b" class="ws-tab icon-question"><span class="btn-label">Info</span></button>',
'</div>',
'<div id="ws-word" class="ws-word">',
'<p id="ws-word-cell" class="ws-word-cell ' + wsNoWordStylesClass + '" style="font-size:' + aS.fontSize + ';"></p>',
'</div>',
'<div id="ws-progress-ctnr" class="ws-progress-ctnr">',
'<div id="ws-progress-bar" class="ws-progress-bar">',
'<div id="ws-progress-knob" class="ws-progress-knob"></div>',
'</div>',
'<div id="ws-controls" class="ws-controls">',
'<button id="ws-beginning" class="ws-beginning ws-button icon-first"></button>',
'<button id="ws-play" class="ws-play ws-button icon-play"></button>',
'<button id="ws-backward" class="ws-backward ws-button icon-previous"></button>',
'<button id="ws-forward" class="ws-forward ws-button icon-next"></button>',
'</div>',
'</div>',
'<div id="ws-settings" class="ws-settings">',
'<div class="ws-settings-display">',
'<h1>WordSwift Options</h1>',
'<label for="ws-speed">Speed (Words Per Minute)</label>',
'<input id="ws-speed" name="ws-speed" type="text" value="' + aS.wpm + '">',
'<label for="ws-num-words">Number of Words Displayed at a Time</label>',
'<input id="ws-num-words" name="ws-num-words" type="text" value="' + aS.numDisplayWords + '">',
'<label for="ws-font-size">Font Size (Include unit)</label>',
'<input id="ws-font-size" name="ws-font-size" type="text" value="' + aS.fontSize + '">',
'<label for="ws-style-words">Apply styles to words based on the semantic value of the text? If yes, text from things like links and headings will be styled to distinguish them from regular text. If no, all words will just appear as plain text. Consult the info section to see a detailed list of all styles applied to words of certain types.</label>',
'<select id="ws-style-words">',
'<option value="yes" ' + applyWordStylesYes + '>Yes</option>',
'<option value="no" ' + applyWordStylesNo + '>No</option>',
'</select>',
'</div>',
'</div>',
'<div id="ws-info" class="ws-info">',
'<div class="ws-info-display">',
'<h1>WordSwift Information and Help</h1>',
'<p>',
'Word Swift offers your visitors the ability to read your content at an excellerated pace. The faster your readers can consume your content, the more they will read. It works by eliminating some of the key factors for why so many of us read slower than we\'d like. Single words from an article are flashed on the screen, quickly and in succession, so as to eliminate the need for readers to move their eyes from word to word. Believe it or not, but this adds significantly to the time it takes to get through an article. Also, because the words are flashed so quickly, readers are also able to avoid subvocalization of words as they read. If you\'ve ever noticed yourself "speaking" words to yourself as you read, that is subvocalization and it is entirely unnecessary and serves only to slow you down.',
'</p>',
'<h2>Keyboard Shortcuts</h2>',
'<p>List of keyboard Shortcuts</p>',
'<h2>Explanation of Styles Applied to Words</h2>',
'<p>',
'WordSwift applies some styles to words to make it easier for you to know more information about the context from which the word was taken. For example, a word taken from a title of a page, is styled in such a way to distinguish it as a title, since that is not otherwise obvious to you as you are reading using WordSwift. The following is a list of the types of elements and the types of styles that will be applied to words that come from them:',
'</p>',
'<p>',
'<span class="ws-h">Words from headers and titles of articles</span>',
'</p>',
'<p>',
'<span class="ws-a">Words from links to other web pages</span>',
'</p>',
'<p>',
'<span class="ws-q">Words from quoted text</span>',
'</p>',
'<p>',
'<span class="ws-em">Words singled out for emphasis</span>',
'</p>',
'<p>',
'<span class="ws-strong">Words singled out for having strong importance</span>',
'</p>',
'<p>',
'<span>Text displayed lower and smaller, such as from numbers in a chemical formula (H<span class="ws-sub">2</span>O)</span>',
'</p>',
'<p>',
'<span>Text displayed higher and smaller, such as from exponents in a math formula (2x<span class="ws-sup">3</span> + 10)</span>',
'</p>',
'<p>',
'<span class="ws-del">Words that were originally part of article, but were removed</span>',
'</p>',
'<p>',
'<span class="ws-s">Text deemed no longer relevant or accurate</span>',
'</p>',
'<p>',
'<span class="ws-code">Text from a block of computer code</span>',
'</p>',
'<p>',
'There may also be some other words stylized in some way, based on the website owner\'s preferences, but the ones listed above are the main ones.',
'</p>',
'</div>',
'</div>',
'</div>',
'</div>'
].join( '' );
var darkener = document.createElement( 'div' ),
container, legendCtnr, contentCtnr, bar, knob, beginning,
play, stepBackward, stepForward, settingsButton, close, info;
darkener.id = 'ws-darken';
darkener.innerHTML = theHTML;
document.body.appendChild( darkener );
container = document.getElementById( 'ws-container' ),
legendCtnr = document.getElementById( 'ws-legend-ctnr' ),
contentCtnr = document.getElementById( 'content-ctnr'),
bar = document.getElementById( 'ws-progress-bar' ),
knob = document.getElementById( 'ws-progress-knob' ),
beginning = document.getElementById( 'ws-beginning' ),
play = document.getElementById( 'ws-play' ),
stepBackward = document.getElementById( 'ws-backward' ),
stepForward = document.getElementById( 'ws-forward' ),
settingsButton = document.getElementById( 'ws-settings-b' ),
close = document.getElementById( 'ws-close' ),
infoButton = document.getElementById('ws-info-b');
wpm = document.getElementById( 'ws-speed' );
numDisplayWords = document.getElementById( 'ws-num-words' );
fontSize = document.getElementById( 'ws-font-size' );
applyWordStyles = document.getElementById( 'ws-style-words' );
W.displayWords();
W.toggleWs( 'in' );
W.progressBarLen = bar.offsetWidth;
W.numWsWords = W.wsWords.length;
W.addEvts( knob, 'mousedown', W.slideDown );
W.addEvts( beginning, 'click', W.goToBeginning );
W.addEvts( play, 'click', W.playWs );
W.addEvts( stepBackward, 'mousedown', W.mDownOnButton );
W.addEvts( stepForward, 'mousedown', W.mDownOnButton );
W.addEvts( document, 'mouseup', W.mUpFromButton );
W.addEvts( document, 'mouseup', W.slideCallBack );
W.addEvts( document, 'mousemove', W.mouseMoved );
W.addEvts( knob, 'touchstart', W.slideDown );
W.addEvts( knob, 'touchmove', W.mouseMoved );
W.addEvts( knob, 'touchend', W.slideCallBack );
W.addEvts( settingsButton, 'click', W.togglePages );
W.addEvts( infoButton, 'click', W.togglePages );
W.addEvts( close, 'click', function() { W.toggleWs( 'out' ); } );
W.addEvts( wpm, 'blur', function() {
W.updateAppliedSetting( 'wpm', parseInt( wpm.value, 10 ) );
W.msPerWord = parseInt( ( ( 1000 * 60 ) / aS.wpm ) * aS.numDisplayWords, 10 );
} );
W.addEvts( numDisplayWords, 'blur', function() {
W.updateAppliedSetting( 'numDisplayWords', parseInt( numDisplayWords.value, 10 ) );
W.msPerWord = parseInt( ( ( 1000 * 60 ) / aS.wpm ) * aS.numDisplayWords, 10 );
W.displayWords();
} );
W.addEvts( fontSize, 'blur', function() {
W.updateAppliedSetting( 'fontSize', fontSize.value );
document.getElementById( 'ws-word-cell' ).style.fontSize = aS.fontSize;
} );
W.addEvts( applyWordStyles, 'blur', function() {
W.updateAppliedSetting( 'applyWordStyles', applyWordStyles.value );
if ( aS.applyWordStyles === 'yes' ) {
document.getElementById( 'ws-word-cell' ).classList.remove( 'ws-no-word-styles' );
}
else {
document.getElementById( 'ws-word-cell' ).classList.add( 'ws-no-word-styles' );
}
} );
W.showEl( container );
W.getSliderScale();
},
toggleWs: function( direction ) {
var darkener = document.getElementById( 'ws-darken' ),
wsContainer = document.getElementById( 'ws-container' ),
bdy = document.body;
if ( direction === 'in' ) {
W.addEvts( darkener, 'mousedown', function( e ) {
if ( ( !W.isIE8 && ( e.target !== darkener ) ) || ( W.isIE8 && ( e.srcElement !== darkener ) ) ) {
return;
}
W.toggleWs( 'out' );
});
}
else if ( direction === 'out' ) {
W.playTO = window.requestAnimationFrame ? window.cancelAnimationFrame( W.playTO ) : window.clearTimeout( W.playTO );
W.goToBeginning();
wsContainer.parentNode.removeChild( wsContainer );
darkener.parentNode.removeChild( darkener );
W.removeEvts( document, 'mousemove', W.mouseMoved );
W.removeEvts( document, 'mouseup', W.slideCallBack );
}
},
showEl: function( el ) {
el.classList.add('show-el');
},
togglePages: function( e ) {
var settings = document.getElementById( 'ws-settings' ),
settingsBtn = document.getElementById( 'ws-settings-b' ),
info = document.getElementById( 'ws-info' ),
infoBtn = document.getElementById( 'ws-info-b' ),
targ = e.target;
if ( targ === settingsBtn ) {
settings.style.zIndex = 20;
info.style.zIndex = 10;
}
if ( targ === infoBtn ) {
settings.style.zIndex = 10;
info.style.zIndex = 20;
}
if ( !W.isIE9Minus && targ === settingsBtn ) {
if ( info.className === 'ws-info ws-info-in' ) {
info.className = 'ws-info';
window.setTimeout( function() {
if ( info.className === 'ws-info' ) {
settings.className = 'ws-settings ws-settings-in';
}
}, 250 );
return;
}
settings.className = settings.className === 'ws-settings' ? 'ws-settings ws-settings-in' : 'ws-settings';
return;
}
if ( !W.isIE9Minus && targ === infoBtn ) {
if ( settings.className === 'ws-settings ws-settings-in' ) {
settings.className = 'ws-settings';
window.setTimeout( function() {
if ( settings.className === 'ws-settings' ) {
info.className = 'ws-info ws-info-in';
}
}, 250 );
return;
}
info.className = info.className === 'ws-info' ? 'ws-info ws-info-in' : 'ws-info';
return;
}
},
getSliderScale: function() {
W.numSliderNotches = W.progressBarLen - document.getElementById( 'ws-progress-knob' ).offsetWidth + 1;
var scaleDirection = ( W.numWsWords < W.numSliderNotches ) ? 'shrink' : 'expand',
baseScale,
maxWordsAtBaseScale,
scaleCorrections;
if ( scaleDirection === 'expand' ) {
baseScale = Math.ceil( W.numWsWords / W.numSliderNotches );
maxWordsAtBaseScale = W.numSliderNotches * baseScale;
scaleCorrections = maxWordsAtBaseScale - W.numWsWords;
correctionInterval = Math.floor( W.numSliderNotches / scaleCorrections );
}
else if ( scaleDirection === 'shrink' ) {
baseScale = Math.ceil( W.numSliderNotches / W.numWsWords );
maxWordsAtBaseScale = W.numWsWords * baseScale;
scaleCorrections = maxWordsAtBaseScale - W.numSliderNotches;
correctionInterval = Math.floor( ( W.numWsWords / scaleCorrections ) );
}
W.sliderScale = {
scaleDirection: scaleDirection,
baseScale: baseScale,
scaleCorrections: scaleCorrections,
correctionInterval: correctionInterval
};
W.setWsScalePos();
},
setWsScalePos: function() {
var correctionHere = [],
notch = 0,
count = 0,
pace;
var setPace = function( toBeChecked ) {
if ( toBeChecked === correctionHere[ 0 ] && ( typeof correctionHere[ 0 ] !== 'undefined' ) ) {
pace = W.sliderScale.baseScale - 1;
correctionHere.splice( 0, 1 );
}
else {
pace = W.sliderScale.baseScale;
}
};
if ( W.numWsWords === W.numSliderNotches ) {
for ( var wordIndex = 0; ( typeof wW.wsWords[ wordIndex ] !== 'undefined' ); wordIndex++ ) {
W.wsWords[ wordIndex ].sliderNotch = wordIndex;
}
}
else if ( W.sliderScale.scaleDirection === 'expand' ) {
for ( var correctionTest = 1; correctionTest <= W.sliderScale.scaleCorrections; correctionTest++ ) {
correctionHere.push( Math.floor( ( correctionTest / W.sliderScale.scaleCorrections ) * W.numSliderNotches ) );
}
setPace( notch );
for ( var wordIndex = 0; ( typeof W.wsWords[ wordIndex ] !== 'undefined' ); wordIndex++ ) {
if ( count < pace ) {
W.wsWords[ wordIndex ].sliderNotch = notch;
count++;
}
else {
notch++;
count = 0;
setPace( notch );
W.wsWords[ wordIndex ].sliderNotch = notch;
count++;
}
}
}
else {
for ( var correctionTest = 1; correctionTest <= W.sliderScale.scaleCorrections; correctionTest++ ) {
correctionHere.push( Math.floor( ( correctionTest / W.sliderScale.scaleCorrections ) * W.numWsWords ) );
}
setPace( 0 );
for ( var wordIndex = 0; typeof W.wsWords[ wordIndex ] !== 'undefined'; wordIndex++ ) {
W.wsWords[ wordIndex ].sliderNotch = [];
for ( count; count < pace; count ++) {
if ( notch <= W.numSliderNotches ) {
W.wsWords[ wordIndex ].sliderNotch.push( notch );
notch++;
}
else {
break;
}
}
count = 0;
setPace( wordIndex + 1 );
}
}
},
goToBeginning: function() {
var play = document.getElementById('ws-play');
W.pauseWs();
W.currWsWord = 0;
W.displayWords();
W.updateKnobPos();
},
playWs: function() {
var currTime = new Date().getTime();
if ( ( ( currTime - W.playWsLastTime ) >= ( W.msPerWord - 8 )
|| ( W.playWsLastTime === 0 && window.requestAnimationFrame ) ) ) {
W.currWsWord = W.currWsWord + aS.numDisplayWords;
W.playWsLastTime = currTime;
W.setPlayEvts( 'play' );
W.togglePlayIcon('play');
W.displayWords();
if ( W.wsWords[ W.currWsWord + W.wordCounter ] ) {
W.updateKnobPos();
}
else {
W.currWsWord = W.wsWords.length - 1;
W.updateKnobPos();
W.pauseWs();
W.playWsLastTime = 0;
return;
}
}
W.playTO = window.requestAnimationFrame( W.playWs );
},
displayWords: function() {
var wordCtnr = document.getElementById( 'ws-word-cell' ),
lastWordsIndex = W.numWsWords - aS.numDisplayWords,
newWordText = '',
newWord,