-
Notifications
You must be signed in to change notification settings - Fork 247
/
spritz.js
332 lines (266 loc) · 9.62 KB
/
spritz.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
// spritz.js
// A JavaScript Speed Reader
// rich@gun.io
// https://github.com/Miserlou/OpenSpritz
// Please don't abuse this.
var readability_token = '172b057cd7cfccf27b60a36f16b1acde12783893';
var diffbot_token = '2efef432c72b5a923408e04353c39a7c';
function create_spritz(){
spritz_loader = function() {
//getURL("https://rawgithub.com/Miserlou/OpenSpritz/master/spritz.html", function(data){
//getURL("https://rawgithub.com/Miserlou/OpenSpritz/dev/spritz.html", function(data){
// This won't work in Firefox because an old bug and won't work in Chrome because of security stuff:
//getURL("spritz.html", function(data){
//getURL("https://rawgithub.com/Miserlou/OpenSpritz/dev/spritz.html", function(data){
// RawGit's CDN usage:
// "Since files are not refreshed after the first request,
// it's best to use a specific tag or commit URL, not a branch URL."
getURL("https://cdn.rawgit.com/Miserlou/OpenSpritz/9e92c605032be16c986ed699d68e0acd3534e6b1/spritz.html", function(data){
var spritzContainer = document.getElementById("spritz_container");
if (!spritzContainer) {
var ele = document.createElement("div");
data = data.replace(/(\r\n|\n|\r)/gm,"");
ele.innerHTML = data;
document.body.insertBefore(ele, document.body.firstChild);
document.getElementById("spritz_toggle").style.display = "none";
};
document.getElementById("spritz_selector").addEventListener("change", function(e) {
clearTimeouts();
spritz();
});
});
};
spritz_loader();
}
function getURL(url, callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function hide_spritz(){
document.getElementById("spritz_spacer").style.display = "none";
document.getElementById("spritz_container").style.display = "none";
document.getElementById("spritz_holder").style.display = "none";
}
// Entry point to the beef.
// Gets the WPM and the selected text, if any.
function spritz(){
var wpm = parseInt(document.getElementById("spritz_selector").value, 10);
if(wpm < 1){
return;
}
var selection = getSelectionText();
if(selection){
spritzify(selection);
}
else{
spritzifyURL();
}
}
// The meat!
function spritzify(input){
var wpm = parseInt(document.getElementById("spritz_selector").value, 10);
var ms_per_word = 60000/wpm;
// Split on any spaces.
var all_words = input.split(/\s+/);
// The reader won't stop if the selection starts or ends with spaces
if (all_words[0] == "")
{
all_words = all_words.slice(1, all_words.length);
}
if (all_words[all_words.length - 1] == "")
{
all_words = all_words.slice(0, all_words.length - 1);
}
var word = '';
var result = '';
// Preprocess words
var temp_words = all_words.slice(0); // copy Array
var t = 0;
for (var i=0; i<all_words.length; i++){
if(all_words[i].indexOf('.') != -1){
temp_words[t] = all_words[i].replace('.', '•');
}
// Double up on long words and words with commas.
if((all_words[i].indexOf(',') != -1 || all_words[i].indexOf(':') != -1 || all_words[i].indexOf('-') != -1 || all_words[i].indexOf('(') != -1|| all_words[i].length > 8) && all_words[i].indexOf('.') == -1){
temp_words.splice(t+1, 0, all_words[i]);
temp_words.splice(t+1, 0, all_words[i]);
t++;
t++;
}
// Add an additional space after punctuation.
if(all_words[i].indexOf('.') != -1 || all_words[i].indexOf('!') != -1 || all_words[i].indexOf('?') != -1 || all_words[i].indexOf(':') != -1 || all_words[i].indexOf(';') != -1|| all_words[i].indexOf(')') != -1){
temp_words.splice(t+1, 0, " ");
temp_words.splice(t+1, 0, " ");
temp_words.splice(t+1, 0, " ");
t++;
t++;
t++;
}
t++;
}
all_words = temp_words.slice(0);
var currentWord = 0;
var running = true;
var spritz_timers = new Array();
document.getElementById("spritz_toggle").addEventListener("click", function() {
if(running) {
stopSpritz();
} else {
startSpritz();
}
});
function updateValues(i) {
var p = pivot(all_words[i]);
document.getElementById("spritz_result").innerHTML = p;
currentWord = i;
}
function startSpritz() {
document.getElementById("spritz_toggle").style.display = "block";
document.getElementById("spritz_toggle").textContent = "Pause";
running = true;
spritz_timers.push(setInterval(function() {
updateValues(currentWord);
currentWord++;
if(currentWord >= all_words.length) {
currentWord = 0;
stopSpritz();
}
}, ms_per_word));
}
function stopSpritz() {
for(var i = 0; i < spritz_timers.length; i++) {
clearTimeout(spritz_timers[i]);
}
document.getElementById("spritz_toggle").textContent = "Play";
running = false;
}
startSpritz();
}
// Find the red-character of the current word.
function pivot(word){
var length = word.length;
var bestLetter = 1;
switch (length) {
case 1:
bestLetter = 1; // first
break;
case 2:
case 3:
case 4:
case 5:
bestLetter = 2; // second
break;
case 6:
case 7:
case 8:
case 9:
bestLetter = 3; // third
break;
case 10:
case 11:
case 12:
case 13:
bestLetter = 4; // fourth
break;
default:
bestLetter = 5; // fifth
};
word = decodeEntities(word);
var start = '.'.repeat((11-bestLetter)) + word.slice(0, bestLetter-1).replace('.', '•');
var middle = word.slice(bestLetter-1,bestLetter).replace('.', '•');
var end = word.slice(bestLetter, length).replace('.', '•') + '.'.repeat((11-(word.length-bestLetter)));
var result;
result = "<span class='spritz_start'>" + start;
result = result + "</span><span class='spritz_pivot'>";
result = result + middle;
result = result + "</span><span class='spritz_end'>";
result = result + end;
result = result + "</span>";
result = result.replace(/\./g, "<span class='invisible'>.</span>");
return result;
}
// Get the currently selected text, if any.
// Shameless pinched from StackOverflow.
function getSelectionText() {
var text = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
text = container.innerText || container.textContent;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
text = document.selection.createRange().text;
}
}
if(text === ""){
return false;
}
else{
return text;
}
}
// Uses the Readability API to get the juicy content of the current page.
function spritzifyURL(){
var url = document.URL;
//getURL("https://www.readability.com/api/content/v1/parser?url="+ encodeURIComponent(url) +"&token=" + readability_token +"&callback=?",
getURL("https://api.diffbot.com/v2/article?url="+ encodeURIComponent(url) +"&token=" + diffbot_token, // +"&callback=?",
function(data) {
data = JSON.parse(data);
if(data.error){
document.getElementById("spritz_result").innerText = "Article extraction failed. Try selecting text instead.";
return;
}
var title = '';
if(data.title !== ""){
title = data.title + ". ";
}
var author = '';
if(data.author !== undefined){
author = "By " + data.author + ". ";
}
var body = data.text;
body = body.trim(); // Trim trailing and leading whitespace.
body = body.replace(/\s+/g, ' '); // Shrink long whitespaces.
var text_content = title + author + body;
text_content = text_content.replace(/\./g, '. '); // Make sure punctuation is apprpriately spaced.
text_content = text_content.replace(/\?/g, '? ');
text_content = text_content.replace(/\!/g, '! ');
spritzify(text_content);
});
}
//////
// Helpers
//////
// This is a hack using the fact that browers sequentially id the timers.
function clearTimeouts(){
var id = window.setTimeout(function() {}, 0);
while (id--) {
window.clearTimeout(id);
}
}
// Let strings repeat themselves,
// because JavaScript isn't as awesome as Python.
String.prototype.repeat = function( num ){
if(num < 1){
return new Array( Math.abs(num) + 1 ).join( this );
}
return new Array( num + 1 ).join( this );
};
function decodeEntities(s){
var str, temp= document.createElement('p');
temp.innerHTML= s;
str= temp.textContent || temp.innerText;
temp=null;
return str;
}