browser/bookmarklet/proxy.html
author Patrick Walton <pwalton@mozilla.com>
Wed, 29 Sep 2010 16:17:05 -0700
changeset 2052 24cebc9a07d5c66eeb6008573e41eafbcdcb2459
parent 1950 8a5abc03ee5bc5c0ba3cf7d78996da52884e297c
permissions -rw-r--r--
Bug 572115 - Introduce a "specialmodules" setting, which will cause modules that match that regex to be highlighted in a different color

<html>
<body>
    <script>
        var workerList = [];

        function postXhrResult(id, success, data) {
            window.parent.postMessage(JSON.stringify({
                type: 'xhr',
                id: id,
                success: success,
                data:data
            }), '*');
        };

        function postWorkerResult(id, success, evt) {
            var msg;
            if (success) {
                msg = evt.data;
            } else {
                msg = {
                    filename: evt.filename,
                    lineno: evt.lineno
                };
            }

            window.parent.postMessage(JSON.stringify({
                type: 'worker',
                id: id,
                success: success,
                msg: msg
            }), '*');
        };

        function getWorker(obj) {
            var worker = workerList[obj.id];
            if (worker === undefined) {
                throw new Error('PROXY ERROR: There is no worker ' + obj.id);
            }
            return worker;
        }

        window.addEventListener("message", function(e) {
            var obj = JSON.parse(e.data);
            switch(obj.task) {
                case 'xhr':
                    var req = new XMLHttpRequest();
                    req.onreadystatechange = function() {
                        if (req.readyState !== 4) {
                            return;
                        }

                        var status = req.status;
                        if (status !== 0 && status !== 200) {
                            postXhrResult(id, false, req.responseText + ' (Status ' + req.status + ")");
                            return;
                        }

                        postXhrResult(obj.id, true, req.responseText);
                    };
                    req.open(obj.method, obj.url, true);
                    req.send();
                    break;

                case 'newWorker':
                    var worker = new Worker(obj.url);
                    worker.onmessage = function(evt) {
                        postWorkerResult(obj.id, true, evt);
                    };
                    worker.onerror = function(evt) {
                        postWorkerResult(obj.id, false, evt);
                    };
                    workerList[obj.id] = worker;
                    break;

                case 'postWorker':
                    var worker = getWorker(obj);
                    worker.postMessage(obj.msg);
                    break;

                case 'terminateWorker':
                    var worker = getWorker(obj);
                    worker.terminate();
                    delete workerList[obj.id];
                    break;
            };
        }, false);
    </script>
</body>
</html>