-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreader_raw.js
More file actions
37 lines (31 loc) · 1.27 KB
/
reader_raw.js
File metadata and controls
37 lines (31 loc) · 1.27 KB
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
//console.log("in reader_raw.js for " +self.location.href);
window.addEventListener('message', function(ev) {
//console.log('reader_raw: ev.data = ' + ev.data);
//console.log('reader_raw: ev.origin = ' + ev.origin);
if (typeof ev.data == 'undefined') {
return;
}
// Message data from iframe is 'helo'. iframe is registering itself with us,
// we respond with 'hello'.
//
if (ev.data == 'sgr:helo') {
//console.log('reader_raw: helo received, hello send to ' + ev.origin);
// Loop all iframes in the document and try to match an iframe DOMWindow to the source
// of this message. If we find one, we know we have a matching iframe registering
// with us. We reply with the unique id of the iframe so it can use this in future
// communications.
//
var iframes = document.getElementsByTagName("iframe");
for (var i=0; i < iframes.length; i++) {
var iframe = iframes[i];
//console.log(iframe);
if (iframe.contentWindow == ev.source) {
var iframe_id = iframe.getAttribute("id").substr("sgr_preview_".length);
//console.log("iframe found " + i + " id=" + iframe_id);
if (iframe_id.length == 8) {
ev.source.postMessage('sgr:hello:' + iframe_id, ev.origin);
}
}
}
}
});