In a Firefox extension, how can I copy rich text/links to the clipboard?

限于喜欢 提交于 2020-01-01 07:16:05

问题


Specifically, I want to copy a link (with text and location) and then to be able to paste it, e.g., into Word as a link.


回答1:


dig around inside Download of the Day: AutoCopy Firefox extension or Clipboard-Save-As 1.0.4




回答2:


Here's the actual code:

var richText = "<a href=\"" + gContextMenu.linkURL + "\">" + gContextMenu.linkText() + "</a>";
var xfer = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
xfer.addDataFlavor("text/html");

var htmlString = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
htmlString.data = richText;
xfer.setTransferData("text/html", htmlString, richText.length * 2);

var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
clipboard.setData(xfer, null, Components.interfaces.nsIClipboard.kGlobalClipboard);

It is also recommended to create another Components.interfaces.nsISupportsString, whose data is plain text, and add it to the same xfer as text/unicode



来源:https://stackoverflow.com/questions/218462/in-a-firefox-extension-how-can-i-copy-rich-text-links-to-the-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!