Created
October 23, 2012 09:10
-
-
Save RhinoLu/3937783 to your computer and use it in GitHub Desktop.
Google Url Shortener 縮短網址
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.external.ExternalInterface; | |
/** | |
* @author http://www.facebook.com/rhino.lu | |
* @link https://developers.google.com/url-shortener/v1/getting_started | |
* html要嵌入 <script src="https://apis.google.com/js/client.js"></script> | |
* | |
* | |
*/ | |
public class GoogleShortener | |
{ | |
private static const script_js:XML = | |
<script> | |
<![CDATA[ | |
function() { | |
MyJS = { }; | |
MyJS.setSWFObjectID = function( swfObjectID ) { | |
MyJS.swfObjectID = swfObjectID; | |
} | |
MyJS.getSwf = function getSwf() { | |
return document.getElementById( MyJS.swfObjectID ); | |
} | |
MyJS.init = function(api_key, callback) { | |
gapi.client.setApiKey(api_key); | |
gapi.client.load("urlshortener", "v1", MyJS.getSwf()[callback]); | |
} | |
MyJS.getShortURL = function(long, callback) { | |
var request = gapi.client.urlshortener.url.insert({ | |
"resource": { "longUrl" : long} | |
}); | |
request.execute(function(response) { | |
MyJS.getSwf()[callback](response); | |
}); | |
} | |
MyJS.expandShortURL = function(short, callback) { | |
var request = gapi.client.urlshortener.url.get({ | |
"shortUrl": short | |
}); | |
request.execute(function(response) { | |
MyJS.getSwf()[callback](response); | |
}); | |
} | |
} | |
]]> | |
</script>; | |
public function GoogleShortener() | |
{ | |
} | |
public static function init(API_KEY:String, CALL_BACK:Function):void | |
{ | |
if (ExternalInterface.available) { | |
ExternalInterface.call( script_js ); | |
ExternalInterface.call( "MyJS.setSWFObjectID", ExternalInterface.objectID ); | |
ExternalInterface.addCallback( "onInit", CALL_BACK ); | |
ExternalInterface.call( "MyJS.init", API_KEY, "onInit" ); | |
} | |
} | |
public static function getShortURL( LONG_URL:String, CALL_BACK:Function):void | |
{ | |
ExternalInterface.addCallback("onDone", CALL_BACK); | |
ExternalInterface.call( "MyJS.getShortURL", LONG_URL, "onDone" ); | |
} | |
public static function expandShortURL( SHORT_URL:String, CALL_BACK:Function):void | |
{ | |
ExternalInterface.addCallback("onDone", CALL_BACK); | |
ExternalInterface.call( "MyJS.expandShortURL", SHORT_URL, "onDone" ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import com.bit101.components.InputText; | |
import com.bit101.components.PushButton; | |
import com.bit101.components.TextArea; | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.events.MouseEvent; | |
public class Main extends Sprite | |
{ | |
private var btn1:PushButton; | |
private var btn2:PushButton; | |
private var input1:InputText; | |
private var input2:InputText; | |
private var txt:TextArea; | |
public function Main():void | |
{ | |
if (stage) init(); | |
else addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
private function init(e:Event = null):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
btn1 = new PushButton(this, 20, 20, "get short" , onClick); | |
btn2 = new PushButton(this, 20, 50, "expand short", onClick); | |
btn1.enabled = btn2.enabled = false; | |
input1 = new InputText(this, 130, 20, "http://lab.letsplay.com.tw/rhinolu/shortener/"); | |
input1.width = 250; | |
input1.height = 20; | |
input2 = new InputText(this, 130, 50, "http://goo.gl/fbsS"); | |
input2.width = 100; | |
input2.height = 20; | |
txt = new TextArea(this, 20, 100); | |
txt.editable = false; | |
txt.width = 360; | |
txt.height = 300; | |
GoogleShortener.init(YOUR_API_KEY, onInit); | |
} | |
private function onClick(e:MouseEvent):void | |
{ | |
var btn:PushButton = e.target as PushButton; | |
if (btn == btn1) { | |
GoogleShortener.getShortURL(input1.text, onGetShortComplete); | |
}else if (btn == btn2) { | |
GoogleShortener.expandShortURL(input2.text, onExpandShortComplete); | |
} | |
} | |
private function onInit():void | |
{ | |
btn1.enabled = btn2.enabled = true; | |
txt.text = "Init complete!"; | |
} | |
private function onGetShortComplete(obj:*):void | |
{ | |
txt.text = t.obj(obj); | |
} | |
private function onExpandShortComplete(obj:*):void | |
{ | |
txt.text = t.obj(obj); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment