ExternalProxy.as
class vzw.data.ExternalProxy {
// Set a reference to flash.external.ExternalInterface. Doing it like this allow us to publish as Flash 6.
public static var externalInterface:Object = _global['flash']['external']['ExternalInterface'];
// Is ExternalInterface really available?
public static var available:Boolean = externalInterface && externalInterface.available && externalInterface.call.apply(ExternalProxy, ["(function(){return !(application_DoFSCommand=null)})"]);
// Call a function, with an optional callback.
public static function call(methodName:String, args:Array, callback:Function):Void {
if (externalInterface && available) {
// Add the Method to the ExternalInterface arguments array so we can apply it.
callback(externalInterface.call.apply(ExternalProxy, [methodName].concat(args)));
} else {
var _length:Number = args.length, i:Number = 0;
for (; i<_length; i++) {
args[i] = escape(args[i]);
}
args = [escape(args.join(','))];
if (callback) {
// Generate a random variable name. We'll pass this to Javascript, which will then
// set the result using SetVariable. We'll watch for that to change.
var variable:String = '__fv_'+Math.ceil(Number(new Date())*Math.random())+'__';
// Add the random variable name to the args array.
args.push(variable);
// Watch for the random variable to change and then execute the callback function.
// "watch" has been removed in AS3. Is this the best way of doing this?
_root.watch(variable, function (id:Number, __value:Object, _value:Object):Object {
//_root.unwatch(variable);
callback(_value);
return _value;
});
}
// Call the fscommand, with the serialized args array.
fscommand(methodName, args.join(','));
}
}
public static function addCallback(methodName:String, callback:Function):Void {
if (externalInterface && available) {
externalInterface.addCallback(methodName, ExternalProxy, callback);
} else {
_root.watch(methodName, function (id:Number, __value:Object, _value:Object):Void {
//_root.unwatch(methodName);
callback(_value);
});
}
}
}fscommand.js (eval)
// Catch the fscommand (Flash 7 and lower).
var application_DoFSCommand = function(command, parameters) {
//alert('application_DoFSCommand("'+command+'", "'+parameters+'");');
// Convert the parameters string to an array.
var params = parameters.split(',');
// Get the unique variable created by ExternalProxy.
var setVariableProxy = (params.length > 1) ? params.pop() : null;
// The actual parameters are a double-encoded array.
var args = unescape(params[0]).split(',');
var length = args.length, i = 0;
for (; i<length; i++) {
// Process each argument.
args[i] = unescape(args[i]).replace(/\"/g, '\\"');
}
// Format the string to evaluate.
var fscommandEvalString = (command+'("'+args.join('", "')+'");');
//alert(fscommandEvalString);
// Evaluate the string.
var evalStringResult = eval(fscommandEvalString);
// Set the ExternalProxy variable as the result of the eval'd string.
if (setVariableProxy) {
var flashMovieObject = document.getElementById('application');
flashMovieObject.SetVariable(setVariableProxy, String(evalStringResult));
//alert(fscommandEvalString+'\nflashMovieObject.SetVariable("'+setVariableProxy+'", '+((evalStringResult) ? '"'+evalStringResult+'"' : null)+');');
}
};fscommand.js (apply)
// Catch the fscommand (Flash 7 and lower).
var application_DoFSCommand = function(command, parameters) {
//alert('application_DoFSCommand("'+command+'", "'+parameters+'");');
// Convert the parameters string to an array.
var params = parameters.split(',');
// Get the unique variable created by ExternalProxy.
var setVariableProxy = (params.length > 1) ? params.pop() : null;
// The actual parameters are a double-encoded array.
var args = unescape(params[0]).split(',');
var length = args.length, i = 0;
for (; i<length; i++) {
// Process each argument.
args[i] = unescape(args[i]).replace(/\"/g, '\\"');
}
// Evaluate the string.
var evalStringResult = window[command].apply(window, args);
// Set the ExternalProxy variable as the result of the eval'd string.
if (setVariableProxy) {
var flashMovieObject = document.getElementById('application');
flashMovieObject.SetVariable(setVariableProxy, String(evalStringResult));
//alert(fscommandEvalString+'\nflashMovieObject.SetVariable("'+setVariableProxy+'", '+((evalStringResult) ? '"'+evalStringResult+'"' : null)+');');
}
};