CopyCutPaste.js
Summary
Allows Mozile to do some basic Copy&Paste operations. In an unprivileged environment it will use a local clipboard. In a privileged enviromnent the system clipboard will be used.
Version: 0.7
Author: David Palm
mozile.getModule("CopyCutPaste").clipboard = "";
mozile.getModule("CopyCutPaste").getClipboard = function() {
var result = "";
if(mozile.isEnhanced("getClipboard")) {
mozile.setSharedData("clipboard", "");
mozile.clientRequest("getClipboard", "if(mozile.getSharedData('clipboard')) mozileEditor.insertString(mozile.getSharedData('clipboard'))");
return result;
}
else if(!mozile.isExtension() && !mozile.getModule("CopyCutPaste").getOption("requestPermissions") ) {
if(this.clipboard) result = this.clipboard;
}
else {
try{netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalBrowserWrite ");}
catch(e){ return result; }
var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
if (!clip) return result;
var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
if (!trans) return result;
trans.addDataFlavor("text/unicode");
clip.getData(trans,clip.kGlobalClipboard);
var str = new Object();
var strLength = new Object();
trans.getTransferData("text/unicode",str,strLength);
if(str) str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
else return result;
if (str) result = str.data.substring(0,strLength.value / 2);
}
return result;
}
mozile.getModule("CopyCutPaste").setClipboard = function(content) {
if(mozile.isEnhanced("setClipboard")) {
mozile.setSharedData("clipboard", content);
mozile.setSharedData("clientRequest", "setClipboard");
}
if(!mozile.isExtension() && !mozile.getModule("CopyCutPaste").getOption("requestPermissions") ) {
this.clipboard = content;
}
else {
try{netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalBrowserWrite ");}
catch(e){ return ""; }
const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
if(gClipboardHelper) gClipboardHelper.copyString(content);
else this.clipboard = content;
}
return content;
}
mozile.getModule("CopyCutPaste").copy = function(){
var copyStr = window.getSelection().toString();
this.setClipboard(copyStr);
return copyStr;
}
mozile.getModule("CopyCutPaste").cut = function(){
var copyStr = this.copy() ;
if(copyStr){
window.getSelection().deleteContents();
return copyStr;
}
else{
mozile.debug(f,0,"Cut command failed. Maybe a problem in copy()?");
return false;
}
}
mozile.getModule("CopyCutPaste").paste = function(){
var clipboard = this.getClipboard();
if(clipboard && clipboard != "") mozileEditor.insertString(clipboard);
return clipboard;
}
mozile.getModule("CopyCutPaste").init = function(){
var mozileCopy = mozile.getCommandList().createCommand("MozileCommand: id=Mozile-Copy, label=Copy, tooltip='Copy selection', accelerator='Command-C', image='"+mozile.getRoot()+"images/copy.png'");
mozileCopy.execute = function() {
mozile.getModule("CopyCutPaste").copy();
return true;
}
var mozileCut = mozile.getCommandList().createCommand("MozileCommand: id=Mozile-Cut, label=Cut, tooltip='Cut selection', accelerator='Command-X', image='"+mozile.getRoot()+"images/cut.png'");
mozileCut.execute = function() {
mozile.getModule("CopyCutPaste").cut();
return true;
}
var mozilePaste = mozile.getCommandList().createCommand("MozileCommand: id=Mozile-Paste, label=Paste, tooltip='Paste selection', accelerator='Command-V', image='"+mozile.getRoot()+"images/paste.png'");
mozilePaste.execute = function() {
mozile.getModule("CopyCutPaste").paste();
return true;
}
mozile.getCommandList().createCommand("MozileCommandSeparator: id=Mozile-CopySeparator");
}
Documentation generated by
JSDoc on Tue Apr 18 14:14:33 2006