Skip to content

Commit

Permalink
custom links
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed Oct 21, 2024
1 parent 4879ac8 commit 5340bf3
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion extractOptionsFromScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ const detectRobot = require("./utils/detectRobot");
const getPrefixedPathname = require("./utils/translation-mode/getPrefixedPathname");
const getUnprefixedPathname = require("./utils/translation-mode/getUnprefixedPathname");

function replaceCustomLinks(window, customLinks = {}) {
// find all tags with href and src
const tagsWithSrc = window.document.querySelectorAll("[src]");
const tagsWithHref = window.document.querySelectorAll("[href]");

// replace the links
for (let tag of tagsWithSrc) {
const src = tag.getAttribute("src");
if (customLinks[src]) {
tag.setAttribute("src", customLinks[src]);
}
}

for (let tag of tagsWithHref) {
const href = tag.getAttribute("href");
if (customLinks[href]) {
tag.setAttribute("href", customLinks[href]);
}
}
}

/**
* Extracts options from a script.
*
Expand Down Expand Up @@ -52,13 +73,29 @@ function extractOptionsFromScript(window, optsArgs = {
const DATA_TRANSLATE_FORM_PLACEHOLDER = "data-translate-form-placeholder"
const DATA_TRANSLATE_SELECT_OPTIONS = "data-translate-select-options"
const DATA_DOMAIN_SOURCE_PREFIX = "data-domain-source-prefix"
const DATA_CUSTOM_LINKS = "data-custom-links"

const DATA_TRANSLATION_CACHE = "data-translation-cache";

// WORKER ATTRIBUTES
// const DATA_PREVENT_INIT_TRANSLATION = "data-prevent-init-translation" // default: false
// const preventInitialTranslation = window.translationScriptTag.getAttribute(DATA_PREVENT_INIT_TRANSLATION) == "true";

const customLinksAttribute = window.translationScriptTag.getAttribute(DATA_CUSTOM_LINKS);
let customLinks = {};
try {
// format: [oldUrl,newUrl], [oldUrl,newUrl]
const parsed = JSON.parse(`[${customLinksAttribute}]`);
customLinks = parsed.reduce((acc, [oldUrl, newUrl]) => {
acc[oldUrl] = newUrl;
return acc;
}, {});

replaceCustomLinks(window, customLinks);
} catch (e) {
customLinks = {};
}

const DATA_ACTIVE_SUBDOMAIN = "data-active-subdomain"; // default: undefined
const activeSubdomain = window.translationScriptTag.getAttribute(DATA_ACTIVE_SUBDOMAIN);

Expand Down Expand Up @@ -364,7 +401,8 @@ function extractOptionsFromScript(window, optsArgs = {
paramsLang,
apiKey,
translationMode,
domainSourcePrefix
domainSourcePrefix,
customLinks,
}
}

Expand Down

0 comments on commit 5340bf3

Please sign in to comment.