在302重定向或嵌入/对象/iframe标签中的元素后获得flash src(跨域)

Getting flash src after a 302 redirect OR an element inside an embed/object/iframe tag (cross-domain)

本文关键字:元素 flash 跨域 src 重定向 标签 iframe 对象      更新时间:2023-09-26

URL example.com/redir会自动将用户(HTTP 302)重定向到example.com/hi.SWF?message=消息+值。

如何获得消息值,使用javascript或flash,在下面的例子中?

<!DOCTYPE html>
<html>
    <body>
         <embed id="foo" src="https://example.com/redir"></embed>
         <!-- Remember that example.com/redir will be automatically redirected to example.com/hi.SWF?message=Message+Value -->
         <!-- The code to get the message value must go here -->
    </body>
</html>

认为:

  1. 上述.html托管在cross-domain.com中,就像解决方案中涉及的任何其他文件一样(.swf.js.html.css等);
  2. 您无法控制example.com;
  3. 您无法控制hi.SWF;
  4. 您可以将标签更改为 & lt; iframe> ,

简单回答:

你不能那样做,因为同源策略。
用户的cookie将不会在您的请求中携带。

我认为获得重定向swf的url及其参数的最简单方法是使用另一个swf作为当前swf的加载器,在那里您可以获得url和参数传递给重定向swf,然后您可以通过ExternalInterface将其发送到JavaScript。

看一下这个例子:

ActionScript:

    // url           : the url of the current swf
    //                 this url is passed by flash vars in the html page
    // default value : http://www.example.com/redirect
var swf_url:String = this.loaderInfo.parameters.url || 'http://www.example.com/redirect',
    // fn            : the name of the js function which will get the url and the message param
    //                 this name is passed by flash vars in the html page
    // default value : console.log
    js_function:String = this.loaderInfo.parameters.fn || 'console.log',
    message:String = 'no message';
var url_request:URLRequest = new URLRequest(swf_url);
var swf_loader:Loader = new Loader();
    swf_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_content_load);
    swf_loader.load(url_request);
function on_content_load(e:Event): void 
{   
    var loader_info:LoaderInfo = LoaderInfo(e.currentTarget);
    // get the message param or use the default message
    message = loader_info.parameters.message || message;
    // if ExternalInterface is available, send data to js
    if(ExternalInterface.available)
    {
        // send the swf url and the message param to the js function
        ExternalInterface.call(js_function, { url: loader_info.url, message: message }); 
    }
    // show the redirected loaded swf
    addChild(swf_loader);
}

HTML边:

在这个例子中,假设我们有一个.htaccess文件,如下所示:

。htaccess:

Redirect    /redirect   /new.swf?message=a%20message%20here
JavaScript:

function get_data_from_flash(data)
{
    console.log('url : ' + data.url);
    // gives :
    //      url : http://www.example.com/new.swf?message=a%20message%20here
    console.log('message : ' + data.message);
    // gives :
    //      message : a message here
}
HTML:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400">
    <param name="movie" value="loader.swf" />
    <param name="flashvars" value="url=http://www.example.com/redirect&fn=get_data_from_flash" />
    <object type="application/x-shockwave-flash" data="loader.swf" width="550" height="400">
        <param name="flashvars" value="url=http://www.example.com/redirect&fn=get_data_from_flash" />
    </object>
</object>

就这些!

如果你想了解更多关于如何使用ExternalInterface的细节,你可以看看这里。

希望能有所帮助。