-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-PageReader.js
391 lines (344 loc) · 13.1 KB
/
MMM-PageReader.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
/*
* File: MMM-PageReader.js
* Created: 24/03/2019 by Daniel Burr <[email protected]>
* Description: Main file for MMM-PageReader
* License: GNU Public License, version 3
*/
Module.register("MMM-PageReader", {
defaults: {
highlight: 'background-color:red;', // CSS to be applied to highlighted sentences
timeout: 1000, // amount of time (in ms) to wait before moving to the next sentence. If set to 0, waits for a PAGE_READER_NEXT event
notification: null, // if defined, a notification with this name (and payload containing text) will be sent for each sentence
geometry: {
width: "100%", // width of page reading window (px or %)
height: "100%", // height of page reading window (px or %)
left: "0", // X position of page reading window (px)
top: "0", // Y position of page reading window (px)
},
html: {
tags: [ 'p', 'h1', 'h2', 'h3', 'h4', 'li' ], // list of tags to parse sentences from
regions: (url) => { // a list of query selectors to parse sentences from
return null // parse all regions
},
transform: (url, doc) => { // custom HTML transformation rule to be applied after loading
},
}
},
getStyles: function() {
return ['MMM-PageReader.css']
},
start: function() {
Log.info(`Starting module: ${this.name}`)
this.config = this.configAssignment({}, this.defaults, this.config)
this.reset()
},
reset: function() {
this.paused = false
this.current_span_index = 0
this.spans = null
this.timeout = null
},
notificationReceived: function(notification, payload, sender) {
switch(notification) {
case "DOM_OBJECTS_CREATED":
this.prepareReadingWindow()
this.prepareMessageDialog()
break
case "PAGE_READER_LOAD":
this.dialog_box.style.display = "block"
this.setDialogMsg("Loading " + payload)
this.sendSocketNotification("PROXY_URL", payload)
break
case "PAGE_READER_NEXT":
this.nextSentence(payload? payload: 1)
break
case "PAGE_READER_PREVIOUS":
this.prevSentence(payload? payload: 1)
break
case "PAGE_READER_STOP":
this.closeWindow()
break
case "PAGE_READER_PAUSE":
if(this.timeout) clearTimeout(this.timeout)
this.paused = true
break
case "PAGE_READER_RESUME":
if(this.paused) {
this.paused = false
this.nextSentence()
}
break
}
},
getDom: function() {
return document.createElement("div")
},
socketNotificationReceived: function(notification, payload) {
if(notification == "PROXY") {
if(payload.proxy) { // success!
this.displayWindow(payload.orig, payload.proxy)
} else {
// update message and autoclose after 2 seconds
this.setDialogMsg("Failed to load URL " + payload.orig)
setTimeout(()=>{ this.closeWindow() }, 2000)
}
}
},
displayWindow: function(url_orig, url_proxied) {
var self = this
var iframe = this.page_reader_iframe
iframe.src = url_proxied
iframe.onload = function() {
var doc = iframe.contentDocument || iframe.contentWindow.document
// add style to apply as highlight
var style = document.createElement('style')
style.type = 'text/css'
style.innerHTML = 'span.highlight {' + self.config.highlight + '}'
doc.head.appendChild(style)
self.setDialogMsg("Applying HTML transformations")
self.applyTransformations(url_orig, doc)
self.setDialogMsg("Parsing sentences")
self.parseRegions(url_orig, doc)
// get results of parse
self.current_span_index = 0
self.spans = doc.querySelectorAll('span.MMM-wrapped-text')
if(self.spans.length > 0) {
// start highlighting
self.highlightSentence()
// hide dialog box
self.dialog_box.style.display = 'none'
} else {
self.setDialogMsg("Found no sentences to read!")
setTimeout(()=>{ self.closeWindow() }, 2000)
}
}
this.page_reader.style.display = "block"
},
/*
* prepareReadingWindow
*
* Creates the following elements:
* > Main window for the reader (this.page_reader, id="PAGE_READER")
* > IFrame for the content (this.page_reader_iframe, id="PAGE_READER_IFRAME")
*/
prepareReadingWindow: function() {
this.page_reader = document.createElement("div")
this.page_reader.id = "PAGE_READER"
this.page_reader.style.display = "none"
this.page_reader.style.width = this.config.geometry.width
this.page_reader.style.height = this.config.geometry.height
this.page_reader.style.top = this.config.geometry.top
this.page_reader.style.left = this.config.geometry.left
this.page_reader_iframe = document.createElement("iframe")
this.page_reader_iframe.id = "PAGE_READER_IFRAME"
this.page_reader_iframe.scrolling = "no"
this.page_reader.appendChild(this.page_reader_iframe)
document.getElementsByTagName('body')[0].appendChild(this.page_reader)
},
/*
* prepareMessageDialog
*
* Creates the following elements:
* > Modal dialog box (this.dialog_box, id="PAGE_READER_DIALOG")
* > Message displayed in dialog box (this.dialog_msg, id="PAGE_READER_DIALOG_MSG")
*/
prepareMessageDialog: function() {
this.dialog_box = document.createElement("div")
this.dialog_box.id = "PAGE_READER_DIALOG"
this.dialog_box.className = "modal fade"
this.dialog_box.style.display = "none"
var div = document.createElement("div")
div.className = "modal-dialog modal-sm"
this.dialog_box.appendChild(div)
var modal_content = document.createElement("div")
modal_content.className = "modal-content"
div.appendChild(modal_content)
var modal_body = document.createElement("div")
modal_body.className = "modal-body"
modal_content.appendChild(modal_body)
this.dialog_msg = document.createElement("p")
this.dialog_msg.id = "PAGE_READER_DIALOG_MSG"
modal_body.appendChild(this.dialog_msg)
var loader = document.createElement("div")
loader.className = "loader"
modal_body.appendChild(loader)
document.getElementsByTagName('body')[0].appendChild(this.dialog_box)
},
closeWindow: function() {
if(this.timeout) clearTimeout(this.timeout)
// hide page reader and dialog box
this.page_reader.style.display = "none"
this.dialog_box.style.display = "none"
// reset iframe
this.page_reader_iframe.src = ''
this.page_reader_iframe.onload = null
this.reset()
},
setDialogMsg: function(text) {
this.dialog_msg.innerHTML = text
this.log(text)
},
/*
* applyTransformations
*
* Apply transformation to HTML
*/
applyTransformations: function(url, doc) {
// change any <img>s pointing to window.location to the 'url' host
var target = new URL(url)
doc.querySelectorAll('img').forEach(img => {
if(img.src) {
var obj = new URL(img.src)
if(obj.protocol == window.location.protocol && obj.host == window.location.host) {
obj.protocol = target.protocol
obj.hostname = target.hostname
obj.port = target.port || '80'
img.src = obj.href
}
}
})
// execute (optional) HTML transformation
if(this.config.html.transform && typeof this.config.html.transform == "function") {
this.setDialogMsg("Applying HTML transformation")
try {
this.config.html.transform(url, doc)
} catch(e) {
this.log("Transform failed: " + e)
}
}
},
/*
* parseRegions
*
* Determine the list of regions to parse and call parseSentences()
* for each region
*/
parseRegions: function(url, doc) {
// determine list of regions to parse
var regions = []
if(this.config.html.regions && typeof this.config.html.regions == "function") {
try {
regions = this.config.html.regions(url)
} catch(e) {
this.log("Failed to get regions: " + e)
}
}
if(regions.length == 0) regions.push('')
// parse sentences
regions.forEach(region => {
this.config.html.tags.forEach(tag => {
var num = this.parseSentences(doc.querySelectorAll(`${region} ${tag}`))
this.log(`Search for '${region} ${tag}' returned ${num} sentences`)
})
})
},
/*
* parseSentences
*
* Split each node into sentences and wrap each sentence in <span> nodes
* of class 'MMM-wrapped-text'
*/
parseSentences: function(nodes) {
var count = 0
for(var i = 0; i < nodes.length; i++) {
var text = nodes[i].textContent.trim()
var result = []
function add(sentence) {
result += "<span class='MMM-wrapped-text'>" + sentence + "</span>"
count++
}
while(1) {
var s = text.match(/[^\.?!]*[\.?!]["”']?/)
if(!s) {
if(text.trim().length > 0) add(text)
break
} else {
add(s[0])
text = text.substr(s[0].length, text.length)
}
}
nodes[i].innerHTML = result
}
return count
},
/*
* highlightSentence
*
* Process current sentence (this.spans[this.current_span_index]) and set
* up timer if requested
*/
highlightSentence: function() {
if(this.current_span_index >= this.spans.length) {
this.closeWindow()
return
}
this.spans[this.current_span_index].setAttribute("class", "highlight")
var pos = this.getPositionOfElement(this.spans[this.current_span_index])
this.page_reader_iframe.contentWindow.scrollTo(0, pos.y)
if(this.config.notification) {
var text = this.spans[this.current_span_index].innerHTML
text = this.unencodeHTML(text)
this.sendNotification(this.config.notification, text)
}
if(this.config.timeout > 0) {
this.timeout = setTimeout(()=>{
this.nextSentence()
}, this.config.timeout)
}
},
nextSentence: function(step = 1) {
if(this.paused) return
this.spans[this.current_span_index].removeAttribute("class")
this.current_span_index += step
this.highlightSentence()
},
prevSentence: function(step = 1) {
this.spans[this.current_span_index].removeAttribute("class")
this.current_span_index -= step
if(this.current_span_index < 0) this.current_span_index = 0
this.highlightSentence()
},
getPositionOfElement: function(elem) {
var w = elem.offsetWidth
var h = elem.offsetHeight
for(var lx = 0, ly = 0; elem != null; lx += elem.offsetLeft, ly += elem.offsetTop, elem = elem.offsetParent) {}
return {x: lx,
y: ly,
w: w,
h: h}
},
configAssignment : function (result) {
var stack = Array.prototype.slice.call(arguments, 1)
var item
var key
while(stack.length) {
item = stack.shift()
for (key in item) {
if(item.hasOwnProperty(key)) {
if(typeof result[key] === 'object' && result[key] && Object.prototype.toString.call(result[key]) !== '[object Array]') {
if (typeof item[key] === 'object' && item[key] !== null) {
result[key] = this.configAssignment({}, result[key], item[key])
} else {
result[key] = item[key]
}
} else {
result[key] = item[key]
}
}
}
}
return result
},
unencodeHTML: function(escapedHtml) {
var elem = document.createElement('div')
elem.innerHTML = escapedHtml
var result = ''
for(var i = 0; i < elem.childNodes.length; ++i) {
result = result + elem.childNodes[i].nodeValue
}
return result
},
log: function(msg) {
this.sendSocketNotification("LOG", msg)
}
})