/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var pattern:RegExp = /(http[s]?:\/\/[\S]+)\s/ig; txt.addEventListener(Event.CHANGE, function(event:Event):void{ var results:Array = txt.text.match(pattern); if(results.length > 0) { for(var i:int=0; i< results.length; i++){ if(results[i].indexOf("bit.ly")==-1){ shortenURL(results[i], handler) } } } private function shortedURL(url:String , handler:Function):void{ var bitlyURL:String = "http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl="+escape(url)+"&login="+LOGIN+"&apiKey="+API_KEY; var urlLoader:URLLoader = new URLLoader(); var req:URLRequest = new URLRequest(bitlyURL); statusLabel.text = "Shortening ..." urlLoader.addEventListener(Event.COMPLETE, function(event:Event):void{ try{ var shortURL:String = XML(event.target.data).results.nodeKeyVal.shortUrl; }catch(e:Error){} }); urlLoader.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void{ }); urlLoader.load(req); }