Skip to content

Commit

Permalink
feat: copy to clipboard (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
lencx committed Feb 1, 2023
1 parent 8eade8e commit 272ef1c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src-tauri/src/scripts/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 14 additions & 11 deletions src-tauri/src/scripts/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() });
Expand Down
35 changes: 29 additions & 6 deletions src-tauri/src/scripts/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" ||
Expand Down

0 comments on commit 272ef1c

Please sign in to comment.