fn_b/resources/js/utils/assetLoader.js
2026-02-12 12:36:53 +08:00

29 lines
1.2 KiB
JavaScript
Executable File

export const fInitAssetLoader = () => {
const aEvs = ["mouseover", "click", "keydown", "wheel", "touchmove", "touchstart"];
const fBoot = async () => {
aEvs.forEach(s => window.removeEventListener(s, fBoot));
// 1. 合并查询与处理
for (const oOld of document.querySelectorAll('script[type="text/lazyload"]')) {
await new Promise(fRes => {
const oNew = document.createElement("script");
// 2. 利用展开运算符批量搬运属性,跳过 type
[...oOld.attributes].forEach(a => a.name !== 'type' && oNew.setAttribute(a.name, a.value));
oNew.type = "text/javascript";
oNew.onload = oNew.onerror = fRes;
oNew.textContent = oOld.textContent; // 直接赋值,无 src 时生效
oOld.replaceWith(oNew); // 3. 现代 DOM API 直接替换
});
}
document.body.classList.add("is-animated");
window.dispatchEvent(new Event("app:ready"));
};
aEvs.forEach(s => window.addEventListener(s, fBoot, { passive: true, once: true })); // 4. once: true 自动解绑
};