forked from Miserlou/Glance-Bookmarklet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spritz.js
313 lines (253 loc) · 8.94 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
// spritz.js
// A JavaScript Speed Reader
// https://github.com/Miserlou/OpenSpritz
// Please don't abuse this.
var readability_token = '172b057cd7cfccf27b60a36f16b1acde12783893';
// Create the view from the remote resource.
function create_spritz(){
spritz_loader = function() {
$.get("https://rawgithub.com/Miserlou/OpenSpritz/master/spritz.html", function(data){
if (!($("#spritz_container").length) ) {
$("body").prepend(data);
}
// I suppose it's better to add that to spritz.html
$('#spritz_selector')
.after('<input type="range" id="spritz_slider" min="1" max="10" value="1">')
.after('<button type="button" id="spritz_toggle">Play</button>');
},'html');
};
load_jq(spritz_loader);
}
// jQuery loader: http://coding.smashingmagazine.com/2010/05/23/make-your-own-bookmarklets-with-jquery/
// This is pretty fucked and should be replaced. Is there anyway we can just force
// the latest jQ? I wouldn't have a problem with that.
function load_jq(spritz_loader){
// the minimum version of jQuery we want
var v = "1.7.0";
// check prior inclusion and version
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
var done = false;
var script = document.createElement("script");
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
script.onload = script.onreadystatechange = function(){
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
spritz_loader();
}
};
document.getElementsByTagName("head")[0].appendChild(script);
} else{
spritz_loader();
}
}
function hide_spritz(){
$('#spritz_spacer').slideUp();
$('#spritz_container').slideUp();
$('#spritz_holder').slideUp();
}
// Entry point to the beef.
// Gets the WPM and the selected text, if any.
function spritz(){
var wpm = parseInt($("#spritz_selector").val(), 10);
if(wpm < 1){
return;
}
var selection = getSelectionText();
if(selection){
spritzify(selection);
}
else{
spritzifyURL();
}
}
// The meat!
function spritzify(input){
var wpm = parseInt($("#spritz_selector").val(), 10);
var ms_per_word = 60000/wpm;
// Split on any spaces.
var all_words = input.split(/\s+/);
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 = false;
var spritz_timers = new Array();
$('#spritz_toggle').click(function() {
if(running) {
stopSpritz();
} else {
startSpritz();
}
});
$('#spritz_slider').change(function() {
updateValues($('#spritz_slider').val() - 1);
});
function updateValues(i) {
$('#spritz_slider').val(i);
var p = pivot(all_words[i]);
$('#spritz_result').html(p);
currentWord = i;
}
function startSpritz() {
$('#spritz_toggle').html('Stop');
running = true;
// Set slider max value
$('#spritz_slider').attr("max", all_words.length);
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]);
}
$('#spritz_toggle').html('Play');
running = false;
}
}
// Find the red-character of the current word.
function pivot(word){
var length = word.length;
// Longer words are "right-weighted" for easier readability.
if(length<6){
var bit = 1;
while(word.length < 22){
if(bit > 0){
word = word + '.';
}
else{
word = '.' + word;
}
bit = bit * -1;
}
var start = '';
var end = '';
if((length % 2) === 0){
start = word.slice(0, word.length/2);
end = word.slice(word.length/2, word.length);
} else{
start = word.slice(0, word.length/2);
end = word.slice(word.length/2, word.length);
}
var result;
result = "<span class='spritz_start'>" + start.slice(0, start.length -1);
result = result + "</span><span class='spritz_pivot'>";
result = result + start.slice(start.length-1, start.length);
result = result + "</span><span class='spritz_end'>";
result = result + end;
result = result + "</span>";
}
else{
var tail = 22 - (word.length + 7);
word = '.......' + word + ('.'.repeat(tail));
var start = word.slice(0, word.length/2);
var end = word.slice(word.length/2, word.length);
var result;
result = "<span class='spritz_start'>" + start.slice(0, start.length -1);
result = result + "</span><span class='spritz_pivot'>";
result = result + start.slice(start.length-1, start.length);
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;
$.getJSON("https://www.readability.com/api/content/v1/parser?url="+ encodeURIComponent(url) +"&token=" + readability_token +"&callback=?",
function (data) {
if(data.error){
$('#spritz_result').html("Article extraction failed. Try selecting text instead.");
return;
}
var title = '';
if(data.title !== ""){
title = data.title + ". ";
}
var author = '';
if(data.author !== null){
author = "By " + data.author + ". ";
}
var body = jQuery(data.content).text(); // Textify HTML content.
body = $.trim(body); // 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 ){
return new Array( num + 1 ).join( this );
}