thelounge/client/js/clipboard.js
2019-11-25 20:13:12 +02:00

31 lines
728 B
JavaScript

"use strict";
export default function(chat) {
// Disable in Firefox as it already copies flex text correctly
if (typeof window.InstallTrigger !== "undefined") {
return;
}
const selection = window.getSelection();
// If selection does not span multiple elements, do nothing
if (selection.anchorNode === selection.focusNode) {
return;
}
const range = selection.getRangeAt(0);
const documentFragment = range.cloneContents();
const div = document.createElement("div");
div.id = "js-copy-hack";
div.appendChild(documentFragment);
chat.appendChild(div);
selection.selectAllChildren(div);
window.setTimeout(() => {
chat.removeChild(div);
selection.removeAllRanges();
selection.addRange(range);
}, 0);
}