export const addScriptSync = async (path: string, id: string) => { if (document.getElementById(id)) { return false; } const xhrObj = new XMLHttpRequest(); xhrObj.open("GET", path, false); xhrObj.setRequestHeader("Accept", "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"); xhrObj.send(""); const scriptElement = document.createElement("script"); scriptElement.type = "text/javascript"; scriptElement.text = xhrObj.responseText; scriptElement.id = id; document.head.appendChild(scriptElement); if (typeof Lute === "undefined") { // 鸿蒙系统上第一次加载会出现 Lute 未定义的情况,重新载入一次就好了,暂时没找到原因,先这样处理 window.location.reload(); } }; export const addScript = (path: string, id: string) => { return new Promise((resolve) => { if (document.getElementById(id)) { // 脚本加载后再次调用直接返回 resolve(false); return false; } const scriptElement = document.createElement("script"); scriptElement.src = path; scriptElement.async = true; // 循环调用时 Chrome 不会重复请求 js document.head.appendChild(scriptElement); scriptElement.onload = () => { if (document.getElementById(id)) { // 循环调用需清除 DOM 中的 script 标签 scriptElement.remove(); resolve(false); return false; } scriptElement.id = id; resolve(true); }; }); };