loadScript.js 1.87 KB
; (function () {
  /**
   * Synchronously adds scripts to the page.
   */
  function loadScript(src, onLoad) {
    const script = document.createElement('script')
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('defer', 'true');
    script.setAttribute('src', src);
    const top = document.getElementsByTagName('script')[0];

    if (onLoad != null) {
      var ready = false;

      script.onload = script.onreadystatechange = function () {
        if (!ready && (!this.readyState || this.readyState == 'complete')) {
          ready = true;
          onLoad();
        }
      };
    }

    if (top != null) {
      top.parentNode.insertBefore(script, top);
    }
  };


  function loadStyleLink(href) {
    const link = document.createElement('link')
    link.setAttribute('href', href)
    link.setAttribute('rel', 'stylesheet')
    const top = document.getElementsByTagName('link')[0];
    if (top != null) {
      top.parentNode.insertBefore(link, top);
    }
  }

  let loadScriptList = [
    { path: './js/plugin/echarts/echarts.js' },
    { path: './js/plugin/video/video.min.js' },
    { path: './js/plugin/video/flv.min.js' },
    { path: './js/plugin/video/videojs-flvjs.min.js' },
    { path: './js/plugin/fingerprint/fingerprint.js' },
    { path: './js/plugin/ace/ace.js' },
  ]


  let loadStyleList = [
    { path: './js/plugin/video/video-js.min.css' }
  ]

  const getFileNameFromPath = (path) => {
    const reg = /.*\/(.*)/g
    return path.replace(reg, '$1')
  }

  if (Enable_OSS) {
    loadScriptList = loadScriptList.map((item) => ({ ...item, path: OSS_Prefix + getFileNameFromPath(item.path) }))
    loadStyleList = loadStyleList.map((item) => ({ ...item, path: OSS_Prefix + getFileNameFromPath(item.path) }))
  }

  for (const { path } of loadStyleList) {
    loadStyleLink(path)
  }

  for (const { path } of loadScriptList) {
    loadScript(path)
  }

})();