diff --git a/src/translation.js b/src/translation.js index 06f72a1..639ecb4 100644 --- a/src/translation.js +++ b/src/translation.js @@ -558,7 +558,7 @@ export async function translateHTML(htmlContent, targetLanguage, sourceUrl = nul }); // Process chunks in batches to avoid overwhelming the API - const batchSize = 2; // Limit concurrent requests + const batchSize = 4; // Increased for faster processing const translatedChunks = []; for (let i = 0; i < chunks.length; i += batchSize) { @@ -587,8 +587,14 @@ export async function translateHTML(htmlContent, targetLanguage, sourceUrl = nul const originalParser = new DOMParser(); const originalDoc = originalParser.parseFromString(processedContent, 'text/html'); + // Extract only the body content from each translated chunk to avoid stacking full HTML documents + const cleanedChunks = translatedChunks.map(chunk => { + const chunkDoc = new DOMParser().parseFromString(chunk, 'text/html'); + return chunkDoc.body ? chunkDoc.body.innerHTML : chunk; + }); + // Replace the body content with reassembled translated chunks - const reassembledBodyContent = translatedChunks.join(''); + const reassembledBodyContent = cleanedChunks.join(''); if (originalDoc.body) { originalDoc.body.innerHTML = reassembledBodyContent; } else {