[ActionScript 3.0] 通过as3操作web内容

package com.controls.utils
{
    import flash.net.*; 
    import flash.utils.ByteArray; 
    import flash.external.ExternalInterface; 
    /**
     * @author Frost.Yen    
     * @E-mail 871979853@qq.com
     * @create 2017-3-6 下午7:44:15
     *
     */
    /** 
     * 统一资源定位符 (Uniform Resource Locator, URL) 完整的URL由这几个部分构成: 
     * scheme://host:port/path?query#fragment 
     * PS:所有获取失败时返回null或"" 
     */ 
    public class WebUtils 
    { 
        private static var regWebkit:RegExp = new RegExp("(webkit)[ \\/]([\\w.]+)","i"); 
        /** 
         * 整个URl字符串 EX.:返回值:http://www.test.com:80/view.html?id=123#start 
         */ 
        public static function get Url():String 
        { 
            return getUrlParams("url"); 
        } 
        /** 
         * 整个URl字符串 EX.:返回值:http://www.test.com:80/view.html?id=123#start 
         */ 
        public static function get Href():String 
        { 
            return getUrlParams("href"); 
        } 
        
        /** 
         * 获取URL中的锚点(信息片断) EX.:返回值:#start 
         */ 
        public static function get Hash():String 
        { 
            return getUrlParams("hash"); 
        } 
        
        /** 
         * URL 的端口部分。如果采用默认的80端口(PS:即使手动添加了:80),那么返回值并不是默认的80而是空字符。 
         */ 
        public static function get Port():String 
        { 
            return (getUrlParams("port") ? getUrlParams("port") : "80"); 
        } 
        
        /** 
         * URL 的路径部分(就是文件地址) EX.:返回值:/view.html 
         */ 
        public static function get PathAndName():String 
        { 
            return getUrlParams("PathAndName"); 
        } 
        /** 
         * URL 的路径部分(就是文件地址) EX.:返回值:/view.html 
         */ 
        public static function get Pathname():String 
        { 
            return getUrlParams("pathname"); 
        } 
        
        /** 
         * 查询(参数)部分。除了给动态语言赋值以外的参数 EX.:返回值:?id=123 
         */ 
        public static function get Search():String 
        { 
            return getUrlParams("search"); 
        } 
        /** 
         * 查询(参数)部分。除了给动态语言赋值以外的参数 EX.:返回值:?id=123 
         */ 
        public static function get QueryString():String 
        { 
            return getUrlParams("query"); 
        } 
        
        /** 
         * URL 的协议部分 EX.:返回值:http:、https:、ftp:、maito:等 
         */ 
        public static function get Protocol():String 
        { 
            return getUrlParams("protocol"); 
        } 
        
        /** 
         * URL 的主机部分,EX.:返回值:www.test.com 
         */ 
        public static function get Host():String 
        { 
            return getUrlParams("host"); 
        } 
        
        public static function Request(param:String):String 
        { 
            var returnValue:String; 
            try 
            { 
                var query:String = QueryString.substr(1); 
                var urlv:URLVariables=new URLVariables(); 
                urlv.decode(query); 
                returnValue = urlv[param]; 
            } 
            catch (error:Object) 
            { 
            } 
            if (returnValue == null) 
            { 
                returnValue = ""; 
            } 
            return returnValue; 
        } 
        
        private static function getUrlParams(param:String):String 
        { 
            var returnValue:String; 
            switch (param) 
            { 
                case "PathAndName" : 
                    returnValue = ExternalInterface.call("function getUrlParams(){return window.location.pathname;}"); 
                    break; 
                case "query" : 
                    returnValue = ExternalInterface.call("function getUrlParams(){return window.location.search;}"); 
                    break; 
                case "url" : 
                    returnValue = ExternalInterface.call("function getUrlParams(){return window.location.href;}"); 
                    break; 
                default : 
                    returnValue = ExternalInterface.call("function getUrlParams(){return window.location." + param + ";}"); 
                    break; 
            } 
            
            return (returnValue ? UrlDecode(returnValue):""); 
        } 
        
        /** 
         * 获取浏览器信息 
         */ 
        public static function get BrowserAgent():String 
        { 
            var returnValue:String = ExternalInterface.call("function BrowserAgent(){return navigator.userAgent;}"); 
            return (returnValue ? returnValue:""); 
        } 
        
        /** 
         * 是否IE浏览器 
         */ 
        public static function get IsIE():Boolean 
        { 
            return (BrowserMatch().browser.toLowerCase() == "msie"); 
        } 
        
        /** 
         * 是否FireFox浏览器 
         */ 
        public static function get IsMozilla():Boolean 
        { 
            return (BrowserMatch().browser.toLowerCase() == "mozilla"); 
        } 
        
        /** 
         * 是否Safari浏览器 
         */ 
        public static function get IsSafari():Boolean 
        { 
            return regWebkit.test(BrowserAgent); 
        } 
        
        /** 
         * 是否Opera浏览器 
         */ 
        public static function get IsOpera():Boolean 
        { 
            return (BrowserMatch().browser.toLowerCase() == "opera"); 
        } 
        
        /** 
         * 获取浏览器类型及对应的版本信息 EX.:BrowserMatch().browser  BrowserMatch().version 
         */ 
        public static function BrowserMatch():Object 
        { 
            var ua:String = BrowserAgent; 
            var ropera:RegExp = new RegExp("(opera)(?:.*version)?[ \\/]([\\w.]+)","i"); 
            var rmsie:RegExp = new RegExp("(msie) ([\\w.]+)","i"); 
            var rmozilla:RegExp = new RegExp("(mozilla)(?:.*? rv:([\\w.]+))?","i"); 
            
            var match:Object = regWebkit.exec(ua) || ropera.exec(ua) || rmsie.exec(ua) || ua.indexOf("compatible") < 0 && rmozilla.exec(ua) || []; 
            
            return { browser: match[1] || "", version: match[2] || "0" }; 
        } 
        
        /** 
         * 获取页面编码方式,EX.:返回值:GB2312、UTF-8等; 
         */ 
        public static function get PageEncoding():String 
        { 
            var returnValue:String = ExternalInterface.call("function PageEncoding(){return window.document.charset;}");//IE 
            if (returnValue == null) 
            { 
                returnValue = ExternalInterface.call("function PageEncoding(){return window.document.characterSet;}"); 
            }//FF 
            //获取成功 
            if (returnValue != null) 
            { 
                returnValue = returnValue.toUpperCase(); 
            } 
            return (returnValue ? returnValue:""); 
        } 
        
        /** 
         * 通过js弹出浏览器提示alert,EX.:Alert("Test"); 
         */ 
        public static function Alert(msg:String):void 
        { 
            navigateToURL(new URLRequest("javascript:alert('"+msg+"');"), "_self"); 
        } 
        
        /** 
         * 通过js的open新窗口打开,(PS:多标签浏览器则新建一个标签打开) 
         */ 
        public static function Open(url:String):void 
        { 
            Eval("javascript:window.open('"+url+"','newwindow')"); 
        } 
        
        /** 
         * URL重定向,使用replace函数,(PS:取消浏览器的前进后退,防止刷新回发数据) 
         */ 
        public static function Redirect(url:String):void 
        { 
            Eval("window.location.replace('"+url + "')"); 
        } 
        
        /** 
         * URL重定向,使用内部navigateToURL函数,(PS:简化了不用每次都new URLRequest的操作) 
         */ 
        public static function NavigateToURL(url:String,target:String="_self"):void 
        { 
            navigateToURL(new URLRequest(url), target); 
        } 
        
        /** 
         * 运行js语句,eval 
         */ 
        public static function Eval(code:String):Object 
        { 
            var rtn:Object = ExternalInterface.call("eval",code + ";void(0);"); 
            return rtn; 
        } 
        
        /** 
         * URL编码,encoding为空时应用统一的UTF-8编码处理,可设"GB2312"、"UTF-8"等,(兼容性处理,对应JS中的escape) 
         */ 
        public static function UrlEncode(str:String,encoding:String = ""):String 
        { 
            if (str == null || str == "") 
            { 
                return ""; 
            } 
            if (encoding == null || encoding == "") 
            { 
                return encodeURI(str); 
            } 
            var returnValue:String = ""; 
            var byte:ByteArray =new ByteArray(); 
            byte.writeMultiByte(str,encoding); 
            for (var i:int; i<byte.length; i++) 
            { 
                returnValue +=  escape(String.fromCharCode(byte[i])); 
            } 
            return returnValue; 
        } 
        
        /** 
         * URL解码,encoding为空时应用统一的UTF-8编码处理,可设"GB2312"、"UTF-8"等,(兼容性处理,对应JS中的unescape) 
         */ 
        public static function UrlDecode(str:String,encoding:String = ""):String 
        { 
            if (str == null || str == "") 
            { 
                return ""; 
            } 
            if (encoding == null || encoding == "") 
            { 
                return decodeURI(str); 
            } 
            var returnValue:String = ""; 
            var byte:ByteArray =new ByteArray(); 
            byte.writeMultiByte(str,encoding); 
            for (var i:int; i<byte.length; i++) 
            { 
                returnValue +=  unescape(String.fromCharCode(byte[i])); 
            } 
            return returnValue; 
        } 
    } 
}

用法测试

 1 package com.view
 2 {
 3     import com.controls.utils.WebUtils;
 4     
 5     import flash.display.*;
 6     import flash.display.Sprite;
 7     import flash.events.*;
 8     import flash.text.TextField;
 9 
10     /**
11      * @author Frost.Yen    
12      * @E-mail 871979853@qq.com
13      * @create 2017-3-6 下午7:42:03
14      *
15      */
16     public class Test extends Sprite
17     {
18         private var _t0:TextField = createText();
19         private var _t1:TextField = createText();
20         private var _t2:TextField = createText();
21         private var _jsAlert:TextField = createButton("Alert");
22         private var _jsOpen:TextField = createButton("Open");
23         private var _jsURL:TextField = createButton("URL");
24         public function Test()
25         {
26             addChildren(_t0,this,100,100);
27             addChildren(_t1,this,100,130);
28             addChildren(_t2,this,100,230);
29             addChildren(_jsAlert,this,100,50);
30             addChildren(_jsOpen,this,150,50);
31             addChildren(_jsURL,this,200,50);
32             this.addChild(_t0);
33             this.addChild(_t1);
34             this.addChild(_t2);
35             this.addChild(_jsAlert);
36             this.addChild(_jsOpen);
37             this.addChild(_jsURL);
38             _t0.appendText(WebUtils.BrowserAgent); 
39             
40             _t1.appendText("是否是IE:"+WebUtils.IsIE.toString()+ "\r\n"); 
41             _t1.appendText("是否是FireFox:"+WebUtils.IsMozilla.toString()+ "\r\n"); 
42             _t1.appendText("是否是Safari:"+WebUtils.IsSafari.toString()+ "\r\n"); 
43             _t1.appendText("是否是Opera:"+WebUtils.IsOpera.toString()+ "\r\n"); 
44             _t1.appendText("浏览器类型:"+WebUtils.BrowserMatch().browser+" 浏览器版本:"+WebUtils.BrowserMatch().version + "\r\n"); 
45             
46             _t2.appendText("获取URL地址:" +WebUtils.Url + "\r\n"); 
47             _t2.appendText("获取所有URL参数:" + WebUtils.QueryString + "\r\n"); 
48             _t2.appendText("获取URL参数id=:" + WebUtils.Request("id") + "\r\n"); 
49             _t2.appendText("获取锚点参数:" + WebUtils.Hash + "\r\n"); 
50             _t2.appendText("cuplayer.com提示获取主机头:" + WebUtils.Host + "\r\n"); 
51             _t2.appendText("获取访问协议:" + WebUtils.Protocol + "\r\n"); 
52             _t2.appendText("获取访问端口:" + WebUtils.Port + "\r\n"); 
53             _t2.appendText("cuplayer.com提示获取页面名称路径:" + WebUtils.PathAndName + "\r\n"); 
54             _t2.appendText("页面编码:" + WebUtils.PageEncoding + "\r\n"); 
55             
56             _jsAlert.addEventListener(MouseEvent.CLICK,btnAlertClick); 
57             _jsOpen.addEventListener(MouseEvent.CLICK,btnOpenClick); 
58             _jsURL.addEventListener(MouseEvent.CLICK,btnUrlClick); 
59         }
60         private function btnAlertClick(evt:MouseEvent):void 
61         { 
62             WebUtils.Alert('cuplayer.com提示弹出JS对话框示例!'); 
63         } 
64         private function btnOpenClick(evt:MouseEvent):void 
65         { 
66             WebUtils.Eval("javascript:window.open('http://www.baidu.com','newwindow')"); 
67         } 
68         private function btnUrlClick(evt:MouseEvent):void 
69         { 
70             WebUtils.Redirect("Test.html?id="+Math.ceil(Math.random()*999999)+"&cid=ok&page="+Math.ceil(Math.random()*10)+"#end"); 
71         } 
72         private function addChildren(child:DisplayObject,parent:DisplayObjectContainer,x:Number=0,y:Number=0):void
73         {
74             parent.addChild(child);
75             child.x = x;
76             child.y = y;
77         }
78         private function createText():TextField
79         {
80             var t:TextField = new TextField();
81             t.autoSize  = "left";
82             //t.wordWrap = true;
83             return t;
84         }
85         private function createButton(label:String):TextField
86         {
87             var t:TextField = new TextField();
88             t.autoSize  = "left";
89             t.text = label;
90             t.border = true;
91             t.borderColor = 0xcccccc;
92             t.background = true;
93             t.backgroundColor = 0xFFFFFF;
94             t.selectable = false;
95             return t;
96         }
97     }
98 }

 

posted on 2017-03-31 21:26  晏过留痕  阅读(765)  评论(0编辑  收藏  举报