Question

I have a SWF with a few publicly accessible methods defined via the ExternalInterface. Some of the methods allow a person to change text and images that are in the SWF. Another one takes a "snapshot" of the SWF -- it generates a JPG image from what's currently visible, then calls a global function in JavaScript with the binary data of the snapshot as the function's argument. The data is not in base64 or anything; it's just raw bytes as a string.

function updateSwfElement(elementID, val) {
  document.getElementById("theSwf").updateElement(elementID, val);
}
function takeSnapshot() {
  document.getElementById("theSwf").takeSnapshot(); // returns undefined
}
function snapshotComplete(data) {
  // this function is called asynchronously by Flash after takeSnapshot() 
  // has completed.
  // "data" contains the image's bytes. typeof(data) === "string" .
  doSomethingWithTheData(data);
}

Now it turns out that for a very specific set of updateElement() calls, calling "takeSnapshot()" will throw the dreaded SyntaxError: Unexpected token ILLEGAL message. A breakpoint set inside snapshotComplete does not fire.

Note that this is NOT the same issue that others have had where a "gremlin" gets into their source code (copy/pasted from JSFiddle or whatnot) that causes a syntax error. There's nothing wrong with the source files. Somehow a call to this method on a SWF makes the JS choke up.

My hunch is that trying to shoehorn raw bytes into a string is a bad idea, and I've instructed the SWF author to Base64-encode the output. But I couldn't think of a good way to prove that I'm right.

What do you think is going on here, and what's a good way to test it?

Was it helpful?

Solution 2

Well, we did solve the problem by making TakeSnapshot() return a string of hexadecimal values due to time constraints -- it's an intranet site so I'm not so concerned about the size of the payload.

The answer to the problem almost definitely lies in the ECMAScript Language Specification:

A 'LineTerminator' character cannot appear in a string literal, even if preceded by a backslash . The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.

It is very likely that the resultant string contained one of these values.

To try to make this answer more specifically useful to the maybe 1 or 2 other people that have this problem: We were using this AS2 Bitmap Exporter library originally developed at andculture.com; using the Unicode options caused the issue when communicating with JavaScript. Switch to the HexCodes option or upgrade to AS3 (presumably, like us, that isn't an option).

OTHER TIPS

If you are not the author of swf file you can write your own swf wrapper and call method from this wrapper swf then convert to base64 and return to javascript. Yes, it's a little bit tricky :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top