robot.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2010 Ephox Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. (function() {
  14. if (window.robot) {
  15. // Don't load if there's already a version of robot loaded.
  16. return;
  17. }
  18. window.robot = {
  19. onload: function(userCallback) {
  20. if (this.ready) {
  21. userCallback();
  22. } else {
  23. this.userCallback = userCallback;
  24. }
  25. },
  26. init: function() {
  27. var jarUrl = "JSRobot.jar";
  28. var scripts = document.getElementsByTagName('script');
  29. for (var i = 0; i < scripts.length; i++) {
  30. var src = scripts[i].src;
  31. var regex = /^(.*\/?)robot.js(\?|$)/;
  32. if (src && regex.test(src)) {
  33. jarUrl = regex.exec(src)[1] + "JSRobot.jar";
  34. }
  35. }
  36. document.write('<applet archive="' + jarUrl + '" code="com.ephox.jsrobot.JSRobot" id="robotApplet" width="10" height="10" mayscript="true"><param name="mayscript" value="true" /></applet>');
  37. this.appletInstance = document.getElementById('robotApplet');
  38. },
  39. callback: function() {
  40. this.ready = true;
  41. if (this.userCallback) {
  42. this.userCallback();
  43. }
  44. },
  45. type: function(key, shiftKey, callback) {
  46. shiftKey = !!shiftKey;
  47. this.appletAction(this.getApplet().typeKey(this.getKeycode(key), shiftKey), callback);
  48. },
  49. forwardDelete: function(callback) {
  50. this.type(0x7F, false, callback);
  51. },
  52. cut: function(callback) {
  53. this.typeAsShortcut('x', callback);
  54. },
  55. copy: function(callback) {
  56. this.typeAsShortcut('c', callback);
  57. },
  58. paste: function(callback) {
  59. this.typeAsShortcut('v', callback);
  60. },
  61. typeAsShortcut: function(key, callback) {
  62. this.appletAction(this.getApplet().typeAsShortcut(this.getKeycode(key)), callback);
  63. },
  64. getKeycode: function(key) {
  65. if (key.toUpperCase && key.charCodeAt) {
  66. if (/^[a-zA-Z\n\b\t]$/.test(key)) {
  67. return key.toUpperCase().charCodeAt(0);
  68. } else {
  69. throw { message: 'Invalid character to type. Must be a-z or A-Z, otherwise use the key code directly.' };
  70. }
  71. }
  72. return key;
  73. },
  74. captureScreenShot: function() {
  75. return this.getApplet().captureScreenShot();
  76. },
  77. setScreenShotDirectory: function(dir) {
  78. this.getApplet().setScreenShotDirectory(dir);
  79. },
  80. getApplet: function() {
  81. return this.appletInstance;
  82. },
  83. appletAction: function(actionResult, callback) {
  84. if (actionResult) {
  85. throw { message: "JSRobot error: " + actionResult };
  86. }
  87. setTimeout(callback, 100);
  88. }
  89. };
  90. window.robot.init();
  91. })();