Copy an Image from Flex Application (Web) and paste it outside the application - actionscript-3

I have a requirement that we should be able to copy an image displayed in our application, to Clipboard and paste it outside (Like on Excel).
I was trying the below code snippet (Inside a button Click).
Clipboard.generalClipboard.clear();
var dataLoaded:Boolean = Clipboard.generalClipboard.setData(ClipboardFormats.RICH_TEXT_FORMAT,
byteArray, false);
The dataLoaded object is true, however it does not paste anything when tried on Excel or MsPaint.
Do we have any way to achieve this?
Thanks.

The code you are showing is not enough in itself to get a successful transfer. Like many other operations within the security sandbox of a FP app (web) this code can only respond to a direct user interaction. So your code without any valid context cannot work of course but if called within a mouse down listener for example (a true user generated mouse event, creating a fake mouseevent would still not work) it should respond correctly:
private function handleMouseClick(event:MouseEvent):void
{
Clipboard.generalClipboard.clear();
var dataLoaded:Boolean = Clipboard.generalClipboard.setData(ClipboardFormats.RICH_TEXT_FORMAT, byteArray, false);
}

Related

How to access entities in CzmlDataSource after loading

I want to access to some entities in my czml data source for keep tracking in the viewer but after the loading, as one of the options for camera. I know that I can access entities in my czml file while I am loading them, but I don't know how I can access to them after loading. Here I have an example:
var viewer = new Cesium.Viewer('cesiumContainer');
var czmlDataSource = new Cesium.CzmlDataSource();
viewer.dataSources.add(czmlDataSource);
czmlDataSource.load('../../SampleData/Vehicle.czml').then(function(){
var myEntity= czmlDataSource.entities.getById('Vehicle');
viewer.trackedEntity=myEntity;
});
This code works fine, but I want to give option to the viewer to choose the camera, then I need to have access to Vehicle after I finished loading, I tried several methods, but non of them works. I have some example bellow:
var viewer = new Cesium.Viewer('cesiumContainer');
var czmlDataSource = new Cesium.CzmlDataSource();
viewer.dataSources.add(czmlDataSource);
czmlDataSource.load('../../SampleData/Vehicle.czml');
var myEntity= czmlDataSource.entities.getById('Vehicle');
viewer.trackedEntity=myEntity;
Do you know how I can define an entity from those which are already in my czml file?
The reason your second block of code doesn't work appears to be simply because you haven't waited for the asynchronous load of the czmlDataSource.
Try modifying your second block of code, take off the last 2 lines and wrap them in a button onClick callback. If you click the button before the CZML is loaded, myEntity will be undefined and the camera won't change. If you click the same button again after the CZML loads, it should work fine.

Unhandled exception when firing WinRT 8.1 WebView ScriptNotify

I'm trying to create an MVVM Caliburn-based WinRT 8.1 app (I know that CM won't be perfectly compatible with 8.1 until version 2.0 is out, but the error does not seem to be related, as it is raised also when the handler is placed in the view code behind). One of its views contains a WebView control, whose content is set via NavigateToString (HTML contents come from app's installed assets). The HTML loaded into this control includes several hyperlinks, most of them representing cross-references to other asset-based HTML content. So when users click the link I want to override the standard navigation action, get my viewmodel notified, and let it load another HTML content from the app assets.
Here is what I did, following the post Open links in external browser in WebView (WinRT):
in the XAML code, I added to the WebView control an attribute for attaching the ScriptNotify event to my VM: cal:Message.Attach="[Event ScriptNotify] = [Action GetFromLink($eventArgs)]" (see https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions).
in my VM, the method signature is public void GetFromLink(NotifyEventArgs e).
whenever my VM loads some HTML into the WebView, it first injects a script in the HTML head which replaces the click handler of each anchor representing a cross-reference (all these anchors are marked by a class="xref" attribute). This script is hold in a constant in my VM:
private const string SCRIPT = "for (var i = 0; i < document.links.length; i++) {" +
"var className = document.links[i].getAttribute(\"class\");" +
"if (className && className === \"xfer\") {" +
"document.links[i].onclick = function() {" +
"window.external.notify('url:' + this.href);" +
"return false;" +
"}}}";
Now, when I launch the app, load an item containing one of these xref's and click on it, I get an unhandled exception telling me that "navCancelInit is undefined". I suppose this error is surfacing from JS code, but I cannot see where and how this function should be defined.
According to http://msdn.microsoft.com/library/windows/apps/br227713, I do not need any additional step for ScriptNotify when HTML has been loaded via NavigateToString. Could anyone suggest a solution?
I got an answer from a MS guy about this, so credit is not mine for this answer: it is a timing issue. I must ensure that the page has fully loaded before running the script which changes the DOM; pretty simple, if you think. Just moving the script at the end of the page, or wrapping it in an onload handler, should make the trick. Hope this can save some hair-pulling to others!
If you listen to this event: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.domcontentloaded.aspx WebView.DOMContentLoaded event, All of the script should be loaded in the WebView and you should be able to access and execute, if you are trying to do this before the scripts won't be loaded.

AIR > browseForSave strange behaviour

I just noticed something weird in my app.
I have a "save" button that triggers this function:
var saveFile:File = File.desktopDirectory.resolvePath(nameOfProject+".uct");
saveFile.browseForSave("Save as");
saveFile.addEventListener(Event.SELECT, saveFileToSystem);
When clicked, it display the "browse for save" window with a suggested name file.
Now I've noticed that when exporting, installing and running the app, when I click the button for the first time everything it's ok, but when I clicked again the location looks like this:
The "where" field is the same file....? And even if I click save, it doesn't save. The strange stuff is that this is not happening while testing my app in Flash Builder.
Any hint?
I did the workaround above, and it seems to do the trick. Code is something like this:
var saveFile:File = File.documentsDirectory.resolvePath("output.file");
var tempMovedAway:File = File.applicationStorageDirectory.resolvePath("temp.temp");
currentFileRenamedForAirBug = picker.clone();
currentFileRenamedForAirBug.moveTo(tempMovedAway), true);
saveFile.addEventListener(Event.SELECT, function():void {
tempMovedAway.deleteFile();
} );
saveFile.addEventListener(Event.CANCEL, function():void {
tempMovedAway.moveTo(currentFileRenamedForAirBug);
});
saveFile.browseForSave("save file");
// MUST be sure to call saveFile.browseForSave, or you risk losing an existing file.
You can wrap this lot in checks for the Air runtime (NativeApplication.nativeApplication.runtimeVersion) and Mac-only, to avoid doing this any more than necessary.
Also, if your file save code could rely on making changes to an existing file (rather than creating an entirely new one), you'll need to tweak this a bit.

event.DataTransfer doesn't transfer ID of dragged object when running in Jetty

I have a Maven Jetty project with JSPs using JavaScript. I want to be able to highlight parts of a canvas corresponding to the dragged image's size.
When I look at my JSP by simply opening it in the browser everything works as expected but when I start the Jetty Server with the goal jetty:run the ID of the dragged object is not being set or cannot be retrieved from the transferData of the event.
The relevant code: All draggable images have a unique ID. On drag start I set the ID of the dragged image on the event's transferData like this:
function dragShipFromTable(event) {
event.dataTransfer.setData("id",event.target.id);
}
When the image is dragged over the canvas I call the following function
function allowDrop(event) {
event.preventDefault();
var id = event.dataTransfer.getData("id");
var img = document.getElementById(id);
// Do the highlighting stuff
....
}
The problem is that when the server is started and I do the above action then in allowDrop(event) the ID of the dragged image is not being retrieved from the transferData. It is undefined and therefore the highlighting fails. This is not the case when simply opening the JSP as a File.
Well I kind of found the answer to my own question. First of all the setData method on the dataTransfer object only allows certain formats as first parameter. I interpreted it as an associative array but this is not the case. So it should be
event.dataTransfer.setData("text/plain", id);
event.dataTransfer.getData("text/plain");
to set and retrieve the id correctly.
But in the end everything failed due to chrome as there is a bug in Chrome which prevents me from using dataTransfer as intended (see How do you get the selected items from a dragstart event in Chrome? Is dataTransfer.getData broken? ).
I now use global variables to store the information I need. And still I am wondering why everything worked fine when the page was displayed as a file instead of as a response from the webserver.

Actionscript works when tested in flash, but not on html page?

I am trying to create an ad for a website. When someone clicks on the ad, it is supposed to redirect them to a website, and register the click with google analytics.
I have done this with the following script:
import flash.external.ExternalInterface;
movieClip_3.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void {
trace("hi");
ExternalInterface.call("console.log", "test");
//ExternalInterface.call("_gaq._trackPageview", "/vpv/annoncer/[firmanavn.dk]");
navigateToURL(new URLRequest("http://www.google.com"), "_blank");
}
When i run this using preview->flash and i click on the surface, (where there is a big red square called movieClip_3) It opens the webpage. However when i try to publish as html, the big red square shows, but nothing happens on click. Not even console.log. I have tried setting allowscriptaccess = always but that does not change anything.
Can you guys help me? Any help is appreciated.
Security problems?
Developers should validate all URLs before passing them to this
function.
For local content running in a browser, calls to the navigateToURL()
method that specify a "javascript:" pseudo-protocol (via a URLRequest
object passed as the first parameter) are only permitted if the SWF
file and the containing web page (if there is one) are in the
local-trusted security sandbox. Some browsers do not support using the
javascript protocol with the navigateToURL() method. Instead, consider
using the call() method of the ExternalInterface API to invoke
JavaScript methods within the enclosing HTML page.
source: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/package.html#navigateToURL()
EDIT:
Since javascript is not permitted out of the sandbox, you can try with ExternalInterface:
ExternalInterface.call("javascript_functionname", "mypage.html");
In the parameters for publishing:
'allowScriptAccess', 'always',
You can only test this on your server not locally.
I'd suggest double checking the security settings (right click on flash container->Global Settings-> Advanced -> Trusted Location Settings). Also make sure your html file contains the javascript function you're trying to execute and look for blocked pop-up notifications in the browser. Maybe you just don't allow pop-ups to run.