Skip to content

Commit

Permalink
chore: better loading tips
Browse files Browse the repository at this point in the history
  • Loading branch information
gizmo-ds committed Mar 16, 2024
1 parent 07d870f commit 4acd128
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/script-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,27 @@ async function main() {
const patched_filename = index_script_file.replace(/\.js$/, '.patched.js')
const local_patched_filename = await kv_get('patched-filename', store)
let patched_code: string | undefined

const supported_languages = {
'zh-CN': zhHans,
'zh-TW': zhHant,
}
const localization = supported_languages[navigator.language]

if (!local_patched_filename || local_patched_filename !== patched_filename) {
const loading = create_loading()
document.body.appendChild(loading)
document.querySelector('#root')?.before(loading)

const u = new URL(location.origin)
u.pathname = index_script_file
const code = await fetch(u.href).then((res) => res.text())
patched_code = await injector(code, 'vcc_auto_translate')
console.log('patched 🎉')
loading.innerText = '翻译补丁已应用 🎉'
kv_set('patched-filename', patched_filename, store)
await kv_set('patched-content', patched_code, store)
loading.remove()
}

const supported_languages = {
'zh-CN': zhHans,
'zh-TW': zhHant,
setTimeout(() => loading.remove(), 1000)
}
const localization = supported_languages[navigator.language]

const translater = new Translater(localization)
globalThis.vcc_auto_translate = translater.translate.bind(translater)
Expand All @@ -47,12 +49,13 @@ async function load_patched_code(patched_code?: string) {
document.body.appendChild(e)
}

function create_loading() {
function create_loading(text = '正在应用翻译补丁...') {
const loading = document.createElement('p')
loading.innerText = 'Loading...'
loading.innerText = text
loading.style.height = '100vh'
loading.style.textAlign = 'center'
loading.style.paddingTop = '45vh'
loading.style.fontSize = 'xxx-large'
loading.style.zIndex = '2000'
return loading
}

0 comments on commit 4acd128

Please sign in to comment.