feat: copy to clipboard (#191)

This commit is contained in:
lencx
2023-02-01 23:48:35 +08:00
parent 8eade8e9b9
commit 272ef1cd37
3 changed files with 47 additions and 17 deletions

View File

@@ -75,6 +75,10 @@ function init() {
width: 22px;
height: 22px;
}
.chatappico.copy {
width: 16px;
height: 16px;
}
@media screen and (max-width: 767px) {
#download-png-button, #download-pdf-button, #download-html-button {
display: none;

View File

@@ -36,8 +36,22 @@ async function invoke(cmd, args) {
});
}
async function message(message) {
invoke('messageDialog', {
__tauriModule: 'Dialog',
message: {
cmd: 'messageDialog',
message: message.toString(),
title: null,
type: null,
buttonLabel: null
}
});
}
window.uid = uid;
window.invoke = invoke;
window.message = message;
window.transformCallback = transformCallback;
async function init() {
@@ -87,17 +101,6 @@ async function init() {
}
});
document.addEventListener('wheel', function(event) {
const deltaX = event.wheelDeltaX;
if (Math.abs(deltaX) >= 50) {
if (deltaX > 0) {
window.history.go(-1);
} else {
window.history.go(1);
}
}
});
if (window.location.host === 'chat.openai.com') {
window.__sync_prompts = async function() {
await invoke('sync_prompts', { time: Date.now() });

View File

@@ -275,14 +275,37 @@ function setIcon(type) {
}
function copyBtns() {
document.querySelectorAll("main >div>div>div>div>div").forEach(i => {
if (!/flex-shrink/i.test(i.getAttribute('class'))) return;
const btn = i.querySelector('button').cloneNode(true);
btn.innerHTML = setIcon('copy');
i.querySelector('.self-end').appendChild(btn);
})
Array.from(document.querySelectorAll("main >div>div>div>div>div"))
.forEach(i => {
if (i.querySelector('.chat-item-copy')) return;
if (!i.querySelector('button.rounded-md')) return;
const btn = i.querySelector('button.rounded-md').cloneNode(true);
btn.classList.add('chat-item-copy');
btn.title = 'Copy to clipboard';
btn.innerHTML = setIcon('copy');
i.querySelector('.self-end').appendChild(btn);
btn.onclick = () => {
copyToClipboard(i?.innerText?.trim() || '');
}
})
}
function copyToClipboard(text) {
if (navigator.clipboard) {
navigator.clipboard.writeText(text);
} else {
var textarea = document.createElement('textarea');
document.body.appendChild(textarea);
textarea.style.position = 'fixed';
textarea.style.clip = 'rect(0 0 0 0)';
textarea.style.top = '10px';
textarea.value = text;
textarea.select();
document.execCommand('copy', true);
document.body.removeChild(textarea);
}
message('Copied to clipboard');
}
if (
document.readyState === "complete" ||