Why does ExternalInterface breaks when I pass parameter with JSON like string?

前端 未结 2 1392
逝去的感伤
逝去的感伤 2021-02-13 10:55

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

2条回答
  •  深忆病人
    2021-02-13 11:35

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

    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.

    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) { ""; }
    

    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) { ""; }
    

    As you can see this is invalid javascript. It will throws an error in your Javascript console and it will never call console.log.

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

    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.

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

    Flash :

    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.

提交回复
热议问题