Fix some indentations, and remove more dead code
[qt-labs:milianws-qwebchannel.git] / src / script-communicator.js
1 /*
2  * Implementation of script communication that:
3  *  - uses script tags for communication, but can detect when a script isn't loaded (this is non-trivial to implement across browsers)
4  *  - works across domains as long as you control the domains
5  *  - works on IE 6, IE 7, IE 8, FF X, Safari, Chrome and Opera
6  *  - small (80 lines of code) with no dependencies
7  *
8  * For more info and usage check out:
9  *    http://amix.dk/blog/post/19489#ScriptCommunicator-implementing-comet-long-polling-for-all-browse
10  *
11  * Made by amix the lucky stiff - amix.dk
12  * Copyright Plurk 2010, released under BSD license
13  */
14 ScriptCommunicator = {
15
16     callback_called: false,
17
18     /*
19      * Important:
20      * The JavaScript you source must do some kind of call back into your code
21      * and this call back has to set ScriptCommunicator.callback_called = true
22      * for this to work!
23      */
24     sourceJavaScript: function(uri, on_success, on_error) {
25         ScriptCommunicator.callback_called = false;
26
27         ScriptCommunicator._onSuccess = on_success;
28         ScriptCommunicator._onError = on_error;
29
30         var loaded_text = 'if(!ScriptCommunicator.callback_called) {' +
31                               'ScriptCommunicator.onError();'+
32                           '}'+
33                           'else { ' +
34                                'ScriptCommunicator.onSuccess();'+
35                           '}';
36
37         var agent = navigator.userAgent.toLowerCase();
38
39         if(agent.indexOf("khtml") != -1) { //Safari
40             document.writeln('<script type="text/javascript" src="'+uri+'" class="temp_script"><\/script>');
41             document.writeln('<script type="text/javascript" class="temp_script">'+ loaded_text +'<\/script>');
42         }
43         else {
44             var script_channel = document.createElement('script');
45             script_channel.src = uri;
46             script_channel.type = "text/javascript";
47             script_channel.className = 'temp_script';
48
49             var loaded = null;
50             if(agent.indexOf("msie") != -1) { //IE
51                 script_channel.onreadystatechange = ScriptCommunicator.onSuccess;
52             }
53             else if(agent.indexOf('firefox/4.0')) {
54                 script_channel.onload = function() {
55                     eval(loaded_text);
56                 }
57             }
58             else {
59                 var loaded = document.createElement('script');
60                 loaded.type = "text/javascript";
61                 loaded.className = 'temp_script';
62                 loaded.text = loaded_text;
63             }
64
65             var body = document.getElementsByTagName('body')[0];
66             body.appendChild(script_channel);
67             if(loaded)
68                 body.appendChild(loaded);
69         }
70     },
71
72     onSuccess: function() {
73         if(this.readyState == 'loaded' && !ScriptCommunicator.callback_called) {
74             return ScriptCommunicator.onError();
75         }
76
77         if(!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
78             return ScriptCommunicator._onSuccess();
79         }
80     },
81
82     onError: function() {
83         return ScriptCommunicator._onError();
84     }
85
86 }