MozillaZine

problem with image copy to clipboard in Firefox

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
psrleo3
 
Posts: 2
Joined: April 13th, 2009, 12:47 am

Post Posted April 13th, 2009, 12:51 am

Am working on clipboard (Copy/Paste) functionality, i could able to copy the selected image to clipboard using IE but unable to do using Firefox 3.0.8 it has some security issues.The following javascript function copies the selected image to the Clipboard iN IE but not in Firefox

function CopyToClipboard()
{
var div = document.getElementById('croppedImage');
div.contentEditable = 'true';
var controlRange;
if (document.body.createControlRange)
{
controlRange = document.body.createControlRange();
controlRange.addElement(div);
controlRange.execCommand('Copy');
}
div.contentEditable = 'false';

}

Could anyone provide the javascript function such that Firefox can copy the selected image to the clipboard.

DanRaisch
Moderator

User avatar
 
Posts: 107708
Joined: September 23rd, 2004, 8:57 pm
Location: Somewhere on the East Coast

Post Posted April 13th, 2009, 5:29 am

Moving to Web Development.

trolly
Moderator

User avatar
 
Posts: 39162
Joined: August 22nd, 2005, 7:25 am

Post Posted April 13th, 2009, 8:13 am

Did you read this? http://kb.mozillazine.org/Granting_Java ... _clipboard
Normally the access to the clipboard is disabled due to security reasons.
Think for yourself. Otherwise you have to believe what other people tell you.
A society based on individualism is an oxymoron. || Freedom is at first the freedom to starve.
Constitution says: One man, one vote. Supreme court says: One dollar, one vote.

psrleo3
 
Posts: 2
Joined: April 13th, 2009, 12:47 am

Post Posted April 13th, 2009, 11:18 pm

After following the security previlages and setting the security policy files as mentioned in the url. I need the javascript function for Firefox.

In IE
function CopyToClipboard()
{
var div = document.getElementById('croppedImage');
div.contentEditable = 'true';
var controlRange;
if (document.body.createControlRange)
{
controlRange = document.body.createControlRange();
controlRange.addElement(div);
controlRange.execCommand('Copy');
}
div.contentEditable = 'false';

}

in the above javasacript document.body.createControlRange only works for IE i need the alterantive for Firefox. Is there any JS function which takes image to copy in clipboard.

trolly
Moderator

User avatar
 
Posts: 39162
Joined: August 22nd, 2005, 7:25 am

Post Posted April 14th, 2009, 1:42 am

I guess that's impossible.
Here is a solution with a supporting flash object. I do not know if they support images.
http://javascript.internet.com/forms/cl ... -copy.html
http://www.jeffothy.com/weblog/clipboard-copy/

An Addon:
https://addons.mozilla.org/en-US/firefox/addon/243

I found this code (probably the same or similar as in the addon) but it will ask for elevated privileges and it may have some faults due to its old date (2004):
Code: Select all
   // This is importent but it's not noted anywhere
   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   
   // create interface to the clipboard
   var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
   if (!clip) return;
   
   // create a transferable
   var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
   if (!trans) return;
   
   // specify the data we wish to handle. Plaintext in this case.
   trans.addDataFlavor('text/unicode');
   
   // To get the data from the transferable we need two new objects
   var str = new Object();
   var len = new Object();
   
   var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);
   
   var copytext=meintext;
   
   str.data=copytext;
   
   trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);
   
   var clipid=Components.interfaces.nsIClipboard;
   
   if (!clip) return false;
   
   clip.setData(trans,null,clipid.kGlobalClipboard);
Think for yourself. Otherwise you have to believe what other people tell you.
A society based on individualism is an oxymoron. || Freedom is at first the freedom to starve.
Constitution says: One man, one vote. Supreme court says: One dollar, one vote.

Return to Web Development / Standards Evangelism


Who is online

Users browsing this forum: No registered users and 1 guest