From 272ef1cd37aa5e057a1c0386c7d5353517dcc5a6 Mon Sep 17 00:00:00 2001 From: lencx Date: Wed, 1 Feb 2023 23:48:35 +0800 Subject: [PATCH] feat: copy to clipboard (#191) --- src-tauri/src/scripts/cmd.js | 4 ++++ src-tauri/src/scripts/core.js | 25 ++++++++++++----------- src-tauri/src/scripts/export.js | 35 +++++++++++++++++++++++++++------ 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src-tauri/src/scripts/cmd.js b/src-tauri/src/scripts/cmd.js index 2b883a773..9a7f7270c 100644 --- a/src-tauri/src/scripts/cmd.js +++ b/src-tauri/src/scripts/cmd.js @@ -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; diff --git a/src-tauri/src/scripts/core.js b/src-tauri/src/scripts/core.js index c683d672b..734a277e4 100644 --- a/src-tauri/src/scripts/core.js +++ b/src-tauri/src/scripts/core.js @@ -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() }); diff --git a/src-tauri/src/scripts/export.js b/src-tauri/src/scripts/export.js index de625161b..8147c223c 100644 --- a/src-tauri/src/scripts/export.js +++ b/src-tauri/src/scripts/export.js @@ -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" ||