-
Notifications
You must be signed in to change notification settings - Fork 1
/
squirt.js
539 lines (467 loc) · 16.2 KB
/
squirt.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
var sq = window.sq;
sq.version = '0.0.1';
sq.host = 'https://rawgit.com/notadice/demo/master/';
(function(){
on('mousemove', function(){
let sq_modal = document.querySelector('.sq .modal')
if (sq_modal) {
sq_modal.style.cursor = 'auto';
}
});
(function makeSquirt(read, makeGUI) {
on('squirt.again', startSquirt);
injectStylesheet(sq.host + 'font-awesome.css');
injectStylesheet(sq.host + 'squirt.css', function stylesLoaded(){
makeGUI();
startSquirt();
});
function startSquirt(){
showGUI();
getText(read);
};
function getText(read){
// text source: demo
if(window.squirtText) return read(window.squirtText);
// text source: selection
var selection = window.getSelection();
if(selection.type == 'Range') {
var container = document.createElement("div");
for (var i = 0, len = selection.rangeCount; i < len; ++i) {
container.appendChild(selection.getRangeAt(i).cloneContents());
}
return read(container.textContent);
}
// text source: readability
var handler;
function readabilityReady(){
handler && document.removeEventListener('readility.ready', handler);
read(readability.grabArticleText());
};
if(window.readability) return readabilityReady();
makeEl('script', {
src: sq.host + 'readability.js'
}, document.head);
handler = on('readability.ready', readabilityReady);
};
})(makeRead(makeTextToNodes(wordToNode)), makeGUI);
function makeRead(textToNodes) {
sq.paused = false;
var nodeIdx,
nodes,
lastNode,
nextNodeTimeoutId;
function incrememntNodeIdx(increment){
var ret = nodeIdx;
nodeIdx += increment || 1;
nodeIdx = Math.max(0, nodeIdx);
prerender();
return ret;
};
var intervalMs, _wpm;
function wpm(wpm){
_wpm = wpm;
intervalMs = 60 * 1000 / wpm ;
};
(function readerEventHandlers(){
on('squirt.close', function(){
sq.closed = true;
clearTimeout(nextNodeTimeoutId);
});
on('squirt.wpm.adjust', function(e){
dispatch('squirt.wpm', {value: e.value + _wpm});
});
on('squirt.wpm', function(e){
sq.wpm = Number(e.value);
wpm(e.value);
dispatch('squirt.wpm.after');
});
on('squirt.pause', pause);
on('squirt.play', play);
on('squirt.play.toggle', function(){
dispatch(sq.paused ? 'squirt.play' : 'squirt.pause');
});
on('squirt.rewind', function(e){
// Rewind by `e.value` seconds. Then walk back to the
// beginning of the sentence.
!sq.paused && clearTimeout(nextNodeTimeoutId);
incrememntNodeIdx(-Math.floor(e.seconds * 1000 / intervalMs));
while(!nodes[nodeIdx].word.match(/\./) && nodeIdx < 0){
incrememntNodeIdx(-1);
}
nextNode(true);
});
})();
function pause(){
sq.paused = true;
dispatch('squirt.pause.after');
clearTimeout(nextNodeTimeoutId);
};
function play(e){
sq.paused = false;
dispatch('squirt.pause.after');
document.querySelector('.sq .wpm-selector').style.display = 'none'
nextNode(e.jumped);
};
var toRender;
function prerender(){
toRender = nodes[nodeIdx];
if(toRender == null) return;
prerenderer.appendChild(toRender);
nodes[nodeIdx].center();
}
function finalWord(){
toggle(document.querySelector('.sq .reader'));
if(window.location.hostname.match('squirt.io|localhost')){
window.location.href = '/install.html';
} else {
showTweetButton(nodes.length,
(nodes.length * intervalMs / 1000 / 60).toFixed(1));
}
toggle(finalWordContainer);
return;
};
var delay, jumped, nextIdx;
function nextNode(jumped) {
lastNode && lastNode.remove();
nextIdx = incrememntNodeIdx();
if(nextIdx >= nodes.length) return finalWord();
lastNode = nodes[nextIdx];
wordContainer.appendChild(lastNode);
lastNode.instructions && invoke(lastNode.instructions);
if(sq.paused) return;
nextNodeTimeoutId = setTimeout(nextNode, intervalMs * getDelay(lastNode, jumped));
};
var waitAfterShortWord = 1.2;
var waitAfterComma = 2;
var waitAfterPeriod = 4;
var waitAfterParagraph = 4.5;
var waitAfterLongWord = 1.5;
function getDelay(node, jumped){
var word = node.word;
if(jumped) return waitAfterPeriod;
if(word == "Mr." ||
word == "Mrs." ||
word == "Ms." ||
word == "U.S.") return 1;
var lastChar = word[word.length - 1];
if(lastChar.match('”|"')) lastChar = word[word.length - 2];
if(lastChar == '\n') return waitAfterParagraph;
if('.!?'.indexOf(lastChar) != -1) return waitAfterPeriod;
if(',;:–'.indexOf(lastChar) != -1) return waitAfterComma;
if(word.length < 4) return waitAfterShortWord;
if(word.length > 11) return waitAfterLongWord;
return 1;
};
function showTweetButton(words, minutes){
var html = "<div>You just read " + words + " words in " + minutes + " minutes!</div>";
var tweetString = "I read " + words + " words in " + minutes + " minutes without breaking a sweat—www.squirt.io turns your browser into a speed reading machine!";
var paramStr = encodeURI("url=squirt.io&user=squirtio&size=large&text=" +
tweetString);
html += '<iframe class=\"tweet-button\" '
+ 'allowtransparency=\"true\" frameborder=\"0\"'
+ ' scrolling=\"no\"'
+ ' src=\"https://platform.twitter.com/widgets/tweet_button.html?'
+ paramStr + '\"'
+ ' style=\"width:120px; height:20px;\"></iframe>';
finalWordContainer.innerHTML = html;
};
function showInstallLink(){
finalWordContainer.innerHTML = "<a class='install' href='/install.html'>Install Squirt</a>";
};
function readabilityFail(){
var modal = document.querySelector('.sq .modal');
modal.innerHTML = '<div class="error">Oops! This page is too hard for Squirt to read. We\'ve been notified, and will do our best to resolve the issue shortly.</div>';
};
dispatch('squirt.wpm', {value: 400});
var wordContainer,
prerenderer,
finalWordContainer;
function initDomRefs(){
wordContainer = document.querySelector('.sq .word-container');
invoke(wordContainer.querySelectorAll('.sq .word'), 'remove');
prerenderer = document.querySelector('.sq .word-prerenderer');
finalWordContainer = document.querySelector('.sq .final-word');
document.querySelector('.sq .reader').style.display = 'block';
document.querySelector('.sq .final-word').style.display = 'none';
};
return function read(text) {
initDomRefs();
if(!text) return readabilityFail();
nodes = textToNodes(text);
nodeIdx = 0;
prerender();
dispatch('squirt.play');
};
};
function makeTextToNodes(wordToNode) {
return function textToNodes(text) {
text = "3\n 2\n 1\n " + text.trim('\n').replace(/\s+\n/g,'\n');
return text
.replace(/ \./g, '.')
.replace(/[\s]\-[\s]/g, ' ')
.replace(/[\,\.\!\:\;](?![\"\'\)\]\}])/g, "$& ")
.split(/[\s]+/g)
.filter(function(word){ return word.length; })
.map(wordToNode);
};
};
var instructionsRE = /#SQ(.*)SQ#/;
function parseSQInstructionsForWord(word, node){
var match = word.match(instructionsRE);
if(match && match.length > 1){
node.instructions = [];
match[1].split('#')
.filter(function(w){ return w.length; })
.map(function(instruction){
var val = Number(instruction.split('=')[1]);
node.instructions.push(function(){
dispatch('squirt.wpm', {value: val})
});
});
return word.replace(instructionsRE, '');
};
return word;
};
// ORP: Optimal Recgonition Point
function getORPIndex(word){
var length = word.length;
var lastChar = word[word.length - 1];
if(lastChar == '\n'){
lastChar = word[word.length - 2];
length--;
}
if(',.?!:;"'.indexOf(lastChar) != -1) length--;
return length <= 1 ? 0 :
(length == 2 ? 1 :
(length == 3 ? 1 :
Math.floor(length / 2) - 1));
};
function wordToNode(word) {
var node = makeDiv({'class': 'word'});
node.word = parseSQInstructionsForWord(word, node);
var orpIdx = getORPIndex(node.word);
node.word.split('').map(function charToNode(char, idx) {
var span = makeEl('span', {}, node);
span.textContent = char;
if(idx == orpIdx) span.classList.add('orp');
});
node.center = (function(orpNode) {
var val = orpNode.offsetLeft + (orpNode.offsetWidth / 2);
node.style.left = "-" + val + "px";
}).bind(null, node.children[orpIdx]);
return node;
};
var disableKeyboardShortcuts;
function showGUI(){
blur();
document.querySelector('.sq').style.display = 'block';
disableKeyboardShortcuts = on('keydown', handleKeypress);
};
function hideGUI(){
unblur();
document.querySelector('.sq').style.display = 'none';
disableKeyboardShortcuts && disableKeyboardShortcuts();
};
var keyHandlers = {
32: dispatch.bind(null, 'squirt.play.toggle'),
27: dispatch.bind(null, 'squirt.close'),
38: dispatch.bind(null, 'squirt.wpm.adjust', {value: 10}),
40: dispatch.bind(null, 'squirt.wpm.adjust', {value: -10}),
37: dispatch.bind(null, 'squirt.rewind', {seconds: 10})
};
function handleKeypress(e){
var handler = keyHandlers[e.keyCode];
handler && (handler(), e.preventDefault())
return false;
};
function blur(){
map(document.body.children, function(node){
if(!node.classList.contains('sq'))
node.classList.add('sq-blur');
});
};
function unblur(){
map(document.body.children, function(node){
node.classList.remove('sq-blur');
});
}
function makeGUI(){
var squirt = makeDiv({class: 'sq'}, document.body);
squirt.style.display = 'none';
on('squirt.close', hideGUI);
var obscure = makeDiv({class: 'sq-obscure'}, squirt);
on(obscure, 'click', function(){
dispatch('squirt.close');
});
var modal = makeDiv({'class': 'modal'}, squirt);
var controls = makeDiv({'class':'controls'}, modal);
var reader = makeDiv({'class': 'reader'}, modal);
var wordContainer = makeDiv({'class': 'word-container'}, reader);
makeDiv({'class': 'focus-indicator-gap'}, wordContainer);
makeDiv({'class': 'word-prerenderer'}, wordContainer);
makeDiv({'class': 'final-word'}, modal);
var keyboard = makeDiv({'class': 'keyboard-shortcuts'}, reader);
keyboard.innerText = "Keys: Space, Esc, Up, Down";
(function make(controls){
// this code is suffering from delirium
(function makeWPMSelect(){
// create the ever-present left-hand side button
var control = makeDiv({'class': 'sq wpm sq control'}, controls);
var wpmLink = makeEl('a', {}, control);
bind("{{wpm}} WPM", sq, wpmLink);
on('squirt.wpm.after', wpmLink.render);
on(control, 'click', function(){
toggle(wpmSelector) ?
dispatch('squirt.pause') :
dispatch('squirt.play');
});
// create the custom selector
var wpmSelector = makeDiv({'class': 'sq wpm-selector'}, controls);
wpmSelector.style.display = 'none';
var plus50OptData = {add: 50, sign: "+"};
var datas = [];
for(var wpm = 200; wpm < 1000; wpm += 100){
var opt = makeDiv({'class': 'sq wpm-option'}, wpmSelector);
var a = makeEl('a', {}, opt);
a.data = { baseWPM: wpm };
a.data.__proto__ = plus50OptData;
datas.push(a.data);
bind("{{wpm}}", a.data, a);
on(opt, 'click', function(e){
dispatch('squirt.wpm', {value: e.target.firstChild.data.wpm});
dispatch('squirt.play');
wpmSelector.style.display = 'none';
});
};
// create the last option for the custom selector
var plus50Opt = makeDiv({'class': 'sq wpm-option sq wpm-plus-50'}, wpmSelector);
var a = makeEl('a', {}, plus50Opt);
bind("{{sign}}50", plus50OptData, a);
on(plus50Opt, 'click', function(){
datas.map(function(data){
data.wpm = data.baseWPM + data.add;
});
var toggle = plus50OptData.sign == '+';
plus50OptData.sign = toggle ? '-' : '+';
plus50OptData.add = toggle ? 0 : 50;
dispatch('squirt.els.render');
});
dispatch('click', {}, plus50Opt);
})();
(function makeRewind(){
var container = makeEl('div', {'class': 'sq rewind sq control'}, controls);
var a = makeEl('a', {}, container);
a.href = '#';
on(container, 'click', function(e){
dispatch('squirt.rewind', {seconds: 10});
e.preventDefault();
});
a.innerHTML = "<i class='fa fa-backward'></i> 10s";
})();
(function makePause(){
var container = makeEl('div', {'class': 'sq pause control'}, controls);
var a = makeEl('a', {'href': '#'}, container);
var pauseIcon = "<i class='fa fa-pause'></i>";
var playIcon = "<i class='fa fa-play'></i>";
function updateIcon(){
a.innerHTML = sq.paused ? playIcon : pauseIcon;
}
on('squirt.pause.after', updateIcon);
on(container, 'click', function(clickEvt){
dispatch('squirt.play.toggle');
clickEvt.preventDefault();
});
updateIcon();
})();
})(controls);
};
// utilites
function map(listLike, f){
listLike = Array.prototype.slice.call(listLike); // for safari
return Array.prototype.map.call(listLike, f);
}
// invoke([f1, f2]); // calls f1() and f2()
// invoke([o1, o2], 'func'); // calls o1.func(), o2.func()
// args are applied to both invocation patterns
function invoke(objs, funcName, args){
args = args || [];
var objsAreFuncs = false;
switch(typeof funcName){
case "object":
args = funcName;
break;
case "undefined":
objsAreFuncs = true;
};
return map(objs, function(o){
return objsAreFuncs ? o.apply(null, args) : o[funcName].apply(o, args);
});
}
function makeEl(type, attrs, parent) {
var el = document.createElement(type);
for(var k in attrs){
if(!attrs.hasOwnProperty(k)) continue;
el.setAttribute(k, attrs[k]);
}
parent && parent.appendChild(el);
return el;
};
// data binding... *cough*
function bind(expr, data, el){
el.render = render.bind(null, expr, data, el);
return on('squirt.els.render', function(){
el.render();
});
};
function render(expr, data, el){
var match, rendered = expr;
expr.match(/{{[^}]+}}/g).map(function(match){
var val = data[match.substr(2, match.length - 4)];
rendered = rendered.replace(match, val == undefined ? '' : val);
});
el.textContent = rendered;
};
function makeDiv(attrs, parent){
return makeEl('div', attrs, parent);
};
function injectStylesheet(url, onLoad){
var el = makeEl('link', {
rel: 'stylesheet',
href: url,
type: 'text/css'
}, document.head);
function loadHandler(){
onLoad();
el.removeEventListener('load', loadHandler)
};
onLoad && on(el, 'load', loadHandler);
};
function on(bus, evts, cb){
if(cb === undefined){
cb = evts;
evts = bus;
bus = document;
}
evts = typeof evts == 'string' ? [evts] : evts;
var removers = evts.map(function(evt){
bus.addEventListener(evt, cb);
return function(){
bus.removeEventListener(evt, cb);
};
});
if(removers.length == 1) return removers[0];
return removers;
};
function dispatch(evt, attrs, dispatcher){
var evt = new Event(evt);
for(var k in attrs){
if(!attrs.hasOwnProperty(k)) continue
evt[k] = attrs[k];
}
(dispatcher || document).dispatchEvent(evt);
};
function toggle(el){
var s = window.getComputedStyle(el);
return (el.style.display = s.display == 'none' ? 'block' : 'none') == 'block';
};
})();