in src/contentScripts/index.ts [103:148]
async function startTranslation(
creds: TranslateClientConfig,
source: string,
target: string,
startingEl: HTMLElement | null
): Promise<void> {
if (startingEl) {
// Crawl the DOM from the starting element and get the text pages and node map
const { pageMap, nodeMap } = crawl(startingEl);
const cachedTranslation = getCache(window.location.href, source, target);
if (cachedTranslation) {
// If the page has been translated previously and is cached, follow this logic tree
applyCachedTranslation(pageMap, nodeMap, cachedTranslation);
} else {
// If the page has not been previously translated and cached, get new translation and apply it
// Create translatable pages from the page map.
const writtenPages = writePages(pageMap);
// Bind the pages into documents (chunks) that can be sent to Amazon Translate
const docs = bindPages(writtenPages);
// Translate the documents
const translated = await translateDocuments(creds, source, target, docs);
// Break the translated documents back into pages
const translatedPages = breakDocuments(translated);
// Break the pages into tuples of the node ID and the translated text
const translatedPageMap = breakPages(translatedPages);
// Make a cache text map for the selected language pair
const textMap = makeCacheTextMap(pageMap, translatedPageMap);
// Cache the translated text map
cacheTranslation(window.location.href, source, target, textMap);
// Apply the translated documents to the DOM
applyTranslation(nodeMap, translatedPages);
}
} else {
throw new Error('Amazon Translate Error: The top level tag does not exist on the document.');
}
}