Skip to content

Commit

Permalink
execute immediately if dom already loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed May 31, 2024
1 parent 54d717e commit 016c262
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
17 changes: 13 additions & 4 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,22 @@ if (isBrowser()) {
dynamicTranslation
}

document.addEventListener("DOMContentLoaded", function() {

function initWeploy() {
// replace links with lang (for SEO purposes)
if (paramsLang && (paramsLang != originalLang)) {
replaceLinks(paramsLang);
}

getTranslations(apiKey, options)
});
}

console.log("document.readyState", document.readyState)
if (!document.readyState || document.readyState == 'complete') {
// DOM is already loaded, run the code
initWeploy();
} else {
// DOM is not loaded yet, wait for it
document.addEventListener("DOMContentLoaded", function() {
initWeploy();
});
}
}
19 changes: 9 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,15 @@ function filterValidTextNodes(textNodes) {
}

async function isStillSameLang(language) {
return true;
// const search = window.location.search;
// const params = new URLSearchParams(search);
// const activeLang = params.get('lang') || await getLanguageFromLocalStorage();

// if (activeLang != language) {
// return false;
// } else {
// return true;
// }
const search = window.location.search;
const params = new URLSearchParams(search);
const activeLang = params.get('lang') || await getLanguageFromLocalStorage();

if (activeLang != language) {
return false;
} else {
return true;
}
}

function translateNodes(textNodes = [], language = "", apiKey = "", seoNodes = []) {
Expand Down

0 comments on commit 016c262

Please sign in to comment.