add document, fix tooltip position issue

This commit is contained in:
2023-03-30 17:26:59 +08:00
parent f5bc42fa20
commit 5406eea20e
15 changed files with 432 additions and 117 deletions

View File

@ -45,23 +45,24 @@ function getStorageKey(lgid) {
return `res_${lgid}`;
}
async function doRefreshLgres() {
async function doRefreshLgres(template) {
template ??= '';
const lgid = getCurrentLgId();
const r = await get(`language/${lgid}/res.json`);
const r = await get(`language/${lgid}${template}`);
const dict = await r.json();
localStorage.setItem(getStorageKey(lgid), JSON.stringify(dict));
cache = dict;
return dict;
}
async function refreshLgres(lgres) {
async function refreshLgres(template, lgres) {
if (lgres == null || typeof consts === 'undefined') {
return await doRefreshLgres();
return await doRefreshLgres(template);
}
const ver = Number(consts.resver);
if (isNaN(lgres.ver) || isNaN(ver) || ver > lgres.ver) {
console.log(`found new language res version: ${lgres.ver} => ${ver}`);
return await doRefreshLgres();
return await doRefreshLgres(template);
}
cache = lgres;
return lgres;
@ -97,42 +98,40 @@ function applyLanguage(dom, result) {
}
}
async function init(dom, ahead) {
async function init(dom, options) {
options ??= {};
const lgid = getCurrentLgId();
let lgres = localStorage.getItem(getStorageKey(lgid));
let result;
if (lgres != null) {
try {
lgres = JSON.parse(lgres);
result = await refreshLgres(lgres);
result = await refreshLgres(options.template, lgres);
} catch (e) {
console.error('error while parsing lgres, try refresh ...', e);
result = await refreshLgres();
result = await refreshLgres(options.template);
}
} else {
result = await refreshLgres();
result = await refreshLgres(options.template);
}
try {
if (ahead != null) {
// not in defer mode
if (document.readyState === 'loading') {
return await new Promise((resolve, reject) => {
let tid = setTimeout(() => reject('timeout'), 30000);
document.addEventListener('DOMContentLoaded', () => {
clearTimeout(tid);
tid = void 0;
if (typeof ahead.callback === 'function') {
ahead.callback(result);
}
applyLanguage(dom, result);
resolve(result);
});
if (document.readyState === 'loading') {
return await new Promise((resolve, reject) => {
let tid = setTimeout(() => reject('timeout'), 30000);
document.addEventListener('DOMContentLoaded', () => {
clearTimeout(tid);
tid = void 0;
if (typeof options.callback === 'function') {
options.callback(result);
}
applyLanguage(dom, result);
resolve(result);
});
}
if (typeof ahead.callback === 'function') {
ahead.callback(result);
}
});
}
if (typeof options.callback === 'function') {
options.callback(result);
}
applyLanguage(dom, result);
return result;