36 lines
1.2 KiB
JavaScript
36 lines
1.2 KiB
JavaScript
const Clipboard = {
|
|
_createTextArea(tagName) {
|
|
if (tagName == null) { tagName = "textarea"; }
|
|
const textArea = document.createElement(tagName);
|
|
textArea.style.position = "absolute";
|
|
textArea.style.left = "-100%";
|
|
textArea.contentEditable = "true";
|
|
return textArea;
|
|
},
|
|
|
|
// http://groups.google.com/group/chromium-extensions/browse_thread/thread/49027e7f3b04f68/f6ab2457dee5bf55
|
|
copy({data}) {
|
|
const textArea = this._createTextArea();
|
|
textArea.value = data.replace(/\xa0/g, " ");
|
|
|
|
document.body.appendChild(textArea);
|
|
textArea.select();
|
|
document.execCommand("Copy");
|
|
document.body.removeChild(textArea);
|
|
},
|
|
|
|
// Returns a string representing the clipboard contents. Supports rich text clipboard values.
|
|
paste() {
|
|
const textArea = this._createTextArea("div"); // Use a <div> so Firefox pastes rich text.
|
|
document.body.appendChild(textArea);
|
|
textArea.focus();
|
|
document.execCommand("Paste");
|
|
const value = textArea.innerText;
|
|
document.body.removeChild(textArea);
|
|
// When copying characters, they get converted to \xa0. Convert to space instead. See #2217.
|
|
return value.replace(/\xa0/g, " ");
|
|
}
|
|
};
|
|
|
|
|
|
global.Clipboard = Clipboard;
|