convert all relative URLs to absolute URLs in remote translations
This commit is contained in:
parent
46a7a8c92e
commit
a340ea4f75
1 changed files with 45 additions and 3 deletions
|
|
@ -82,10 +82,52 @@ async function translateRemoteHTML(htmlContent, targetLanguage, sourceUrl) {
|
|||
doc.documentElement.innerHTML = translatedContent;
|
||||
}
|
||||
|
||||
// Add base tag for proper resource resolution (like translateCurrentPage does)
|
||||
const baseUrl = new URL('.', sourceUrl).href; // Get base URL (without filename)
|
||||
// Convert all relative URLs to absolute URLs
|
||||
const baseUrl = new URL('.', sourceUrl).href;
|
||||
|
||||
// Remove any existing base tag
|
||||
// Convert relative URLs in img src attributes
|
||||
doc.querySelectorAll('img[src]').forEach(img => {
|
||||
if (!img.src.startsWith('http') && !img.src.startsWith('//') && !img.src.startsWith('data:')) {
|
||||
img.src = new URL(img.getAttribute('src'), sourceUrl).href;
|
||||
}
|
||||
});
|
||||
|
||||
// Convert relative URLs in link href attributes (CSS, etc.)
|
||||
doc.querySelectorAll('link[href]').forEach(link => {
|
||||
if (!link.href.startsWith('http') && !link.href.startsWith('//')) {
|
||||
link.href = new URL(link.getAttribute('href'), sourceUrl).href;
|
||||
}
|
||||
});
|
||||
|
||||
// Convert relative URLs in script src attributes
|
||||
doc.querySelectorAll('script[src]').forEach(script => {
|
||||
if (!script.src.startsWith('http') && !script.src.startsWith('//')) {
|
||||
script.src = new URL(script.getAttribute('src'), sourceUrl).href;
|
||||
}
|
||||
});
|
||||
|
||||
// Convert relative URLs in a href attributes
|
||||
doc.querySelectorAll('a[href]').forEach(link => {
|
||||
if (!link.href.startsWith('http') && !link.href.startsWith('//') && !link.href.startsWith('#') && !link.href.startsWith('mailto:') && !link.href.startsWith('tel:')) {
|
||||
link.href = new URL(link.getAttribute('href'), sourceUrl).href;
|
||||
}
|
||||
});
|
||||
|
||||
// Convert background images in style attributes
|
||||
doc.querySelectorAll('[style*="background"]').forEach(el => {
|
||||
const style = el.getAttribute('style');
|
||||
if (style && style.includes('url(')) {
|
||||
const updatedStyle = style.replace(/url\(['"]?([^'"]+)['"]?\)/g, (match, url) => {
|
||||
if (!url.startsWith('http') && !url.startsWith('//') && !url.startsWith('data:')) {
|
||||
return `url('${new URL(url, sourceUrl).href}')`;
|
||||
}
|
||||
return match;
|
||||
});
|
||||
el.setAttribute('style', updatedStyle);
|
||||
}
|
||||
});
|
||||
|
||||
// Add base tag as fallback for any missed URLs
|
||||
const existingBase = doc.querySelector('base');
|
||||
if (existingBase) {
|
||||
existingBase.remove();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue