Flash中打开链接绕过弹出窗口阻拦程序的方法

如果设置了弹出窗口阻拦,在Flash中打开链接,会被拦住,这对在Flash中嵌入广告不是一个好消息,好在可以通过iframe hack可以绕过这一限制,昨天折腾了大半天,总算搞定了。至于iframe hack的原理是什么,不需要明白,我们只需要吃鸡蛋,不用管鸡长的什么样子。有几个Flash lib 封装了iframe hack,如收费的flexcapacitor和免费的 flex-iframe

使用flex-iframe之后,解决起来就很简单了:

(1)首先,设置 flash 程序的属性 wmode 为 transparent ,这样flash程序就挡不住iframe层了,否则的话,动一下flash,iframe层就看不见了。

(2)接下来,我们建一个广告的容器,假设是继承自VBox的class Ad,在Ad中放一个IFrame进去,IFrame连向广告页面,这里做演示用,就直接连向www.google.com了:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009"
         xmlns:mx="library://ns.adobe.com/flex/mx"
         width="400" height="300"
         xmlns:code="http://code.google.com/p/flex-iframe/"
         >
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <code:IFrame id="ad0" width="100%" height="160" source="http://www.google.com">
    </code:IFrame>
</mx:VBox>

把它嵌入到界面中,成功运行得到:

image 看起来不错,是吗?试着点击一下,可以直接打开。

问题是,这个iframe在Flash之上,如果Flash弹出一个窗口,则这个广告会拦在新窗口之上,请看:

image

设置Ad的visible是不行的,Ad的visible控制不了外面iframe的visible,iframe的visible需要有IFrame对象来控制,因此,需要第三步。

(3)控制可见性

对Ad类增加方法,控制iframe的可见性:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009"
         xmlns:mx="library://ns.adobe.com/flex/mx"
         width="400" height="300"
         xmlns:code="http://code.google.com/p/flex-iframe/"
         creationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            private static var _show : Boolean = false;
            public static function rotateAds():void
            {
                //TODO:
            }
            public static var instance : Ad;

            public static function show():void
            {
                if(instance == null) return;
                _show = true;
                instance.setFrameVisible(true);
            }
            public static function hide():void
            {
                if(instance == null) return;
                _show = true;
                instance.setFrameVisible(false);
            }

            protected function init():void
            {
                instance = this;
                ad0.visible = _show;
            }
            private function setFrameVisible(val:Boolean):void
            {
                ad0.visible = val;
            }
        ]]>
    </fx:Script>
    <code:IFrame id="ad0" width="100%" height="160" source="http://www.google.com" visible="false">
    </code:IFrame>
</mx:VBox>

当需要显示时show一下,需要隐藏时hide一下即可。当然,您也可以给Ad加一个属性进行绑定。

原文地址:https://www.cnblogs.com/xiaotie/p/1841806.html