diff --git a/src/translation.js b/src/translation.js index d5dde17..07fdf71 100644 --- a/src/translation.js +++ b/src/translation.js @@ -353,7 +353,7 @@ export function extractPageContent() { return documentClone.documentElement.outerHTML; } -// Translate current page content while preserving HTML structure +// Translate current page content export async function translateCurrentPage(targetLanguage) { const pageContent = extractPageContent(); @@ -361,41 +361,12 @@ export async function translateCurrentPage(targetLanguage) { throw new Error("Unable to extract meaningful content from this page."); } - // Create a temporary DOM to work with - const parser = new DOMParser(); - const doc = parser.parseFromString(pageContent, 'text/html'); - - // Get text content from body for translation - const bodyText = doc.body.textContent || doc.body.innerText || ''; - - if (!bodyText.trim()) { - throw new Error("No text content found to translate."); - } - // Limit content length to avoid overwhelming the AI - const maxLength = 50000; + const maxLength = 50000; // Much higher limit for complete page translation const contentToTranslate = - bodyText.length > maxLength - ? `${bodyText.substring(0, maxLength)}...` - : bodyText; + pageContent.length > maxLength + ? `${pageContent.substring(0, maxLength)}...` + : pageContent; - // Translate the text content - const translatedText = await translateText(contentToTranslate, targetLanguage); - - // Replace the body content with translated text while preserving structure - const translatedDoc = parser.parseFromString(pageContent, 'text/html'); - - // Create a simple structure with the translated text - translatedDoc.body.innerHTML = ` -
-

- ${translatedDoc.title || 'Translated Page'} (${targetLanguage.toUpperCase()}) -

-
- ${translatedText.replace(/\n/g, '
')} -
-
- `; - - return translatedDoc.documentElement.outerHTML; + return await translateText(contentToTranslate, targetLanguage); }