Javascript, Flash and cross-browser clipboard fun

Recently I have been wrestling with writing some code that will manipulate text on a user’s clipboard in a web browser. I thought I would share my findings here for others who might be interested in the same topic.

First lets start by looking at the Windows Clipboard in general. The clipboard, as we all know, stores blobs that the user has copied or cut from windows applications for future use. It stores this data in a variety of formats (if writing a windows application you can use the formats specified by the DataFormats class.) It is a convenient way to transport data between applications in windows. A simple example of writing to the clipboard in c# would then look like:

// For this example, the data to be placed on the clipboard is a simple
// string.
string textData = "I want to put this string on the clipboard.";
 
// After this call, the data (string) is placed on the clipboard and tagged
// with a data format of "Text".
Clipboard.SetData(DataFormats.Text, (Object)textData);

Doing this on in a windows app is great, and easy, because that is really what the api’s were designed for… but what if we want to do this in a web app? Why would we do this you ask, well one argument is that as web apps and client apps converge, users are starting to expect more and more of the familiar windows UI concepts to be available to them on the web. Yeah, we’ll say that is the reason.

In a web app, there are primarily 3 ways that we are going to run code that will interact with our user: javascript, flash, and silverlight. We’ll take a look at each of these and see what is possible.

Javascript

So what can we do with javascript and the clipboard? It depends on the browser and on the interaction that you want. Lets start with the simplest, and potentially cross browser action: execCommand. The execCommand method can perform a variety of operations, but the ones we care about are ‘copy’, ‘cut’, and ‘paste’. Because execCommand was built with enabling javascript based wysiwyg editors in mind, it operates on the selected areas of a web page. That is, text that has been highlighted or where there is an active cursor (e.g. in a text box). So how can we use execCommand? If you want to simply copy the selection to the clipboard, then it is trivial and you can just:

document.execCommand("Copy");

This copies the user’s selection to the clipboard.

What is good about the execCommand copy approach? It is potentially cross browser (not with default security settings in FF) and simple to implement. So long as they have javascript enabled you can use it. What is bad? In modern browsers the user will be prompted the first time that you attempt to do this and warned that you are trying to access their clipboard, which is a bad UX but necessary for security reasons.

Furthermore, you may only copy text that the user has selected in the browser.

For more examples of execCommand clipboard stuff, see this Geekpedia tutorial or this excellent JS WYSIWYG Editor tutorial.

The IE only alternative

An option, if for some strange reason you would like to build an application that only works in IE, is to take advantage of the clipboardData object. Using this, you can write and read directly from the clipboard and the user prompt is ignored when using the default security settings. Using this you can only take advantage of two ‘types’ on the clipboard: ‘text’ and ‘url’

To write text to the clipboard, you could then do the following:

var textToCopy = "This is some text for the clipboard";
window.clipboardData.setData("Text", textToCopy);

One nice thing about this, is that you can write arbitrary text to the clipboard (no user selection needed). But remember, this is Internet Explorer only. Mozilla does have a solution, but it is FF only. So if you want to use the clipboard in all browsers and not change security settings what can we do?

Flash

The best answer so far is flash. The idea (originally credited to Mark O’Sullivan) is to use the Flash setClipboard method to write to the clipboard. Why is this better than the javascript solutions? Because, the good people at Adobe did all the cross browser work for us. Now the only requirement is that the browser has a flash plugin, which given the penetration of flash is a fair bet. What we can then do is, when we want to copy something to the clipboard, we pass (via javascript) the the text to our flash object and let it do the clipboard dirty work.

Jeffothy Keyings has a good example of how to do this, and links to the open source flash object (_clipboard.swf) by O’Sullivan.

Silverlight

I looked and looked, and to the best of my knowledge Silverlight does not yet expose a method for accessing the clipboard. Alas.

Conclusions

So where does that leave us?

  1. If you want cross browser clipboard access you will need to use flash.
  2. If you do not want a dependency on flash, then you only have a good solution for IE

One last thing…

One more thing that is worth noting is that you can only write to and read from the clipboard from within the browser using the TEXT format. Why is that important? Because if you want to put images, or rich text on the clipboard you can’t. If you go into your browser and highlight some text with styles and then paste it into Microsoft Word, you see that the styles are preserved. That is because this is written to the Clipboard and read from it as RTF. Not only that, but if you paste it into Microsoft OneNote, you’ll find that you can see the original source of the clip. That is because as rich client apps, IE (or FF), Word and OneNote are not sandboxed in the same way that flash and javascript are. To see this more clearly try downloading Clipboard Inspector and looking at the format of items that you copy to the clipboard.

When clipping from a web browser you get the HTML option and there is header information showing source, etc:

When you write via javascript or flash you are forced to write out text, so when applications access the clipboard, even if you have valid HTML on there they will html encode the characters thinking they they are text, and thus not render them correctly. There are numerous reasons why this makes sense from a security perspective, but it is a pain and something that web developers need to be aware of nonetheless.

Miles per Gallon vs. Gallons per Mile

A new study by Richard Larrick of Duke’s Fuqua School of Business, recently published in Science magazine shows that most people have the wrong intuition about how to save gas. This is because they misunderstand what the “miles per gallon” statistic actually represents. As a result of this they may not be doing the optimal things to decrease their gas consumption.

Lets look at a simple example.

Say you have 2 cars, a Honda Sedan which gets 30mpg and a Ford SUV which gets 15mpg. You put an equal number of miles on both cars and can’t get rid of the SUV because you need it for hauling SUV sized things.

Now you are proud of your eco-friendliness, you recycle your cans and buy only organic, free-range, local food, so you want to figure out what you can do to use less gas. You realize that you have enough money to upgrade one of your cars. You can replace the Sedan with a Hybrid that gets 55mpg or you can replace the SUV with a Hybrid that gets 22mpg. Which would you choose?

According to Larrick most people would replace the Sedan. Why? Because 50mpg sounds like a great thing! A gain of 25 mpg sounds far more Green Peacey than a gain of 7mpg.

Now, lets look at the math.

Lets say that you drive 1000 miles a month, evenly split between the two vehicles. That means that pre-upgrade we are burning:

500/30 + 500/15 = ~50.0 gallons of gas (16.6 for the Sedan, 33.3 for the SUV)

If we upgrade the Sedan, we get:

500/55 + 500/15 = ~42.4 gallons of gas (9.1 for the Sedan, 33.3 for the SUV)

If we upgrade the SUV, we get:

500/30 + 500/22 = ~39.3 gallons of gas (16.6 for the Sedan, 22.7 for the SUV)

So by upgrading the SUV, even though you only boost it by 7mpg you are actually burning less gas overall! Why is this? Because we are holding the number of miles driven fixed, thus the more important statistic is how many gallons per mile your car gets. Miles per gallon is useful when figuring out how far you can go on a tank of gas but less so, when you have a fixed distance to travel and are looking to figure out how much gas you’ll need.

Larrick’s paper concludes that people looking to save gas (or trees) should figure out the gallons per mile for their vehicles when considering upgrades. For our above examples we can see that the cars got (in units of gallons per 10 miles):

Sedan + SUV = 0.333 + 0.666 = 1 gallons/10 miles
Hybrid Sedan + SUV = 0.182 + 0.666 = 0.848 gallons/10 miles
Sedan + Hybrid SUV = 0.333 + 0.456 = 0.789 gallons/10 miles

Something to consider next time you get a car, and yet another feather in the behavioral decision theorists’ caps.

Trynt Web Services

Looking around for cool services to make use of in ISpeechPlugins I stumbled upon Trynt Heavy Technologies. I’m not sure what their business plan is but they have a seemingly useful list of APIs for people to take advantage of. Worth a look if you want some services to play with.

I hope they take themselves seriously

Things that make NNT sadWhile watching Game 4 of the NBA Finals this evening I accidentally saw a commercial for thee local news here in Seattle. I generally try and avoid local news like the plague–its like cable news but with a lower budget. Anyhow, the commercial warned any and all that were listening that we had to stay tuned in order to learn about how an “everyday item that millions of people have may be killing you.” This everyday item was… wait for it… your vinyl shower curtain. Now I generally discount all scare stories that the news offers up, but the warning was so grandiose and the object to be feared so ridiculous, that I had to look it up. Heres what I found (emphasis mine)

Could your shower curtain be harming your health?

An environmental group claims in a new study of vinyl shower curtains that some of them may release into the air toxic chemicals which could cause asthma, eye irritation or even cancer.

“We have a clear-cut case that these products release elevated levels of harmful chemicals,” said report co-author Mike Schade, PVC campaign coordinator for the Center for Health, Environment and Justice. “Our research builds on a growing body of evidence that shows that the government fails to protect us from the growing number of toxic chemicals in products.”

But some health experts are paying scant attention to those behind the curtain study. And perhaps with good reason.

Skeptics pointed out what they call a glaring error in the study’s methodology. The group tested a total of five shower curtains, of which only one shower curtain — not one brand; one curtain — was subjected to complete testing for chemicals in its composition, as well as those it released into the air — a phenomenon known as “off-gassing.”

Further testing was not performed “to avoid potential instrument damage,” according to the report.

The study found the one curtain which was tested for off-gassing may have released, over the course of the first few hours after it was opened, chemicals that could be toxic if swallowed or inhaled only in quantities thousands of times greater than those found.

Critics said that the testing was not verified by an independent lab and didn’t account for real-world conditions such as temperature or humidity in a shower stall.

The study also included estimated results for some of the chemical amounts it reported, and most of the off-gassed chemicals found initially were not detectable after a few days.

In short, it is a piece of shower curtain research that some experts said just doesn’t hold water.

They tested FIVE CURTAINS? Thats it?! Not only that but only one was fully tested and it emmitted for a short times toxic gasses that are 1/1000 of the toxic dose? While the rest of the article continues to backtrack and talk slightly more rationally, if you acknowledge these facts in your article, then how in the world can you lead with “Could your shower curtain be harming your health?”?! Furthermore, lets not understate the case, the last line should read “it is a piece of shower curtain research that any and all scientists that have the right to use the title said just doesn’t hold water.”

Jesus, I need to just watch the news for 30 seconds to remind myself why I don’t make a habit of it. I take solace in hoping that everyone involved in this scare mongering really believes what they said and is trying to figure out how to get by without a plastic shower curtain.

This blog as my notebook

I thought I should give some context to the upcoming posts. As I don’t imagine that anyone other than myself, and maybe my wife (if she is incredibly bored) will read this blog, I intent to use it as a kind of personal notebook. I have found (along with many others), that writing down my thoughts vastly increases the chance that I will remember them. I intend to use this blog as that repository of things that I hope to remember, because even if I write them out and still forget, I’ll have a much smaller search space when I try and look them back up again.

For the most part I intend to study the Behavioral Economics literature and keep my notes here. Given that I will also be writing about technology and the person who cares about both of these two topics is rare, I suggest to the would be reader that they subscribe to one of my topic feeds (programming, econ, etc) to help filter the noise.