Skip to content

Commit

Permalink
fix prefix replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed Dec 1, 2024
1 parent 4a417f4 commit f2500ff
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions utils/translation/startTranslationCycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,30 @@ async function startTranslationCycle(window, node, apiKey, delay, shouldOptimize
const [srcUrl, srcWidth] = src.split(" ");
const url = new URL(srcUrl, window.location.origin);
url.hostname = originalWebsiteHostname;
if (options.domainSourcePrefix) {

// handle path relative to current pathname (that not starts with slash)
// if started with slash, it means it's relative to the root
// meanwhile if it's not started with slash, it means it's relative to the current pathname
const rawAttribute = el.getAttribute(attr);
if (options.domainSourcePrefix && !rawAttribute.startsWith("/")) {
url.pathname = `${options.domainSourcePrefix}${url.pathname}`;
}

return `${url.href} ${srcWidth}`;
}).join(", ");
el.srcset = newSrcset;
} else {
const url = new URL(el[attr]);
url.hostname = originalWebsiteHostname;
if (options.domainSourcePrefix) {

// handle path relative to current pathname (that not starts with slash)
// if started with slash, it means it's relative to the root
// meanwhile if it's not started with slash, it means it's relative to the current pathname
const rawAttribute = el.getAttribute(attr);
if (options.domainSourcePrefix && !rawAttribute.startsWith("/")) {
url.pathname = `${options.domainSourcePrefix}${url.pathname}`;
}

el[attr] = url.href;
}
})
Expand Down

0 comments on commit f2500ff

Please sign in to comment.