MCPcopy Create free account
hub / github.com/Yorick-Ryu/deep-share / extractConversationData

Function extractConversationData

scripts/injectDocxButton.js:226–292  ·  view source on GitHub ↗
(copyButton)

Source from the content-addressed store, hash-verified

224
225 // Helper function to extract conversation data
226 async function extractConversationData(copyButton) {
227 // Always use assistant role for docx conversion
228 let role = 'assistant';
229
230 if (copyButton) {
231 let originalClipboardContent = null;
232 try {
233 // 1. Backup original clipboard content
234 try {
235 originalClipboardContent = await navigator.clipboard.readText();
236 } catch (e) {
237 console.debug('Could not backup clipboard content (this is normal if permission not granted or clipboard empty):', e);
238 }
239
240 // 2. Click the copy button to copy content to clipboard
241 copyButton.click();
242
243 // 3. Wait for clipboard to be populated
244 await new Promise(resolve => setTimeout(resolve, 500));
245
246 // 4. Get content from clipboard
247 const clipboardContent = await navigator.clipboard.readText();
248
249 // 5. Restore original clipboard content if we successfully backed it up
250 if (originalClipboardContent !== null) {
251 try {
252 await navigator.clipboard.writeText(originalClipboardContent);
253 console.debug('Clipboard content restored');
254 } catch (e) {
255 console.warn('Failed to restore clipboard content:', e);
256 }
257 }
258
259 if (clipboardContent) {
260 console.debug('Successfully read AI response from clipboard');
261 return {
262 role: role,
263 content: clipboardContent
264 };
265 } else {
266 console.warn('Clipboard content was empty after clicking copy button');
267 window.showToastNotification(chrome.i18n?.getMessage('getClipboardError'), 'error');
268 return null;
269 }
270 } catch (error) {
271 console.error('Error reading from clipboard:', error);
272
273 // Attempt restoration even on error if we had a backup
274 if (originalClipboardContent !== null) {
275 try {
276 await navigator.clipboard.writeText(originalClipboardContent);
277 } catch (ex) { }
278 }
279
280 if (error.message && error.message.includes('Read permission denied')) {
281 showClipboardPermissionError();
282 } else {
283 window.showToastNotification(`${chrome.i18n?.getMessage('getClipboardError')}: ${error.message}`, 'error');

Callers 1

observeAndInjectButtonFunction · 0.85

Calls 5

debugMethod · 0.80
clickMethod · 0.45
warnMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected