I found this excellent article by Peter McBride - JavaScript and VBScript Injection in ActionScript 3
I based my solution off of the methodology in the article and it worked beautifully.
I created several AS classes, each with static public functions that could be invoked to load their respective JS into memory. Like the article describes, anonymous functions assigned to variables.
import flash.external.ExternalInterface;
public class JSONInjector
{
public function JSONInjector()
{
}
static public function registerPlayer():void
{
var js:XML = <script>
<![CDATA[
function()
{
JSPLAYER = new Object()
JSPLAYER.obs = {}; // Observers
JSPLAYER.addListener = function( eventType, callbackObj, callbackFunc){ .. }
Rsize = function(divID, height, width)
{
var element = document.getElementById(divID);
element.style.height = height + "px";
element.style.width = width + "px";
}
OpenNewWindow = function(URLtoOpen, windowName, windowFeatures)
{
var newWindow=window.open(URLtoOpen, windowName, windowFeatures);
}
...
}
]]>
</script>
try{
ExternalInterface.call(js);
}catch(e:Error){}
}
Then within the init of the main application I simply called the functions:
JSONInjector.registerContainer()
JSONInjector.registerPlayer()
And within my external interface files, I simply called the JS objects through external interface:
ExternalInterface.call("JSPLAYER.pause",true)
It's as simple as that, and now the need for any peripheral .js files has been negated and we have a self-contained solution for all video playback.
1 comment:
Fascinating stuff, i'm working on a desktop app in AIR that I want to detect the browser, current URL, and the size of the web-page, and store that info locally (as well as pass it to a database).
Maybe script-injecting would be a great way to action this :)
Post a Comment