I'm trying to recreate (or "forge") a Skype quote with JavaScript. Therefore I need to push the XML-String to the clipboard, formatted as a "SkypeMessageFragment". The alert shows that this works out well, but investigating the clipboard with ClipView displays only one character (<), though the text/plain content is correct.
A reason might be the type of the content, which should be a MemoryStream (but this is not possible with JavaScript of course), explained here.
Has anyone an idea how to solve the problem, or isn't it possible at the time?
document.addEventListener('copy', function(e) {
var msg = $('#msg').val();
var name = $('#name').val();
var date = $('#date').val();
var time = moment(date, 'MM/DD/YYYY h:mm a').valueOf();
var skype = '<quote author="'+name+'" timestamp="'+time+'">'+msg+'</quote>';
var locale = 1033;
e.clipboardData.setData('text/plain', msg);
e.clipboardData.setData('SkypeMessageFragment', skype);
console.log('Copy');
alert(e.clipboardData.getData('SkypeMessageFragment'));
e.preventDefault();
});
I'll assume that you're using Windows, since (as far as I know) Skype quotes aren't working on other operating systems.
First of all, your code won't work in Chromium since it serializes all the clipboard data in a special type named Chromium Web Custom MIME Data Format, whereas Skype fetches the SkypeMessageFragment type.
So let's have a look to Firefox's behavior. I've used Free Clipboard Viewer to check the data Firefox is storing, and for text/plain type everything is fine. But this is what I have for the SkypeMessageFragment type:

From the first bytes, we can deduce that the encoding is UCS-2 with Little Endian, which is coherent since JavaScript engines stores strings in UCS-2 format internally. We can also see that the last byte is the null byte.
This is why you see only <, since the next byte is the null byte, so your software may think that the string is terminated.
Now, if we do the same for a real Skype quote, we can see that Skype is using UTF-8 with no trailing null byte. So it expects a UTF-8 encoded string in the clipboard, that's why your quote isn't working as expected.
We can try to force UTF-8 format using that piece of code:
function usc2_to_utf8(usc2_string) {
utf8_string = "";
for (var i = 0; i < usc2_string.length; i += 2) {
var codePoint = 0;
codePoint += usc2_string.codePointAt(i + 1);
codePoint <<= 8;
codePoint += usc2_string.codePointAt(i);
utf8_string += String.fromCharCode(codePoint);
}
return utf8_string;
}
That trick stores the data in UTF-8 format in the clipboard:

But that's not enough. The quote appears in the textarea as if it was crafted by Skype, but Skype won't recognize the quote if you post it. I guess this has to do with the 2 / 3 null bytes in the end of the string that I didn't manage to remove.
Also, note that Firefox might change how it handles custom clipboard types soon.
You might want to use Flash, but Flash will also fail, since it will prefix all the custom types.
I don't know how other extensions like Java applets work with clipboard, but my guess is that you don't want to force the user to install anything, so I'm afraid that there's no solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With