xkcd
nice alternative to http://wonderfl.net/c/6w9l hack
/**
* Copyright makc3d ( http://wonderfl.net/user/makc3d )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/7VUh
*/
// forked from wh0's jsonp client
package {
import flash.events.*;
import flash.display.*;
import flash.net.*;
import com.actionscriptbible.*;
import com.bit101.components.*;
public class FlashTest extends Example {
public function FlashTest() {
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, function (e:UncaughtErrorEvent):void { trace (e.error); });
var ur:URLRequest = new URLRequest('http://dynamic.xkcd.com/api-0/jsonp/comic/?callback=GIF89a');
hax(ur, function (response:String):void {
var json:String = response.substr(7, response.length - 10);
// SyntaxError: Error #1132
// var data:* = JSON.parse(json);
var loader:Loader = new Loader;
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, showAll);
loader.load (new URLRequest (json.match(/\"img\"\:\"([^\"]+)\"/) [1]));
});
}
private function showAll (e:Event):void {
var info:LoaderInfo = e.target as LoaderInfo;
info.removeEventListener (Event.COMPLETE, showAll);
var s:Number = Math.min (1, stage.stageWidth / info.loader.width, stage.stageHeight / info.loader.height);
info.loader.scaleX = s;
info.loader.scaleY = s;
addChild (info.loader);
}
}
}
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.utils.ByteArray;
internal function hax(request:URLRequest, callback:Function):void {
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, function (e:Event):void {
// nb: the data comes wrapped in a SWF file
var swf:ByteArray = e.target.bytes;
swf.position = 48;
callback(swf.readUTFBytes(e.target.bytesLoaded));
});
l.load(request);
}