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 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
Posted by Viktor Sehr on June 15, 2009 at 08:27 PM CEST #
Posted by Morten Nobel-Jørgensen on June 16, 2009 at 09:46 AM CEST #
Posted by security alarm monitoring on December 10, 2009 at 06:04 AM CET #
Posted by okhndzfg on January 07, 2010 at 05:18 AM CET #
Posted by Georg Cantor on January 25, 2010 at 07:35 PM CET #
Posted by Experiencing levitra. on March 08, 2010 at 04:30 AM CET #
Posted by Cialis. on March 08, 2010 at 12:28 PM CET #
Posted by Fosamax. on March 09, 2010 at 03:07 AM CET #
Posted by Auto owners insurance. on March 09, 2010 at 07:41 AM CET #
Posted by Synthroid. on March 10, 2010 at 06:39 AM CET #
Posted by Levaquin. on March 10, 2010 at 01:12 PM CET #