[home]   [me]   [my gallery]   [my blog]   [my articles]   [my code and projects]   [my links]
 

Copy to clipboard from unsigned applet


Running a unsigned applet in a browser have some security restrictions, such as forbidding running in full screen mode and programmatically write access to the system clipboard. These restrictions seem a bit outdated, since Flash have those two features, without Flash being considered a security risk.

In this blog I'll explain a workaround for copying text into from a unsigned applet.

Browsers actually have the same issue. There is not way write to the system clipboard using standard html DOM (However IE does provide a workaround). As you might have noticed on this page (or on places like DZone.com), the syntaxhighlighter have solved the issue, simply by creating a small flash application that copies the content into the clipboard. Nice hack.

In this blog, I'll show how to use the same hack on Java.

The browser javascript copyToClipboard function explained

If the browser is IE, then the copyToClipboard function uses the window.clipboardData.setData function to set the clipboard data. Otherwise, the browser creates an embedded flash object, and adds the data to flash using a parameter.

var flashcopier;
var flashlocation = 'clipboard.swf';

// setup flashcopier
if(!window.clipboardData){
document.write("<div id=\"flashcopier\"></div>");
}

function copyToClipboard(text){
if(window.clipboardData)
{
window.clipboardData.setData('text', text);
}
else
{
if(flashcopier == null)
{
flashcopier = document.getElementById("flashcopier");
}
flashcopier.innerHTML = '<embed src="' + flashlocation + '" FlashVars="clipboard='+encodeURIComponent(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
}
}

The usage of this javascript file could look like this:

<script type="text/javascript" src="/roller/java/resource/clipboard/javatoclipboard.js"></script>
<script type="text/javascript">
flashlocation = '/roller/java/resource/clipboard/clipboard.swf';
</script>
<a href="javascript:copyToClipboard('Hello world');alert('Hello world copied to clipboard');">Copy 'Hello world' to clipboard using javascript</a>
 

and the result would look like this:

Copy 'Hello world' to clipboard using javascript

Copy text from applets

Since Java applets can call javascript functions, applets can now also copy text.

import netscape.javascript.JSObject;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CopyToClipboard extends JApplet implements ActionListener, Runnable{
private JTextArea textArea;

public void init() {
SwingUtilities.invokeLater(this);
}

public void run() {
initGUI();
}

private void initGUI() {
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());

textArea = new JTextArea("Type some text here");
textArea.setSelectionEnd(textArea.getText().length());
add(new JScrollPane(textArea), BorderLayout.CENTER);

JPanel buttonPanel = new JPanel(new FlowLayout());
add(buttonPanel, BorderLayout.SOUTH);

JButton copy = new JButton("Copy to clipboard");
copy.addActionListener(this);
buttonPanel.add(copy);
}

public void actionPerformed(ActionEvent e) {
String text = textArea.getText();

JSObject window;
try {
window = JSObject.getWindow(this);
window.call("copyToClipboard", new Object[] {text});
JOptionPane.showMessageDialog(this, "Text copied to clipboard");
} catch (Exception e2) {
e2.printStackTrace();
JOptionPane.showMessageDialog(this, "Error during copy to clipboard");
}
}
}

You need to declare/import your javascript function on the same page as the applet, so the html for the applet could look something like this:

<script type="text/javascript" src="/roller/java/resource/clipboard/javatoclipboard.js"></script>
<script type="text/javascript">
flashlocation = '/roller/java/resource/clipboard/clipboard.swf';
</script>
<script src="http://java.com/js/deployJava.js"></script>
<script>
deployJava.runApplet({codebase:"/roller/java/resource/clipboard/",
code:"CopyToClipboard.class",
width:"400", Height:"400", mayscript:"true"}, null, "1.5");
</script>

And the applet looks like this:

Download full source code here: src_copytoclipboard_0_1.zip

 
 
 
 
Comments:

Where do I find the package allowing me to import netscape.javascript.JSObject; ?

Posted by Viktor Sehr on June 15, 2009 at 08:27 PM CEST #

plugin.jar located in the lib folder in your jre (note that it does not exists in the jdk folder)

Posted by Morten Nobel-Jørgensen on June 16, 2009 at 09:46 AM CEST #

[Trackback] This info is pretty good stuff!

Posted by security alarm monitoring on December 10, 2009 at 06:04 AM CET #

[Trackback] okhndzfg

Posted by okhndzfg on January 07, 2010 at 05:18 AM CET #

Good stuff. Is it possible make the flash application retrieve data from the clipboard? Then we would have a paste function.

Posted by Georg Cantor on January 25, 2010 at 07:35 PM CET #

[Trackback] Levitra online consultation us overnight. Viagra levitra viagra comparisons. Levitra. How to take levitra.

Posted by Experiencing levitra. on March 08, 2010 at 04:30 AM CET #

[Trackback] Cialis. Can women take cialis.

Posted by Cialis. on March 08, 2010 at 12:28 PM CET #

[Trackback] Fosamax mg. Side effects from fosamax medication. Colorado fosamax lawyers.

Posted by Fosamax. on March 09, 2010 at 03:07 AM CET #

[Trackback] Auto insurance.

Posted by Auto owners insurance. on March 09, 2010 at 07:41 AM CET #

[Trackback] Synthroid side effects. Cost for synthroid. Skipping synthroid symptoms. Synthroid crush. Armour synthroid. Synthroid. Synthroid and orange juice.

Posted by Synthroid. on March 10, 2010 at 06:39 AM CET #

[Trackback] Pamidronate severe hypocalcemia levaquin.

Posted by Levaquin. on March 10, 2010 at 01:12 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
 

« March 2010
SunMonTueWedThuFriSat
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   
       
Today

AddThis Feed Button

 
© Morten Nobel