Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 Flash - render html via ExternalInterface

This is my first question on Stack Overflow. It's not the first time I wanted to write one, but usually I find a solution using the search bar, this time I didn't. The problem I'm tackling is a little bit complex so I'll try and be as thorough as possibile.

Basically we're adding Chinese payments to an already existing ecommerce in Flash. The whole website is in AS3, embedded using SWFObject, already using ExternalInterface for other things.

This new Chinese payment method is a bit old-fashioned, so they have a strange way of handling payments. Once we've sent a POST to their servers with all the order details, they respond with an HTML page. My problem is rendering this page, considering that I receive it inside Flash.

The solution I'm trying at the moment works partially, meaning that I'm able to see the page, but the Chinese characters that are supposed to be in the page are rendering out badly. Instead of the Chinese characters I'm seeing weird characters, so I'm guessing there must be an encoding problem when I pass the HTML from Flash to Javascript. This is how I'm doing it:

AS3:

//extract html page from response
var newHTML:String = e.currentTarget.data;
//trim whitespace to avoid javascript error
newHTML = newHTML.replace(/\n/g, '');
newHTML = newHTML.split("\r").join("");

if(ExternalInterface.available)
  ExternalInterface.call("chinesePayment('"+newHTML+"')");
else
  trace("External interface error");

Javascript:

function chinesePayment(param) {
  var newWindow = window.open();
  //var unescaped = unescape(param);
  newWindow.document.write(param);
}

I've tried messing about with unescape, escape, URIencoding but without any success, so I'm really hoping you can help me out here!

Thanks, Domenico

EDIT:

I'd just like to mention that I'm receiving a correct HTML page from their servers. I've tried saving the page locally, copying the HTML code directly from the server response, and the page views correctly. That means that there has to be something wrong in the process of passing the page from AS3 to Javascript.

EDIT2 !important:

I've realised that the problem lays in the popup encoding. When I copy the HTML from the popup, paste it in an editor and save it I can correctly view the HTML. Seems like the popup doesn't consider gbk encoding. I'm now looking for a solution to this problem.

like image 602
aberonni Avatar asked Nov 29 '12 14:11

aberonni


People also ask

What is external interface in AS3?

ExternalInterface - AS3. Classes x. The ExternalInterface class is an application programming interface that enables straightforward communication between ActionScript and the SWF container– for example, an HTML page with JavaScript or a desktop application that uses Flash Player to display a SWF file.

What is the As3 HTML text property?

The AS3 HTML text property combined with the Flash's ability to interpret and display subject specific XML symbols gives you the ability to create well-formatted Flash applications that use math symbols, foreign alphabets and ancient alphabets. Start the Flash program.

What is the externalinterface available property of Flash Player?

The ExternalInterface. available property indicates whether the current Flash Player is in a container that offers an external interface. If the external interface is available, this property is true; otherwise, it is false.

What is external interface in Adobe AIR?

Note for AIR applications: In Adobe AIR, the ExternalInterface class can be used to communicate between JavaScript in an HTML page loaded in the HTMLLoader control and ActionScript in SWF content embedded in that HTML page. [static] [read-only] Indicates whether this player is in a container that offers an external interface.


1 Answers

I've finally found a solution!!!!

Basically I had to render the chinese characters in flash so that when I passed them to javascript they were already encoded. So first of all I had to change the type of URLLoaderDataFormat to BINARY so that I received a byteArray:

my_loader.dataFormat = URLLoaderDataFormat.BINARY;

Once I received the response I modified the code this way:

        var bytes:ByteArray = e.currentTarget.data;
        var newHTML:String = bytes.readMultiByte(bytes.length,"gb2312");
        //trim whitespace to avoid javascript error
        newHTML = newHTML.replace(/\n/g, '');
        newHTML = newHTML.split("\r").join("");

        if(ExternalInterface.available)
            ExternalInterface.call("chinesePayment('"+newHTML+"')");
        else
            trace("External interface error");

As you can see, thanks to a specific byteArray function I can read the response using the preferred charset, and now it works!

The javascript remained the same, without unescape or similars. In this way the javascript function received a string with the chinese characters in it, not with the equivalent utf chars.

Thanks to everyone, you helped me get to the solution!

like image 166
aberonni Avatar answered Sep 19 '22 12:09

aberonni