(message, sourceButton, documentTitle = null)
| 18 | |
| 19 | // Function to handle the conversion process |
| 20 | async function convertToDocx(message, sourceButton, documentTitle = null) { |
| 21 | console.debug('Starting DOCX conversion'); |
| 22 | |
| 23 | // Check if message is a Promise and await it |
| 24 | if (message instanceof Promise) { |
| 25 | try { |
| 26 | message = await message; |
| 27 | } catch (error) { |
| 28 | console.error('Error resolving message Promise:', error); |
| 29 | window.showToastNotification('Error preparing content for conversion', 'error'); |
| 30 | return; |
| 31 | } |
| 32 | } else { |
| 33 | console.debug('Message:', message); |
| 34 | } |
| 35 | |
| 36 | // Check for API key first |
| 37 | try { |
| 38 | const settings = await chrome.storage.sync.get({ |
| 39 | docxApiKey: '' |
| 40 | }); |
| 41 | |
| 42 | // If no API key is set and using API mode, show notification and open popup |
| 43 | if (!settings.docxApiKey || settings.docxApiKey.trim() === '') { |
| 44 | window.showToastNotification( |
| 45 | chrome.i18n?.getMessage('apiKeyMissing') || '请购买或填写API-Key以使用文档转换功能', |
| 46 | 'error', |
| 47 | 5000 |
| 48 | ); |
| 49 | |
| 50 | // Try to open the extension popup after a short delay to allow toast to be seen |
| 51 | setTimeout(() => { |
| 52 | try { |
| 53 | chrome.runtime?.sendMessage({ |
| 54 | action: 'openPopup', |
| 55 | actionParam: 'apiKeyMissing' |
| 56 | }).catch(err => { |
| 57 | console.warn('Could not open popup automatically:', err.message); |
| 58 | }); |
| 59 | } catch (err) { |
| 60 | console.error('Failed to send message to open popup:', err); |
| 61 | } |
| 62 | }, 1000); |
| 63 | return; |
| 64 | } |
| 65 | } catch (error) { |
| 66 | console.error('Error checking API key:', error); |
| 67 | } |
| 68 | |
| 69 | let convertingNotificationId = null; |
| 70 | |
| 71 | // Disable the DOCX button if provided |
| 72 | if (sourceButton && sourceButton instanceof Element) { |
| 73 | sourceButton.style.opacity = '0.5'; |
| 74 | sourceButton.style.pointerEvents = 'none'; |
| 75 | sourceButton.setAttribute('disabled', 'true'); |
| 76 | // Store original title and update with converting message |
| 77 | sourceButton._originalTitle = sourceButton.title || chrome.i18n?.getMessage('docxButton'); |
no test coverage detected