|
1 <!doctype html> |
|
2 <html> |
|
3 <script type="application/javascript;version=1.8"> |
|
4 var img; |
|
5 var audio; |
|
6 var iframe; |
|
7 |
|
8 addEventListener("message", function(e) { |
|
9 mess = JSON.parse(e.data); |
|
10 |
|
11 if ("img" in mess) |
|
12 img.src = mess.img; |
|
13 else if ("audio" in mess) |
|
14 audio.src = mess.audio |
|
15 else if ("iframe" in mess) |
|
16 iframe.src = mess.iframe; |
|
17 else if ("xhr" in mess) { |
|
18 let xhr = new XMLHttpRequest(); |
|
19 xhr.onload = function() { |
|
20 sendItUp({ text: xhr.responseText }); |
|
21 } |
|
22 try { |
|
23 xhr.open("GET", mess.xhr); |
|
24 xhr.send(); |
|
25 } |
|
26 catch (ex) { |
|
27 sendItUp({ didThrow: true }); |
|
28 } |
|
29 } |
|
30 |
|
31 }, false); |
|
32 |
|
33 function sendItUp(obj) { |
|
34 window.parent.postMessage(JSON.stringify(obj), "*"); |
|
35 } |
|
36 |
|
37 function audioNotifyParent(e) { |
|
38 sendItUp({ type: e.type }); |
|
39 } |
|
40 |
|
41 function imgNotifyParent(e) { |
|
42 sendItUp({ type: e.type, |
|
43 width: e.target.width, |
|
44 height: e.target.height }); |
|
45 } |
|
46 |
|
47 function iframeNotifyParent(e) { |
|
48 res = { type: e.type }; |
|
49 try { |
|
50 res.text = e.target.contentDocument.getElementsByTagName("p")[0].textContent; |
|
51 } catch (ex) {} |
|
52 try { |
|
53 res.imgWidth = e.target.contentDocument.getElementById("img").width; |
|
54 } catch (ex) {} |
|
55 |
|
56 sendItUp(res); |
|
57 } |
|
58 |
|
59 onload = function() { |
|
60 img = document.getElementById('img'); |
|
61 img.onerror = img.onload = imgNotifyParent; |
|
62 iframe = document.getElementById('iframe'); |
|
63 iframe.onerror = iframe.onload = iframeNotifyParent; |
|
64 audio = document.getElementById('audio'); |
|
65 audio.onerror = audio.onloadeddata = audioNotifyParent; |
|
66 } |
|
67 |
|
68 </script> |
|
69 <body> |
|
70 <img id=img> |
|
71 <audio id=audio> |
|
72 <iframe id=iframe></iframe> |
|
73 </html> |