diff options
| author | Bobby <[email protected]> | 2024-04-18 22:26:32 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-04-18 22:26:32 -0400 |
| commit | 9f46bf5238c8c177bbcf02783600975e0ef245c2 (patch) | |
| tree | fc28218caf29bbc0e23adc6299b8c2f7993212a5 /static/js | |
| parent | 3721a4d2ca2eca8f0d43b9650627fb74da6d1428 (diff) | |
| download | thatcomputerscientist-9f46bf5238c8c177bbcf02783600975e0ef245c2.tar.xz thatcomputerscientist-9f46bf5238c8c177bbcf02783600975e0ef245c2.zip | |
Translated Pages are Cached
Diffstat (limited to 'static/js')
| -rw-r--r-- | static/js/db.js | 20 | ||||
| -rw-r--r-- | static/js/tl.js | 29 |
2 files changed, 49 insertions, 0 deletions
diff --git a/static/js/db.js b/static/js/db.js new file mode 100644 index 00000000..2cffa80c --- /dev/null +++ b/static/js/db.js @@ -0,0 +1,20 @@ +function savePageToDB(entirePage, path) { + window.sessionStorage.setItem(path, entirePage); + // db.collection('pages').doc(path).set({ path: path, page: entirePage }); +} + +const lang = localStorage.getItem("lang"); +const path = window.location.pathname; + +const storedPage = window.sessionStorage.getItem(path); + +if (lang === "ja" && storedPage) { + console.log("Page already translated"); + document.getElementById('wrap').innerHTML = storedPage; +} else { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.src = + "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"; + document.getElementById("tl_block").appendChild(script); +} diff --git a/static/js/tl.js b/static/js/tl.js index 44c8b044..df6c69eb 100644 --- a/static/js/tl.js +++ b/static/js/tl.js @@ -45,6 +45,9 @@ function translateJapanese() { } else { selectEl.value = 'ja'; selectEl.dispatchEvent(new Event("change")); + setTimeout(function () { + save(); + }, 6969); } } @@ -59,6 +62,9 @@ if (currentLang === "ja") { $('#tl_ja').show(); $('body').addClass('ja'); $('body').removeClass('en'); + setTimeout(function () { + save(); + }, 6969); } else { $('#tl_ja').hide(); $('#tl_en').show(); @@ -66,3 +72,26 @@ if (currentLang === "ja") { $('body').removeClass('ja'); restoreLang(); } + +// let entirePage; + +function save() { + // only body + const entirePage = document.getElementById('wrap').innerHTML; + const path = window.location.pathname; + + const storedPage = window.sessionStorage.getItem(path); + + if (storedPage === null || storedPage !== entirePage) { + console.log('Saving page to DB'); + window.sessionStorage.setItem(path, entirePage); + } + + // db.collection('pages').doc(path).get().then(page => { + // const storedPage = page?.page; + // if (storedPage === null || storedPage !== entirePage) { + // console.log('Saving page to DB'); + // savePageToDB(entirePage, path); + // } + // }); +} |
