Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加Hummer.loadScriptWithUrl #23

Open
chenyulun opened this issue Jul 8, 2022 · 0 comments
Open

添加Hummer.loadScriptWithUrl #23

chenyulun opened this issue Jul 8, 2022 · 0 comments

Comments

@chenyulun
Copy link

class {
inProgress = {};
loadScriptWithUrl(url, done,key) { // webpack里面有key参数,用于表示是否用户主动加载了js,直接监听,不需要创建添加
if (inProgress[url]) {
        inProgress[url].push(done);
        return;
      }
      let script, needAttach;
      if (key !== undefined) {
        const scripts = document.getElementsByTagName("script");
        for (let i = 0; i < scripts.length; i++) {
          const s = scripts[i];
          if (s.getAttribute("src") == url) {
            script = s;
            break;
          }
        }
      }
      if (!script) {
        needAttach = true;
        script = document.createElement("script");

        script.charset = "utf-8";
        script.timeout = 120;
        // if (__webpack_require__.nc) {
          // script.setAttribute("nonce", __webpack_require__.nc);
       //  }

        script.src = url;
      }
      inProgress[url] = [done];
      const onScriptComplete = (prev, event, error) => {
        // avoid mem leaks in IE.
        script.onerror = script.onload = null;
        clearTimeout(timeout);
        const doneFns = inProgress[url];
        delete inProgress[url];
        script.parentNode && script.parentNode.removeChild(script);
        doneFns && doneFns.forEach((fn) => fn(error));
        if (prev) return prev(event);
      };
      const timeout = setTimeout(
        onScriptComplete.bind(null, undefined, {
          type: "timeout",
          target: script,
        }),
        120000
      );
      script.onerror = onScriptComplete.bind(null, script.onerror, {'errCode': '404', 'errMsg': 'Not Found'});
      script.onload = onScriptComplete.bind(null, script.onload, null);
      needAttach && document.head.appendChild(script);
    };
}
}

代码都是webpack里面的,帮忙添加一下发布,谢谢了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant