Copy/Paste in JavaScript? - cross-browser

I know this question was asked like a million times by now, but I couldn't really find a good up-to-date solution.
I've implemented my own menu to provide the user the ability to Cut, Copy and Paste into my WebApp.
But I'm not sure how to actually work with the clipboard on Firefox, IE, Safari/Chrome.
Thank you for your help.

I just wrote a detailed technical blog post on this very subject (I work for Lucidchart and we recently did an overhaul on our clipboard). Included in the post is this fiddle which is a working example of copying and pasting via Javascript.
The good news is that this example gives you working code for setting/getting any supported clipboard data types whenever the user uses a clipboard hotkey.
The bad news is that using your own context menu to copy and paste is problematic. Even Google can't get around this (try using context-menu copy or paste in Google Docs in Firefox). You'll be able to get it to work without too much trouble in IE. This is because you can access the clipboardData object at anytime from Javascript via:
window.clipboardData
(When you attempt to do this outside of a system cut, copy, or paste event, however, IE will prompt the user to grant the web application clipboard permission.)
In Chrome, you can create a chrome extension that will give your web app clipboard permissions (this is what we do for Lucidchart). Then for users with your extension installed you'll just need to fire the system event yourself when they click the menu option:
document.execCommand('copy');
It looks like Firefox has some options that allow users to grant permissions to certain sites to access the clipboard, but I haven't tried any of these personally.

did u try :
http://ericphan.info/development/cross-browser-copy-and-paste-with-jquery-copy/
UPDATE:
the link is not available so i copy the content from cache :
The Scenario
I was working on a client project for SSW when the client reported a bug in the web app.
The bug involved a dynamically generated mailto link that got updated when you selected multiple employees. The client was reporting an error when he selected more than 10 employees to email. His Lotus Notes mail client popped up an error saying:
Error processing command line arguments
Testing this myself I found that Outlook 2007 could easily support the emails of 30-40 employees before the mailto link stopped working.
The Cause
It turns out that the mailto spec has a limit and the mail clients also have a limit. Lotus Notes only handles 240 characters in the mailto link and other modern mail clients like Outlook 2007 support the 2083 characters - the max length of a URL
This explains the discrepancy in testing.
The fix - JQuery to the rescue
Since this is a limitation of the HTML spec we needed another solution to meet the client’s requirement of “I want to be able to select multiple employees and send an email to all of them”
We could have created an email form that used SMTP to send out the email - but the client wanted to use Lotus Notes as his mail client.
We ended up changing the “email” button to copy all the emails (comma separated) onto the clipboard and popped open a new email window. All the client had to do was hit CTRL + V and paste the emails into the TO field. This was the quickest and most cost effective solution that gave the client the flexibility to use their own email client.
There is a JQuery plugin called jquery.copy that provided cross browser copy and paste by using a flash (swf) file. This is similar to how the syntax highlighter on my blog works.
Once you reference the jquery.copy.js file all you need to do to push data into the clipboard is run the following:
$.copy("some text to copy");
Nice and easy ;)
Note: you may need to change the path the the SWF file in jquery.copy.js to get this to work

Related

how to run batch file in chrome

I tried using very simple HTML to create several links to several batch files on my server that are intended to run when users click on the links. Using Chrome, every time I click on one of those files my browser displays the script itself (even though its a .bat) and doesn't actually run the script. Internet Explorer runs it every time, however all the users on the network use Chrome for web browsing.
Is there any way to force Chrome (preferably through HTML or VB or some other scripting on the page itself rather than change all of the users' browser settings) to run these batch files when the user clicks on the link?
The answers given so far - that it's "not possible" - are incorrect or outdated. Using Chrome Apps you can call executables (called "hosts") if they are registered with Chrome. Of course a Chrome App is a client application so you need to distribute it.
See https://developer.chrome.com/extensions/nativeMessaging#examples
HTML, JS on browser cannot run shell command, command line. You have to implement server script to execute your bat file then call it from HTML, JS via Ajax or direct link.

Download an HTML file so that it opens in an email client (e.g. MS Outlook)

I'm struggling to find an answer to what I thought would be a relatively straightforward problem, and wondered if anyone could help:
It is to find a way of allowing users to open downloaded HTML files in an email client such as MS Outlook (without having to save them first).
We have a Web-based application that allows users to upload files, including emails. To do this, they need to save the email onto their network, and then upload the saved file via the browser's 'file select' control. The files are saved in an Oracle database, and we have a utility that allows the user to later download the saved email. When they do so, they are given the option to 'Open' or 'Save'.
When the email was saved using the .msg format, opening the downloaded email opens that email in Outlook, and all the usual email functionality is available. However, if the email is saved in HTML, selecting 'open' always loads the email content into the browser.
Is there a way of invoking Outlook when the user opens an HTML file from the Web app, so that they don't have to either 1) always save their emails as .msg files or 2) download the email to the network and use 'open with' to open it in Outlook?
The download utility allows us to set the MIME type, but application/vnd.ms-outlook (and the various email-related types) have no effect; so that an HTML email always opens in the browser.
We have also tried changing the filename extension to '.msg', but this results in an Outlook error message and the email does not open. Removing the file extension altogether means that the file still opens in the browser.
Changing the HTM/HTML file associations so that they open with Outlook is not an option for us, I'm afraid.
Does anyone know of a solution to this? Although the problem as I've described it isn't such a big deal for our users, we were hoping to generate draft emails programmatically for users to edit and send, but without the ability to open an HTML file in Outlook, this is going to be a lot trickier than we'd hoped.
Our main network browser is still IE7, incidentally.
Many thanks for your help with this.
Outlook does not handle HTML files at all. It needs to either be MSG or EML file.
For the latter option, it is easy enough to create a dummy EML file with an HTML MIME part that represents your HTML data.
The file extension would have to be set to .msg, and the file would have to be rewritten in this format as well. You can see the technical specifications for this format here: http://msdn.microsoft.com/en-us/library/cc463912%28v=exchg.80%29.aspx

Prefill webpage from Lotus Notes client application

Looking to prefill a webpage (a form in a wordpress site) from a lotus notes application. My investigations to date show that using the query strings behind the urlname should work - however I am missing something here.
www.url.com/register?fname="bob"&sname="smith" etc...
I thought this could be done, but am not sure where the starting point would be?
Ideal solution - webservice from the owner of url.com to allow us to populate and get a response once done. They are not keen to do this as it does not save them any time, just our business.
I am looking to understand how to prepopulate the webpage so that the user just has to submit the form at url.com.
You could go mega-sneaky and use the Selenium WebDriver to launch a browser session on your client. It has full control over the browser and can sniff out the fields you need to populate. I wrote some simple code for starters - but the samples on the original web site might be more comprehensive.

Can one configure Chrome to offer opening downloads in suitable applications?

I recently made Google Chrome my default browser (mainly due to its bookmark and preferences syncing capabilities across multiple systems).
One thing I find increasingly annoying with it though is, that Chrome does not allow to directly open a downloaded file in an appropriate application!
In IE I was us to, that, when I clicked onto a link pointing to some document, e.g. a spreadsheet, to being asked, whether I wanted to save this document to a local file or to directly open it (in Excel in this example). This worked with all kinds of registered applications but also other formats, e.g. email addresses (mailto:-links) or Calendar entries (.ics-files) which directly opened Outlook's new email dialog with the email-address already filled int or created a new calendar entry from the .ics file, resp.
In Chrome one can only save such docs to a local file and then needs to locate that and has to open it manually. I find this always most cumbersome! Is there some plugin that allows to bring back the convenience I was used to?
And to spare me the usual comments on such feature requests right away: Yes, I am aware, that this is a potential security issue, if one opens docs from dubious sources, etc. I know, that one needs to understand what one is doing in such situations, but I think I do!
Click the little arrow beside the file, which is being downloaded and choose Always open files of this type. That will make it open automatically in the program the file type has been associated with in Windows after it finishes downloading.

Chrome Extension Plugin Ability?

I am building an extension for Chrome which gives the user a basic API. I would like for other developers to have the ability to add functions of their own to my API. For example, some developers offer a new "plugin" (which is only JavaScript code), and I want users to be able to download that plugin into their extension.
The main problem I'm facing is this:
How do you load new code into an extension permanently?
Ideally I would like to add code into the extension's JavaScript, but I have no way to write to the file; I am under the impression that I am restricted by JavaScript - is this true?
While I could perhaps load new code dynamically (by downloading some script), that code will only hold for the current run, and is not added permanently. Rather, it is gone once the user reloads the extension.
The only solution I can see so far is to create a login system where I save each user's downloaded plugins and give him the mandatory option to load them every time he opens the extension.
This method is very messy and impractical, because I don't want to make a user login every time. In fact, I would very much like to refrain from using any login system whatsoever.
What I desire is something similar to what the GreaseMonkey extension does, which is the ability to let users write scripts and allow other users to be able to download them.
I'm obviously looking to create an extension which is much smaller and simpler than GreaseMonkey, but something like GreaseMonkey is more or less what I am looking for.
Any thoughts or suggestions?
All of the "plugins" will be independent chrome extensions. You can then use Message Passing to send a message to every installed extension and the ones that are plugins should have code that goes something like:
if recieve "some identifying key"
then respond "information about this plugin"
Now your main extension knows what plugins are installed and can load their JS files using chrome-extension://[extensionID]/file.js".
That should get you started :)