Javascript Just Get copyed text from clipboard on Chrome - google-chrome

Can you tell me how can I just get text who is copied in clipboard. I don't want to make a copy because data are copied from Excel.
In IE I use :
var clipText = window.clipboardData.getData('Text');
And it's work perfect.
Is it possible in chrome ? or maybe Firefox ?
Thanks for advance

The window.clipboardData object is only available in IE. It seems like a big security vulnerability to me for a website to be able to access your clipboard data, especially without you knowing. According to the specification, it's mostly deprecated as of Microsoft Edge.
Instead, you can access the data by listening to the paste event:
document.addEventListener('paste', function (event) {
var clipText = event.clipboardData.getData('Text');
});

If you're looking to use jQuery and bind an element to the 'paste' event then you can access the clipboard data by using the originalEvent property on the calling event.
Check the window object to see if the clipboardData is undefined. This will mean that you're not using IE or Edge.
this.bind('paste', function(e){
if (window.clipboardData === undefined)
clipText = e.originalEvent.clipboardData.getData('Text') // use this method in Chrome to get clipboard data.
else
clipText = window.clipboardData.getData('Text') // use this method in IE/Edge to get clipboard data.
});

Related

ZoomWindow extension breaks without GUI?

Using v2.13 of the viewer, the ZoomWindow extension relies on having the default GUI enabled. Is there a way around this? The load method is:
proto.load = function() {
var viewer = this.viewer;
var toolbar = viewer.getToolbar(true);
//var toolbar = viewer.getToolbar ? viewer.getToolbar(true) : undefined;
// Init & Register tool
this.tool = new namespace.ZoomWindowTool(viewer);
viewer.toolController.registerTool(this.tool);
// Add the ui to the viewer.
this.createUI(toolbar);
return true;
};
which fails because getToolbar is undefined.
It seems from the commented out line that this has been considered, but not implemented.
What is the best way to implement a work around - should I copy the entire extension with a new name, or can I replace the load method at runtime?
Edit: was looking to use the headless viewer, but it seems easiest just to hide the UI with css.
It's not clear to me if you are using the GuiViewer3D or want to use the Viewer3D, the viewer without Autodesk custom UI. If you use GuiViewer3D, you can simply wait for the toolbar to be loaded before loading the ZoomWindow extension, which requires the toolbar controls to be created in order to add a button to it.
viewer.addEventListener(Autodesk.Viewing.TOOLBAR_CREATED_EVENT, function () {
viewer.loadExtension('Autodesk.Viewing.ZoomWindow')
})
Here is a blogpost I wrote a while ago about using events in the viewer. It is not up-to-date with the current version but remains valid:
http://adndevblog.typepad.com/cloud_and_mobile/2015/10/event-watcher-extension-for-view-data.html
Now as Zhong mentioned, if you want to use the headless viewer with no UI and still use the extension, you may have to copy and customize it as you suggested. But an easier workaround could be to use GuiViewer3D and simply hide the existing toolbar with css, so the the js code remains valid. Set display:none on div id="guiviewer3d-toolbar", for example, or on the adsk-control class.
Hope that helps

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

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);
}

FireFox Add-on: retrieve text from textbox in current tab gives no result

I am trying to create an add-on that gets the contents (textbox.value) from a textbox with ID = city from the current webpage and write it to a text file.
The file can be written without getting the textbox value. But, If I update the code then it writes nothing. Below is the code I used to get the textbox value.
var cityfromfield = window.content.document.getElementById('city').value;
var date = new Date();
var TimeStamp = date.toLocaleString();
var wstrtotext = TimeStamp + cityfromfield;
fos.write(wstrtotext, wstrtotext.length);
Any help would be appreciated.
Without more information, it is necessary to guess at what your problem is. The most likely issue is that you are attempting to find the textbox element with ID=city in the wrong document.
Firefox add-ons generally run in a scope where the global window object is not defined (if it is defined depends on how the portion of your code that is currently running was entered). Even if it is defined, it is often not defined as the window which you are expecting (the window of the current tab). You will probably need to obtain a reference to the window object for the most recently accessed window/tab.
If a browser window exists (in some instances you could be running where no browser window exists, yet, e.g. at start-up), you can obtain a reference to the most recent browser window, document, and gBrowser with:
if (window === null || typeof window !== "object") {
//If you do not already have a window reference, you need to obtain one:
// Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
/* Add-on SDK:
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
//*/
//* Overlay and bootstrap (from almost any context/scope):
var window=Components.classes["#mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
//*/
}
if (typeof document === "undefined") {
//If there is no document defined, get it
var document = window.content.document;
}
if (typeof gBrowser === "undefined") {
//If there is no gBrowser defined, get it
var gBrowser = window.gBrowser;
}
If you are running the code in response to an event (e.g. a button command event), you can obtain the current window with:
var window = event.view
The lack of having the global window object available, or having it reference something other than what you are expecting, is something that many people encounter as a problem when writing Firefox add-ons.
Note: If you are wanting to be natively compatible with multi-process Firefox (Electrolysis, or e10s), then gaining access to the contents of the current document is more complex. There are shims in place which should make your code continue to work with multi-process Firefox for some time, but they may/will eventually go away.
References:
nsIWindowMediator
Working with windows in chrome code
SDK: window/utils
SDK: windows
Large portions of this were copied from my earlier answers, including this link.

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.

listen to keystrokes from different pages

I want to develop an extension that runs in the background and listens to keystrokes and stores them as a string in a variable. For example, if I have 5 tabs in a chrome browser window and I press a,b,c,d,e on each tab of the window; the final string should be abcde.
Could any please provide a sample code for this?
Help will be greatly appreciated.
You could add code like this to a content script:
var bodyElement = document.getElementsByTagName("body")[0];
bodyElement.addEventListener("keypress", function(e){
console.log(e);
console.log(String.fromCharCode(e.keyCode));
});
The body element must be loaded for this code to work, so use jQuery's $(document).ready(), or similar, or in the extension manifest set the run_at value for the script to document_end.