update remote URL fetching to use uncloseai.com CORS proxy

This commit is contained in:
Russell Ballestrini 2025-07-11 17:01:33 -04:00
parent b050bbc6bf
commit aa1285ba6c

View file

@ -4,12 +4,21 @@ import { getUIText } from "./ui-translations.js";
import { extractWebpageContent } from "./content.js";
import { translateCurrentPage, translateText, SUPPORTED_LANGUAGES } from "./translation.js";
// Function to fetch and extract content from a remote URL
// Function to fetch and extract content from a remote URL using CORS proxy
async function fetchRemotePageContent(url) {
try {
// Use a CORS proxy or fetch API with proper headers
// For now, we'll use a simple approach - in production you might want a proper CORS proxy
const response = await fetch(url, {
// Validate URL format
let targetUrl;
try {
targetUrl = new URL(url);
} catch (urlError) {
throw new Error(`Invalid URL format: ${url}`);
}
// Use our CORS proxy at uncloseai.com
const proxyUrl = `https://uncloseai.com/api/fetch/${encodeURIComponent(url)}`;
const response = await fetch(proxyUrl, {
method: 'GET',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
@ -17,7 +26,7 @@ async function fetchRemotePageContent(url) {
});
if (!response.ok) {
throw new Error(`Failed to fetch URL: ${response.status} ${response.statusText}`);
throw new Error(`Failed to fetch URL via proxy: ${response.status} ${response.statusText}`);
}
const htmlContent = await response.text();
@ -45,10 +54,7 @@ async function fetchRemotePageContent(url) {
return extractedContent;
} catch (error) {
// If direct fetch fails due to CORS, suggest alternatives
if (error.name === 'TypeError' && error.message.includes('CORS')) {
throw new Error(`Cannot fetch "${url}" due to CORS restrictions. The website doesn't allow cross-origin requests.`);
}
console.error('Remote fetch error:', error);
throw new Error(`Failed to fetch remote content: ${error.message}`);
}
}
@ -392,15 +398,19 @@ ${preservedText}`;
let translatedText;
if (isPageMode) {
if (activeTab === "page") {
translatedText = await translateCurrentPage(targetLanguage);
} else {
} else if (activeTab === "custom") {
const customText = textArea.value.trim();
if (!customText) {
alert(getUIText("pleaseEnterTranslateText"));
return;
}
translatedText = await translateText(customText, targetLanguage);
} else if (activeTab === "remote") {
// For remote URLs, we already fetched and prepared the content
// Use the same translateText function with the fetched content
translatedText = await translateText(rawContent, targetLanguage);
}
resultLabel.textContent = `Translation (${targetLanguageName}):`;
@ -545,9 +555,14 @@ ${preservedText}`;
}
alert(error.message);
} finally {
translateButton.textContent = isPageMode
? "🌐 Translate Page"
: "🌐 Translate Text";
// Reset button text based on active tab
if (activeTab === "page") {
translateButton.textContent = getUIText("translatePageButton");
} else if (activeTab === "custom") {
translateButton.textContent = getUIText("translateTextButton");
} else if (activeTab === "remote") {
translateButton.textContent = getUIText("fetchAndTranslate");
}
translateButton.disabled = false;
}
};