Reputation: 325
I am working on a Firefox-addon that is supposed to copy snippets of an HTML document to clipboard using two flavours: text/unicode and text/html.
The code looks as follows:
function copySelection() {
var textUnicode = window.getSelection().toString();
var textHtml = window.getSelection();
var trans = Transferable(window);
trans.addDataFlavor("text/unicode");
trans.setTransferData("text/unicode", SupportsString(textUnicode), textUnicode.length * 2);
trans.addDataFlavor("text/html");
trans.setTransferData("text/html", textHtml, textHtml.length * 2); // *2 because it's unicode
Services.clipboard.setData(trans, null, Services.clipboard.kGlobalClipboard);
return true;
}
The problem is that I cannot paste the copied text OOWriter (formatted) or anywhere else (plain text). At the same time I can see with xclip, the text is copied to the cliboard, but I cannot paste it anywhere. Am I doing something wrong?
Upvotes: 0
Views: 253
Reputation: 5054
You make the false assumption that getSelection()
returns a string with the html representation of the current selection.
But the line var textHtml = window.getSelection();
simply assigns the Selection object to textHtml
.
A little more work is required.
Enumerate the selected ranges (the user might have done a multiple selection), clone each range, append the contents to a div, then the innerHTML property of that div is what you're looking for.
Keep in mind that you also have to take care of attributes with relative urls (src, href) and turn them to absolute.
Upvotes: 1