Я возился с ExternalInterface
, потому что я думал, что, возможно, есть лучший способ получить доступ к консоли, чем мой предыдущий метод, и я обнаружил, что первый параметр ExternalInterface.call
может указывать больше, чем просто вызываемая функция.
Я написал эту функцию для упрощения использования JavaScript console
и trace
до:
Console.log(...);
Или в блоке условной компиляции:
CONFIG::DEBUG {
Console.log(...);
}
/**
* @author zzzzBov <zzzzbov@gmail.com>
* @version 0.1
* @since 2012-02-24
*/
package com.zzzzbov.debug {
import flash.external.ExternalInterface;
/**
* `com.zzzzbov.debug.Console` is a debugging tool to interface with web browsers' developer consoles.
*/
public class Console {
/**
* The hidden utility method to make `ExternalInterface` calls to JavaScript.
*
* @param type the type of console function that will be called
* @param args the array of arguments to pass to the console[type] function
*/
private static function console(type:String, args:Array = null):void {
//prevents an additional `null` from being passed as an argument when the second parameter is skipped
if (args === null) {
args = [];
}
//Checks that the ExternalInterface is available and that console[type] exists
if (ExternalInterface.available && ExternalInterface.call('function () {return !!(console && ("' + type + '" in console));}')) {
//calls the console[type] function with the provided arguments.
ExternalInterface.call.apply(ExternalInterface, ['console.'+type].concat(args));
} else {
//calls `trace` if console[type] isn't available
trace.apply(null, [type].concat(args));
}
}
/**
* Checks the truthiness of {@param expr}; if the assertion fails, it also prints out {@param args}.
*
* @param expr an expression to test
* @param args the message to print to the console
*/
public static function assert(expr:*, ... args):void {
console('assert', [expr].concat(args));
}
/**
* Clears the console of messages
*/
public static function clear():void {
console('clear');
}
/**
* Writes the number of times that count was executed. The {@param title} will be printed in addition to the count.
*
* Warning: No IE support
*
* @param title A title for a particular counter
*/
public static function count(title:String = null):void {
console('count', title != null ? [title] : []);
}
/**
* Writes the arguments to the console.
*
* Warning: No IE support.
*
* @param args the arguments to print in the console
*/
public static function debug(... args):void {
console('debug', args);
}
/**
* Prints an interactive listing of all properties on {@param obj}.
*
* @param obj the object to expand
*/
public static function dir(obj:*):void {
console('dir', [obj]);
}
/**
* Prints an XML source tree of an HTML or XML element.
*
* Warning: No IE support; source tree may not be fully supported.
*/
public static function dirxml(node:*):void {
console('dirxml', [node]);
}
/**
* Writes the arguments to the console as an error message.
*
* @param args the arguments to print in the console
*/
public static function error(... args):void {
console('error', args);
}
/**
* Writes a message to the console and opens a nested block for future console messages.
*
* Warning: No IE support. Call {@link #groupEnd()} to close the block.
*
* @param args the arguments to print in the console
*/
public static function group(... args):void {
console('group', args);
}
/**
* Writes a message to the console and opens a collapsed, nested block for future console messages.
*
* Warning: No IE support. Call {@link #groupEnd()} to close the block.
*
* @param args the arguments to print in the console
*/
public static function groupCollapsed(... args):void {
console('groupCollapsed', args);
}
/**
* Closes the block most recently opened by {@link #group(...)} or {@link #groupCollapsed(...)}.
*
* Warning: No IE support.
*/
public static function groupEnd():void {
console('groupEnd');
}
/**
* Writes the arguments to the console as an informational message.
*
* @param args the arguments to print in the console
*/
public static function info(... args):void {
console('info', args);
}
/**
* Writes the arguments to the console.
*
* The first argument may use a printf-like syntax for substituting subsequent arguments into a formatted message.
*
* @param args the arguments to print in the console
*/
public static function log(... args):void {
console('log', args);
}
/**
* Turns on the JavaScript profiler.
*
* Call {@link #profileEnd()} to stop profiling and print the profiler report.
*
* @param title the title to print in the header of the profile report
*/
public static function profile(title:String = null):void {
console('profile', title != null ? [title] : []);
}
/**
* Turns off the JavaScript profiler and prints the profiler report.
*/
public static function profileEnd():void {
console('profileEnd');
}
/**
* Creates a new timer with the given name.
*
* Warning: No IE support. Call {@link #timeEnd(name)} to stop the timer and print the elapsed time.
*
* @param name the name of the timer to start
*/
public static function time(name:String):void {
console('time', [name]);
}
/**
* Stops the timer with the provided name and prints the elapsed time to the console.
*
* Warning: No IE support. Call {@link #time(name)} to start a timer of a given name.
*
* @param name the name of the timer to stop
*/
public static function timeEnd(name:String):void {
console('timeEnd', [name]);
}
/**
* Writes the arguments to the console as a warning.
*
* @param args the arguments to print in the console
*/
public static function warn(... args):void {
console('warn', args);
}
}
}
Сообщите мне, если вы обнаружите какие-либо проблемы или у вас есть какие-либо предложения.