flash on 2010-11-16

by quqjp
インポート
♥0 | Line 77 | Modified 2010-11-16 15:55:43 | MIT License
play

ActionScript3 source code

/**
 * Copyright quqjp ( http://wonderfl.net/user/quqjp )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/39ci
 */

package {
    
    /**
     * インポート
     */
    import flash.events.Event;
    import flash.events.FocusEvent;
    import flash.events.IEventDispatcher;
    import flash.external.ExternalInterface;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.utils.setInterval;
    import flash.utils.setTimeout;
    import flash.xml.XMLDocument;
    import flash.system.Security;
    
    /**
     * 
     */
    public class FlashTest extends Sprite {
        
        //ユニークID
        protected var flashid:String = "abcd12345";
        
        //入力フィールド
        protected var inputtf:TextField = new TextField();
        
        /**
         * コンストラクタ
         */
        public function FlashTest() {
            this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
        }
        
        /**
         * 
         */
        protected function onAdded(e:Event):void{
                    
            IEventDispatcher(e.target).removeEventListener(e.type, arguments.callee);
            
            try{
                ExternalInterface.call("alert","a");
                Security.allowDomain(ExternalInterface.call("function() { return location.hostname }"));
            }catch(e:Error){
                
            }

            //
            inputtf.text = "aaaaaa";
            inputtf.width = 100;
            inputtf.height = 100;
            inputtf.x = 100;
            inputtf.y = 100;
            inputtf.border = true;
            inputtf.type = TextFieldType.INPUT;
            inputtf.addEventListener(FocusEvent.FOCUS_IN, tfCatchFucus);
            this.addChild(inputtf);
            
            var xmldata:XML = new XML(<xml><![CDATA[
                    
                function() {
                            
                    document.myflashid = '##flashide##';
                    document.swftarget;
                    document.connectSWF = function(){
                        $("object").each(function(){
                        try{
                            if (this.connect(document.myflashid)) {
                                document.swftarget = this;
                            }
                        }catch(e){}                        
                        });        
                    }    
                            
                    document.openPrompt = function(defaultstr) {
                        var result = window.prompt("入力してください。", defaultstr);
                        if (result != defaultstr) {
                            document.swftarget.update(result);
                        }
                    }
                        
                }
                    
            ]]></xml>);
                    
            var xmlstr:String = String(xmldata);
            xmlstr = xmlstr.split('##flashide##').join(this.flashid);
            ExternalInterface.call(xmlstr);
            ExternalInterface.addCallback("connect", onConnectFromJS);
            ExternalInterface.addCallback("update", update);
            ExternalInterface.call("document.connectSWF");
                    
        }    
            
        /**
         * JSからのConnect 
         */
        protected function onConnectFromJS(flashid:String):Boolean{
            if (this.flashid == flashid) {
                return true;
            }else {
                return false;
            }
        }
        
        /**
         * TextFieldのフォーカスの取得
         */
        protected function tfCatchFucus(e:FocusEvent):void{
            ExternalInterface.call("document.openPrompt", [inputtf.text]);
        }
        
        /**
         * 更新
         */
        protected function update(str:String):void{
            inputtf.text = str;
        }
        
    }
}