add copy function

This commit is contained in:
nnduc
2026-11-19 20:27:55 +07:00
parent a0499ade59
commit f99fe670ba
7 changed files with 94 additions and 11 deletions
+41
View File
@@ -923,6 +923,41 @@ var Chat = {
}
);
},
// A selection over several messages of the discussion: one message per
// line, without the times, ticks and buttons around them
copySelection: function (event) {
let selection = window.getSelection();
if (!selection.rangeCount || selection.isCollapsed) return;
let range = selection.getRangeAt(0);
let widget = document.querySelector('#chat_widget');
if (!widget || !widget.contains(range.commonAncestorContainer)) return;
// Not the OMEMO ciphertext shown while (or because it failed) decrypting
let texts = Array.from(widget.querySelectorAll('div.bubble > div.message > p:not(.encrypted)'))
.filter(p => range.intersectsNode(p));
if (texts.length < 2) return;
let lines = texts.map(p => {
let part = document.createRange();
part.selectNodeContents(p);
if (part.compareBoundaryPoints(Range.START_TO_START, range) < 0) {
part.setStart(range.startContainer, range.startOffset);
}
if (part.compareBoundaryPoints(Range.END_TO_END, range) > 0) {
part.setEnd(range.endContainer, range.endOffset);
}
let div = document.createElement('div');
div.appendChild(part.cloneContents());
div.querySelectorAll('img').forEach(img => img.replaceWith(img.alt ?? ''));
div.querySelectorAll('br').forEach(br => br.replaceWith('\n'));
return div.textContent;
}).filter(line => line.trim() != '');
event.clipboardData.setData('text/plain', lines.join('\n'));
event.preventDefault();
},
setMessagePressBehaviour: function () {
document.querySelectorAll('#chat_widget li div.bubble:not(.file) > div.message').forEach(message => {
message.onmousedown = function (e) {
@@ -1879,6 +1914,12 @@ MovimWebsocket.attach(function () {
}
});
// Once, the widget scripts are reloaded
if (!window.movimChatCopy) {
window.movimChatCopy = true;
document.addEventListener('copy', (e) => Chat.copySelection(e));
}
MovimEvents.registerWindow('focus', 'chat', () => {
if (MovimWebsocket.connection) {
var jid = MovimUtils.urlParts().params[0];