I have to call an ActionScript method via Javascript, but I have a problem accessing the flash object itself. I embed the flash file via the help of swfobject.
Previously, when I use the static publishing approach, I could easily get the flash object by calling these methods:
swfobject.registerObject("flash_object", "9", "expressInstall.swf");
var flash_object = swfobject.getObjectById("flash_object");
For some technical reasons, now I have to use the dynamic publishing approach (using swfobject.embedSWF). But, as mentioned in the documentation, the method getObjectById can only be used if you use static publishing approach.
Now, how can I access the flash object?
Cheers,
Andree
With the good old document.getElementById("flash_object")
Just be sure to do it after page load. You can set it up via the callback function, too:
var mySWF = null;
var flashvars = {};
var params = {};
var attributes = {};
var embedHandler = function (e){
mySWF = e.ref; //e.ref is a pointer to the <object>
//do something with mySWF
};
swfobject.embedSWF("/path/to/file.swf", "flash_object", "550", "400", "9", "/path/to/expressInstall.swf", flashvars, params, attributes, embedHandler);
I would suggest going through the documentation on How to Integrate Flex with Java EE applications.
It explains how to use Flashvars to pass data from javascript to actionscript method. I used it myself and it works just fine.
Related
I've read through what little documentation there is on Google's Swiffy page, and I don't see any way to pass FlashVars data to the HTML5 swiffyobject.
On an old version of a site there is this embed code (which uses SWFobject) to pass a javascript variable to Flash:
var mainmovie = new SWFObject("mainpage.swf", "themovie", "640", "480", "8", "#000099");
mainmovie.addParam("align", "middle");
mainmovie.addParam("wmode", "opaque");
mainmovie.addVariable("PageLayout", layoutCode);
mainmovie.write("target_div");
In other words, the variable PageLayout is passed to the Flash movie from .js
Is there some way to similarly pass javascript variables to swiffyobjects, either through something similar to SWFobject's addVariable, or through the standard FlashVars interface?
Just answering my own question here because I found the answer.
The Swiffy equivalent of FlashVars is:
stage.setFlashVars("VariableName = value");
where "stage" is the Swiffy HTML5 object.
So I’m trying to interact with a flash variables using jQuery. The original author of the flash based program has not got back to me yet and so I thought to ask here. I'm not that strong in AC3 so forgive me.
Within the original action script, I added a new import statement:
import flash.external.*;
There's a function that initializes the program called ini and added this towards the bottom:
//MODS===========
ExternalInterface.addCallback(‘gotoLastPage’,gotoLastPage)
//===============
For all intensive purposes, just know that there is an existing and working function called gotoLastPage. It is declared as private void and works by the default application. All seemed fine there, got no errors when I recompiled the swf file.
Now the swf object is initialized like this
var flashvars = {};
flashvars.pages = “reader_fl/pages.xml”;
flashvars.settings = “reader_fl/settings.xml”;
var params = {};
params.quality = “high”;
params.scale = “noscale”;
params.wmode = “transparent”; var attributes = {};
attributes.align = “middle”;
attributes.allowFullscreen = “true”;
swffit.showScrollV();
swfobject.embedSWF("reader_fl/PageFlip_v6.swf", "Reader_Window_player", "100%", "100%",
"10.0.0", false, flashvars, params, attributes);
As a note, I'm using swfobject. The reader comes up fine and is wrapping around a div called Reader_Window_player.
Now when I go to jQuery, I tried:
$("#Floating_CtrlStart").click(function(){
var Reader = $('#Reader_Window_player')[0];
Reader.gotoLastPage();
})
However, I still can’t seem to access the gotoLastPage. Console says that gotoLastPage is not defined.
Any help here?
Are you opening the html page from the file system and not served from a web server? If so, that would explain why it's not working.
Calls to ExternalInterface fail if the content (html and swf) is in the local-with-networking or local-with-filesystem sandbox (source: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7c9b.html).
I love JQuery but I usually do that the old fashion way:
var getSwf = function (swfName) {
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[swfName] : document[swfName];
}
getSwf("Reader_Window_player").gotoLastPage();
Also make sure you have the following in your JS:
attributes.id = "Reader_Window_player";
attributes.name = "Reader_Window_player";
and as #Cherniv stated in the comments:
params.allowScriptAccess="always"
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
calling a Flash ExternalInterface with JavaScript
I have a flash file with AS code. I want to run Javascript that will run a function the AS. For example: In the AS I have a function called "loadXML". The object that holds the SWF file called "pawel" (The ID of the object). How can I run a Javascript code that will run on "pawel" the function "loadXML"? I'm using Flash 6 with AS 3.
You should use ExternalInterface.addCallback() method. See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#addCallback() for details.
I suggest you check this http://www.redcodelabs.com/2012/04/calling-actionscript-method-from-javascript/ it has running sample and as3/js code.
I believe the goal here is to send the swf (which has compiled an external .as file) the location of an xml file somewhere on the server so the swf can compile, parse, and create something.
I've done this many times, using flash variables, which may be useful to you as well. When embedding the .swf in your webpage you can send it flash variables via js or html, or dynamically with php. Once i have my fla working perfectly with hardcoded variables (like the xml) i then add the flash variable code to the head and make a few minor adjustments to the fla – which usually breaks the fla from running "properly" within flash (because it's now dependent upon these external variables)
anyways, here's how i do my flash vars (there's other ways, worth a good google search)
import flash.net.*;
var flashVars:Object = new Object();
flashVars = this.loaderInfo.parameters;
var xmlVal;
for (var item:String in flashVars)
{
switch (item)
{
case "xmlLocation" :
xmlVal = String(flashVars[item]);
break;
}
}
Here's the javascript which sends the values:
<script type="text/javascript">
//flashObj
var flashvars = {};
flashvars.xmlLocation = "http://google.com/myXML.xml";
var params = {wmode:"transparent"};
var attributes = {};
swfobject.embedSWF("images/banner.swf", "yourSliderId", "175", "300", "9.0.0", false, flashvars, params, attributes);
</script>
this is using SWFObject (an open API flash embedding js library) to handle the swf embed. I prefer it because if you look at the code above you can read it and understand it, the default way is really hard to read, edit and understand.
If you simply need XML and then you're done, this will work for you. If you still need to say hit the 'next' or 'previous' button using javascript, refer to this web site article, i believe this may help you out further:
http://arrixlive.wordpress.com/2005/03/25/javascript-in-love-with-flash-control-swf-from-javascript/
I am using the swfObject library to dynamically embed a MP3 player i've made in Flash CS5. In the .fla file, i've declared a list of methods that can be called via Javascript (using the flash.external.ExternalInterface flash class).
That's not the problem since all these function work properly when called from Google Chrome's console.
However, swfObject provides a way to invoke javascript API only if the .swf has been statically included (i.e. using swfobject.registerObject() ) but i can't find a way to achieve the same goal when the .swf is dynamically included (i.e. using swfobject.embedSWF() ).
Thanks in advance for your help and contibutions :)
When you are using swfobject.embedSWF, you can specify the ID of the swf object in the attributes parameters :
swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes, callbackFn)
Example :
swfobject.embedSWF(
"YourFlash.swf", "WhereToPlaceThis", "0px", "0px", "10.0.0",
"expressInstall.swf", {}, {}, { id : "IdOfTheSWF" },
function () {
var SWF = document.getElementById("IdOfTheSWF"); // That's your SWF //
SWF.yourFlashFunction(); // And you can invoke function //
}
);
If you are developing an extension for one of the mozilla applications (e.g. Firefox, Thunderbird, etc.) you define a extension id in the install.rdf.
If for some reason you need to know the extension id e.g. to retrieve the extension dir in local file system (1) or if you want to send it to a webservice (useage statistic) etc. it would be nice to get it from the install.rdf in favour to have it hardcoded in your javascript code.
But how to access the extension id from within my extension?
1) example code:
var extId = "myspecialthunderbirdextid#mydomain.com";
var filename = "install.rdf";
var file = extManager.getInstallLocation(extId).getItemFile(extId, filename);
var fullPathToFile = file.path;
I'm fairly sure the 'hard-coded ID' should never change throughout the lifetime of an extension. That's the entire purpose of the ID: it's unique to that extension, permanently. Just store it as a constant and use that constant in your libraries. There's nothing wrong with that.
What IS bad practice is using the install.rdf, which exists for the sole purpose of... well, installing. Once the extension is developed, the install.rdf file's state is irrelevant and could well be inconsistent.
"An Install Manifest is the file an Add-on Manager-enabled XUL application uses to determine information about an add-on as it is being installed" [1]
To give it an analogy, it's like accessing the memory of a deleted object from an overflow. That object still exists in memory but it's not logically longer relevant and using its data is a really, really bad idea.
[1] https://developer.mozilla.org/en/install_manifests
Like lwburk, I don't think its available through Mozilla's API's, but I have an idea which works, but it seems like a complex hack. The basic steps are:
Set up a custom resource url to point to your extension's base directory
Read the file and parse it into XML
Pull the id out using XPath
Add the following line to your chrome.manifest file
resource packagename-base-dir chrome/../
Then we can grab and parse the file with the following code:
function myId(){
var req = new XMLHttpRequest();
// synchronous request
req.open('GET', "resource://packagename-base-dir/install.rdf", false);
req.send(null);
if( req.status !== 0){
throw("file not found");
}
var data = req.responseText;
// this is so that we can query xpath with namespaces
var nsResolver = function(prefix){
var ns = {
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"em" : "http://www.mozilla.org/2004/em-rdf#"
};
return ns[prefix] || null;
};
var parser = CCIN("#mozilla.org/xmlextras/domparser;1", Ci.nsIDOMParser);
var doc = parser.parseFromString(data, "text/xml");
// you might have to change this xpath expression a bit to fit your setup
var myExtId = doc.evaluate("//em:targetApplication//em:id", doc, nsResolver,
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null);
return myExtId.singleNodeValue.textContent;
}
I chose to use a XMLHttpRequest(as opposed to simply reading from a file) to retrieve the contents since in Firefox 4, extensions aren't necessarily unzipped. However, XMLHttpRequest will still work if the extension remains packed (haven't tested this, but have read about it).
Please note that resource URL's are shared by all installed extensions, so if packagename-base-dir isn't unique, you'll run into problems. You might be able to leverage Programmatically adding aliases to solve this problem.
This question prompted me to join StackOverflow tonight, and I'm looking forward participating more... I'll be seeing you guys around!
As Firefox now just uses Chrome's WebExtension API, you can use #serg's answer at How to get my extension's id from JavaScript?:
You can get it like this (no extra permissions required) in two
different ways:
Using runtime api: var myid = chrome.runtime.id;
Using i18n api: var myid = chrome.i18n.getMessage("##extension_id");
I can't prove a negative, but I've done some research and I don't think this is possible. Evidence:
This question, which shows that
the nsIExtensionManager interface
expects you to retrieve extension
information by ID
The full nsIExtensionManager interface
description, which shows no
method that helps
The interface does allow you to retrieve a full list of installed extensions, so it's possible to retrieve information about your extension using something other than the ID. See this code, for example:
var em = Cc['#mozilla.org/extensions/manager;1']
.getService(Ci.nsIExtensionManager);
const nsIUpdateItem = Ci.nsIUpdateItem;
var extension_type = nsIUpdateItem.TYPE_EXTENSION;
items = em.getItemList(extension_type, {});
items.forEach(function(item, index, array) {
alert(item.name + " / " + item.id + " version: " + item.version);
});
But you'd still be relying on hardcoded properties, of which the ID is the only one guaranteed to be unique.
Take a look on this add-on, maybe its author could help you, or yourself can figure out:
[Extension Manager] Extended is very
simple to use. After installing, just
open the extension manager by going to
Tools and the clicking Extensions. You
will now see next to each extension
the id of that extension.
(Not compatible yet with Firefox 4.0)
https://addons.mozilla.org/firefox/addon/2195