source: Dev/branches/rest-dojo-ui/client/util/doh/Robot.html @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 6.5 KB
Line 
1<!DOCTYPE html>
2<html style="margin:0px; padding:0px; border:0px none; overflow:hidden;">
3<head>
4<script>
5        // support document.domain
6        // Yes, we need this try/catch to handle document.domain.
7        // If one page explicitly sets document.domain,
8        // ALL pages (this page, test case, page to be tested) must also explicitly set document.domain,
9        // or you get Permission Denied.
10        // So it's not as easy as just setting document.domain to whatever the browser reports,
11        // because you don't know if the user explicitly set that.
12        try{
13                // Was the passed domain just a stupid browser default?
14                // If so, this statement will work.
15                var test=window.frameElement.ownerDocument;
16        }catch(e){
17                // Permission Denied.
18                // Means user explicitly set document.domain elsewhere.
19                // robot.js passes the document.domain as a GET parameter.
20                var domain=unescape(/[\\?&]domain=([^&#]*)/.exec(window.location.href)[1]);
21                document.domain=domain;
22        }
23        // find doh
24        var doc = window.frameElement.ownerDocument;
25        var w = doc.parentWindow || doc.defaultView || top;
26        doh = w.doh;
27
28        function init(){
29                var applet = document.getElementsByTagName('applet')[0];
30
31                doh.robot._killApplet = function(){
32                        document.body.removeChild(applet);
33                        applet = null;
34                };
35        }
36
37        // the box that the applet writes into to test the user's keyboard capabilities
38        var _robotField = null;
39
40
41        /*
42                Robot uses the event handlers below to measure system-level
43                properties that the java.awt.Robot class just expects us to know
44                magically. For instance, we don't know how big the user set their mouse
45                wheel to be in the Control Panel, so we measure it using onmousewheel.
46        */
47
48        function _onfocus(robotField){
49                _robotField = robotField;
50                doh.robot._initWheel();
51        }
52
53        function _onblur(e){
54                setTimeout(function(){
55                        document.body.removeChild(_robotField);
56                },0);
57        }
58
59        function _onmousedown(e){
60                e = e||window.event;
61                if(e.screenX == 0||e.clientX == 0){ return; }
62                var docScreenX = e.screenX-e.clientX;
63                var docScreenY = e.screenY-e.clientY;
64                doh.robot._setDocumentBounds(docScreenX, docScreenY);
65        }
66
67        function _onmousewheel(e){
68                e = e||window.event;
69                doh.robot.mouseWheelSize = e.detail ? (e.detail * -1) : (e.wheelDelta / 120);
70                if(e.preventDefault){ e.preventDefault(); }
71                else { e.returnValue = false; }
72                doh.robot._primePump = true;
73                doh.robot._initKeyboard();
74                return false;
75        }
76
77        var charCode = 32;
78        var keystring = "";
79        var expectedlength = 0;
80
81        doh.robot._nextKeyGroup = function(size){
82                // called from Robot
83                // moves to the next group of keys to press; shift, alt-graph etc.
84                expectedlength = size;
85                keystring = "";
86        };
87
88        function _onkeypress(e){
89                e = e||window.event;
90                var c = e.charCode ? e.charCode :
91                   (e.keyCode ? e.keyCode :
92                   (e.which ? e.which : 0)); // opera
93                if(doh.robot._primePump){
94                        // event pump was primed with spaces and JS finally received one
95                        // tell robot to stop sending spaces now
96                        if(c == 32){
97                                doh.robot._spaceReceived = true;
98                        }
99                        else if(c == 13){
100                                // enter: start accepting keys
101                                doh.robot._primePump = false;
102                        }
103                }
104                else if(c == 32){
105                        keystring += String.fromCharCode(charCode);
106                        charCode = 32;
107                        if(keystring.length >= expectedlength){
108                                doh.robot._notified(keystring);
109                        }
110                }
111                else if(c > 32){ // printable
112                        charCode = c;
113                }
114                // keyboard discovery misbehaves on Safari/Mac
115                // prevent default action if possible
116                if(e.preventDefault){ e.preventDefault(); }
117                else { e.returnValue = false; }
118                return false;
119        }
120
121        var rf=null;
122        doh.robot._onKeyboard = function(){
123                // called from Robot
124                // Robot calls _onKeyboard after it finishes discovering the keyboard
125                // need to untie from Java thread with setTimeout
126                setTimeout(function(){
127                        rf.style.visibility = "hidden";
128                        doh.robot._run(window.frameElement);
129                }, 0);
130        };
131</script>
132</head>
133<body style="margin:0px; padding:0px; border:0px none;background-color:transparent;" onload="init()">
134<input type="text" tabIndex="-1" style="border:0px none;margin:0px;padding:0px;width:200px;height:42px;background-color:white;z-index:9998;opacity:0;filter:alpha(opacity=0);cursor:default;"
135        onmousewheel="_onmousewheel(arguments[0])"
136        onmousedown="_onmousedown(arguments[0])"
137        onkeypress="_onkeypress(arguments[0])"
138        onfocus="_onfocus(this)"
139        onblur="_onblur(arguments[0])"></input>
140<script>
141        rf = document.getElementsByTagName('input')[0];
142        if(rf.addEventListener){
143                rf.addEventListener('DOMMouseScroll', _onmousewheel, false);
144        }
145</script>
146<img src="robot/signature.png" style="width:3px; height:3px; position:absolute; left:0px; top:0px; z-index:9999"></img>
147<script>
148        // taken from hostenv_browser.js
149        // Dojo is not always available to DOH
150        var needsSecurityManager='<param name="needsSecurityManager" value="'+((
151                (Math.max(navigator.appVersion.indexOf("WebKit"), navigator.appVersion.indexOf("Safari"), 0) > 0 && navigator.userAgent.indexOf("Chrome/") == -1 && parseFloat(navigator.appVersion.split("Version/")[1]) < 4) // Safari 3
152                || (document.all && navigator.userAgent.indexOf("Opera") == -1 && parseFloat(navigator.appVersion.split("MSIE ")[1]) < 8)) // IE6,7
153                ?"true":"false")+'">';
154        var appletString = '<applet width="1" height="1" code="DOHRobot.class" archive="robot/DOHRobot.jar" MAYSCRIPT style="position:absolute;left:0px;top:22px;"><param name="mayscript" value="true"><param name="scriptable" value="true"><param name="initial_focus" value="false">'+needsSecurityManager+'</applet>';
155        document.write(appletString);
156        // if no Java, the applet is officially dead
157        if(/MSIE/.test(navigator.appVersion)){
158                // navigator.javaEnabled() not known for its reliability in IE6
159                try{
160                        // applet loads synchronously in IE6
161                        document.applets[0].isActive();
162                }catch(e){
163                        doh.robot._appletDead=true;
164                        // IE skips the dojo.robot XHR test below in the else
165                        // since the no java behavior is the same as no applet behavior in IE
166                        doh.run();
167                }
168        }else if(!navigator.javaEnabled()){
169                // FF, Safari, Opera etc.
170                // unreliable in IE6
171                doh.robot._appletDead=true;
172                doh.run();
173        }else{
174                // Opera etc. have Java enabled, but load the applet asychronously so it might still be missing from server at this point
175                // Do an XHR to find out if the applet is there
176                // IE is taken care of in the first if, so no need for special ActiveX XHR for it
177                var dohRobotCheck=new XMLHttpRequest();
178                dohRobotCheck.onreadystatechange=function(){
179                        if(dohRobotCheck.readyState == 4 && dohRobotCheck.status == 404){
180                                // oops! no applet
181                                // move the tests along
182                                doh.robot._appletDead=true;
183                                doh.run();
184                        }
185                };
186                dohRobotCheck.open("HEAD", "robot/DOHRobot.jar");
187                dohRobotCheck.send(null);
188        }
189</script>
190</body>
191</html>
Note: See TracBrowser for help on using the repository browser.