如何在XUL应用程序中将图像数据复制到剪贴板?

时间:2022-03-08 23:07:57

I have a XULRunner application that needs to copy image data to the clipboard. I have figured out how to handle copying text to the clipboard, and I can paste PNG data from the clipboard. What I can't figure out is how to get data from a data URL into the clipboard so that it can be pasted into other applications.

我有一个XULRunner应用程序需要将图像数据复制到剪贴板。我已经弄清楚如何处理将文本复制到剪贴板,我可以从剪贴板粘贴PNG数据。我无法弄清楚的是如何将数据从数据URL导入剪贴板,以便将其粘贴到其他应用程序中。

This is the code I use to copy text (well, XUL):

这是我用来复制文本的代码(嗯,XUL):

var transferObject=Components.classes["@mozilla.org/widget/transferable;1"].
    createInstance(Components.interfaces.nsITransferable);

var stringWrapper=Components.classes["@mozilla.org/supports-string;1"].
    createInstance(Components.interfaces.nsISupportsString);

var systemClipboard=Components.classes["@mozilla.org/widget/clipboard;1"].
    createInstance(Components.interfaces.nsIClipboard);

var objToSerialize=aDOMNode;

transferObject.addDataFlavor("text/xul");

var xmls=new XMLSerializer();
var serializedObj=xmls.serializeToString(objToSerialize);

stringWrapper.data=serializedObj;

transferObject.setTransferData("text/xul",stringWrapper,serializedObj.length*2);

And, as I said, the data I'm trying to transfer is a PNG as a data URL. So I'm looking for the equivalent to the above that will allow, e.g. Paint.NET to paste my app's data.

而且,正如我所说,我试图传输的数据是PNG作为数据URL。所以我正在寻找与上述相同的内容,例如Paint.NET粘贴我的应用程序的数据。

2 个解决方案

#1


3  

Here's a workaround that I ended up using that solves the problem pretty well. The variable dataURL is the image I was trying to get to the clipboard in the first place.

这是我最终使用的一种解决方法,可以很好地解决问题。变量dataURL是我试图首先进入剪贴板的图像。

var newImg=document.createElement('img');
newImg.src=dataURL;

document.popupNode=newImg;

var command='cmd_copyImageContents'

var controller=document.commandDispatcher.getControllerForCommand(command);

if(controller && controller.isCommandEnabled(command)){
    controller.doCommand(command);
}

That copies the image to the clipboard as an 'image/jpg'.

将图像作为“图像/ jpg”复制到剪贴板。

#2


2  

Neal Deakin has an article on manipulating the clipboard in xulrunner. I'm not sure if it answers your question specifically, but it's definitely worth checking out.

Neal Deakin有一篇关于在xulrunner中操纵剪贴板的文章。我不确定它是否具体回答了你的问题,但绝对值得一试。

#1


3  

Here's a workaround that I ended up using that solves the problem pretty well. The variable dataURL is the image I was trying to get to the clipboard in the first place.

这是我最终使用的一种解决方法,可以很好地解决问题。变量dataURL是我试图首先进入剪贴板的图像。

var newImg=document.createElement('img');
newImg.src=dataURL;

document.popupNode=newImg;

var command='cmd_copyImageContents'

var controller=document.commandDispatcher.getControllerForCommand(command);

if(controller && controller.isCommandEnabled(command)){
    controller.doCommand(command);
}

That copies the image to the clipboard as an 'image/jpg'.

将图像作为“图像/ jpg”复制到剪贴板。

#2


2  

Neal Deakin has an article on manipulating the clipboard in xulrunner. I'm not sure if it answers your question specifically, but it's definitely worth checking out.

Neal Deakin有一篇关于在xulrunner中操纵剪贴板的文章。我不确定它是否具体回答了你的问题,但绝对值得一试。