ExternalCall.as 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package {
  2. import flash.external.ExternalInterface;
  3. internal class ExternalCall
  4. {
  5. /*public function ExternalCall()
  6. {
  7. }
  8. */
  9. public static function Simple(callback:String):void {
  10. ExternalInterface.call(callback);
  11. }
  12. public static function FileQueued(callback:String, file_object:Object):void {
  13. ExternalInterface.call(callback, EscapeMessage(file_object));
  14. }
  15. public static function FileQueueError(callback:String, error_code:Number, file_object:Object, message:String):void {
  16. ExternalInterface.call(callback, EscapeMessage(file_object), EscapeMessage(error_code), EscapeMessage(message));
  17. }
  18. public static function FileDialogComplete(callback:String, num_files_selected:Number, num_files_queued:Number, total_num_files_queued:Number):void {
  19. ExternalInterface.call(callback, EscapeMessage(num_files_selected), EscapeMessage(num_files_queued), EscapeMessage(total_num_files_queued));
  20. }
  21. public static function UploadStart(callback:String, file_object:Object):void {
  22. ExternalInterface.call(callback, EscapeMessage(file_object));
  23. }
  24. public static function UploadProgress(callback:String, file_object:Object, bytes_loaded:uint, bytes_total:uint):void {
  25. ExternalInterface.call(callback, EscapeMessage(file_object), EscapeMessage(bytes_loaded), EscapeMessage(bytes_total));
  26. }
  27. public static function UploadSuccess(callback:String, file_object:Object, server_data:String, responseReceived:Boolean):void {
  28. ExternalInterface.call(callback, EscapeMessage(file_object), EscapeMessage(server_data), EscapeMessage(responseReceived));
  29. }
  30. public static function UploadError(callback:String, error_code:Number, file_object:Object, message:String):void {
  31. ExternalInterface.call(callback, EscapeMessage(file_object), EscapeMessage(error_code), EscapeMessage(message));
  32. }
  33. public static function UploadComplete(callback:String, file_object:Object):void {
  34. ExternalInterface.call(callback, EscapeMessage(file_object));
  35. }
  36. public static function Debug(callback:String, message:String):void {
  37. ExternalInterface.call(callback, EscapeMessage(message));
  38. }
  39. public static function Bool(callback:String):Boolean {
  40. return ExternalInterface.call(callback);
  41. }
  42. /* Escapes all the backslashes which are not translated correctly in the Flash -> JavaScript Interface
  43. *
  44. * These functions had to be developed because the ExternalInterface has a bug that simply places the
  45. * value a string in quotes (except for a " which is escaped) in a JavaScript string literal which
  46. * is executed by the browser. These often results in improperly escaped string literals if your
  47. * input string has any backslash characters. For example the string:
  48. * "c:\Program Files\uploadtools\"
  49. * is placed in a string literal (with quotes escaped) and becomes:
  50. * var __flash__temp = "\"c:\Program Files\uploadtools\\"";
  51. * This statement will cause errors when executed by the JavaScript interpreter:
  52. * 1) The first \" is succesfully transformed to a "
  53. * 2) \P is translated to P and the \ is lost
  54. * 3) \u is interpreted as a unicode character and causes an error in IE
  55. * 4) \\ is translated to \
  56. * 5) leaving an unescaped " which causes an error
  57. *
  58. * I fixed this by escaping \ characters in all outgoing strings. The above escaped string becomes:
  59. * var __flash__temp = "\"c:\\Program Files\\uploadtools\\\"";
  60. * which contains the correct string literal.
  61. *
  62. * Note: The "var __flash__temp = " portion of the example is part of the ExternalInterface not part of
  63. * my escaping routine.
  64. */
  65. private static function EscapeMessage(message:*):* {
  66. if (message is String) {
  67. message = EscapeString(message);
  68. }
  69. else if (message is Array) {
  70. message = EscapeArray(message);
  71. }
  72. else if (message is Object) {
  73. message = EscapeObject(message);
  74. }
  75. return message;
  76. }
  77. private static function EscapeString(message:String):String {
  78. var replacePattern:RegExp = /\\/g; //new RegExp("/\\/", "g");
  79. return message.replace(replacePattern, "\\\\");
  80. }
  81. private static function EscapeArray(message_array:Array):Array {
  82. var length:uint = message_array.length;
  83. var i:uint = 0;
  84. for (i; i < length; i++) {
  85. message_array[i] = EscapeMessage(message_array[i]);
  86. }
  87. return message_array;
  88. }
  89. private static function EscapeObject(message_obj:Object):Object {
  90. for (var name:String in message_obj) {
  91. message_obj[name] = EscapeMessage(message_obj[name]);
  92. }
  93. return message_obj;
  94. }
  95. }
  96. }