为什么ExternalInterface的休息,当我通过参数使用JSON字符串一样?当我、字符串、参数、ExternalInterface

2023-09-08 13:20:49 作者:久念成疾,久病成医

我有一个很奇怪的问题与Flash 10和ExternalInterface的。我目前使用的是自制的桥梁使用RTMFP使用JavaScript,每当我试图通过一个包含JSON数据,我得到一个奇怪的JavaScript错误来自闪存:

I have a very odd problem with Flash 10 and ExternalInterface. I am currently using a homemade bridge to use RTMFP with Javascript and whenever I try to pass data that contains JSON, I get a wierd Javascript error that comes from Flash :

missing ) after argument list
try { __flash__toXML(Flash.Utilities.A..."")) ; } catch (e) { "<undefined/>"; }

这是不可能获得更多的信息,因为这来自于Flash和它没有绑定到任何JavaScript文件。

It's impossible to get more information since this come from Flash and it's not bound to any Javascript file.

要重现此问题,您可以使用此脚本:

To reproduce this problem you can use this script :

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.external.ExternalInterface;

    public class Main extends Sprite 
    {

        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);
            var test:String;

            test = "\"\\\"\"";

            ExternalInterface.call("console.log", test);
        }

    }

}

我能做些什么来避免这个问题,它是一个已知的问题?

What can I do to avoid this problem and is it a known problem ?

推荐答案

这显然是一个已知的问题,它似乎并不像Adobe将会很快解决它。

This is apparently a known issue and it doesn't seems like Adobe is going to fix it anytime soon.

我发现这个问题的相关详细交代并basicly这个问题似乎是,闪存没有按' T柄的 \ &安培; 正确从而导致JavaScript错误或数据损坏,从Flash转移到中JavaScript的。

I found a detailled explaination of this problem and basicly the issue seems to be that Flash doesn't handle the \ and the & properly which can lead to javascript error or data corruption during the transfer from Flash to javascript.

什么闪光时,尝试从闪存数据传输到JS做的是执行以下的事情:

What Flash attempts to do when you transfer data from Flash to JS is to execute the following thing :

try {
    __flash__toXML(yourJavascriptFunction("[DATA]")) ;
} catch (e) { "<undefined/>"; }

现在的问题是,它使得你的数据的原始,它并没有逃脱反斜线的。如果字符串仅包含 \ ,你想叫的console.log 就可以了,它会尽量做到以下几点东西:

The problem is that it puts your data raw and it doesn't escape the backslash at all. If your string contains only \ and you want to call console.log on it, it will try to do the following thing :

try {
    __flash__toXML(console.log("\")) ;
} catch (e) { "<undefined/>"; }

正如你可以看到这是无效的JavaScript。它会引发错误在JavaScript控制台,它绝不会叫的console.log

该解决方案是ducktape闪存行为或做一些讨厌的黑客绕过它。

The solution is to either ducktape Flash behavior or do some nasty hack to get around it.

要ducktape闪存buggyness你能逃脱blackslash,然后他们转移。该解决方案将适用于现在,但在Flash将修复它(最有可能不是在不久的将来,如果考虑到这个错误是著名的4年以上),它会破坏你的应用程序。

To ducktape Flash buggyness you can escape the blackslash before you transfer them. This solution will work for now, but when Flash will fix it (most probably not in a near future, if you consider that this bug is known for more than 4 years), it will break your application.

另一种可能是给url EN code中的字符Flash不处理好( \ &安培; )和脱code他们在另一边

The other possibility is to url encode the character that Flash doesn't handle well (\, ", &) and to decode them on the other side.

闪存:

data = data.split("%").join("%25")
           .split("\\").join("%5c")
           .split("\"").join("%22")
           .split("&").join("%26");

JavaScript的:

data = data.replace(/%22/g, "\"")
           .replace(/%5c/g, "\\")
           .replace(/%26/g, "&")
           .replace(/%25/g, "%");

这是丑陋的,但它的作品。

It's ugly, but it works.