()
| 597 | } |
| 598 | |
| 599 | function injectMdButtonToMenu() { |
| 600 | // Find the Radix menu content |
| 601 | const menuContents = document.querySelectorAll('div[role="menu"]'); |
| 602 | |
| 603 | menuContents.forEach(menuContent => { |
| 604 | // Check if already injected |
| 605 | if (menuContent.querySelector('.deepshare-menu-md-button')) { |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | console.debug('DeepShare: Injecting Markdown button into ChatGPT More menu'); |
| 610 | |
| 611 | // Create menu item element matching ChatGPT's style |
| 612 | const mdButton = document.createElement('div'); |
| 613 | mdButton.className = 'group __menu-item gap-1.5 deepshare-menu-md-button hover:bg-token-main-surface-secondary cursor-pointer px-3 py-2 rounded-xl'; |
| 614 | mdButton.setAttribute('role', 'menuitem'); |
| 615 | mdButton.setAttribute('tabindex', '0'); |
| 616 | |
| 617 | // Find an existing menu item to copy styles if needed, or just use the class |
| 618 | mdButton.innerHTML = ` |
| 619 | <div class="flex items-center justify-center group-disabled:opacity-50 group-data-disabled:opacity-50 icon"> |
| 620 | <svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" style="width: 20px; height: 20px;"> |
| 621 | <path d="M20.56 18H3.44C2.65 18 2 17.37 2 16.59V7.41C2 6.63 2.65 6 3.44 6H20.56C21.35 6 22 6.63 22 7.41V16.59C22 17.37 21.35 18 20.56 18M6.81 15.19V11.53L8.73 13.88L10.65 11.53V15.19H12.58V8.81H10.65L8.73 11.16L6.81 8.81H4.89V15.19H6.81M19.69 12H17.77V8.81H15.85V12H13.92L16.81 15.28L19.69 12Z"/> |
| 622 | </svg> |
| 623 | </div> |
| 624 | <div class="flex min-w-0 grow items-center gap-2.5"> |
| 625 | <div class="truncate">${chrome.i18n?.getMessage('saveAsMarkdown') || 'Save as Markdown'}</div> |
| 626 | </div> |
| 627 | `; |
| 628 | |
| 629 | // Append to the end of the menu |
| 630 | menuContent.append(mdButton); |
| 631 | |
| 632 | // Click event |
| 633 | mdButton.addEventListener('click', (e) => { |
| 634 | e.stopPropagation(); |
| 635 | if (!activeMessageContainer) { |
| 636 | console.error('DeepShare: No active message container found'); |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | const markdown = extractContentWithFormulas(activeMessageContainer); |
| 641 | if (markdown) { |
| 642 | downloadMarkdownFile(markdown); |
| 643 | } |
| 644 | |
| 645 | // Close menu - GPT uses Radix, clicking outside or selecting works |
| 646 | // We'll just let it be or try to find a backdrop if necessary |
| 647 | // Most Radix menus close on selection automatically if handled right |
| 648 | // But since we stopPropagation, we might need to manually close it if GPT doesn't |
| 649 | document.body.click(); |
| 650 | }); |
| 651 | }); |
| 652 | } |
| 653 | |
| 654 | function downloadMarkdownFile(content) { |
| 655 | const filename = generateFilename(content) + '.md'; |
nothing calls this directly
no test coverage detected