summaryrefslogtreecommitdiff
path: root/src/webchannel-iframe.html
blob: f20e783ae8fed8731630c51e5610a54fbb68bf12 (plain)
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
<html>
    <head>
        <script>
            function exec(data, callback, type)
            {
                type = type || 'EXEC';
                var xhr = new XMLHttpRequest;
                xhr.open("POST", "../" + type, true);
                xhr.setRequestHeader("Content-Length", data.length);
                xhr.onreadystatechange = function() {
                    if (xhr.readyState != 4 || xhr.status != 200)
                        return;
                    callback(xhr.responseText);
                };
                xhr.send(data);
            }

            function poll(id, callback)
            {
                exec(id, function(response) {
                    callback(response);
                    setTimeout(function() { poll(id, callback); }, 0);
                }, 'SUBSCRIBE');
            }

            window.addEventListener("message", function(event) {
                var data = JSON.parse(event.data);
                function callback(r) { window.parent.postMessage(JSON.stringify({ type: "callback", id: data.id, payload: r }), "*"); }
                if (data.type == "EXEC")
                    exec(data.payload, callback);
                else if (data.type == "SUBSCRIBE")
                    poll(data.id, callback);
            });
        </script>
    </head>
    <body onload="window.parent.postMessage('11111')">
    </body>
</html>