function init() {
  var s = location.search;
  var url = "";
  var onnxModelName = "";
  if (s.indexOf("=") >= 0) {
    url = s.substring(s.indexOf("=") + 1);
  } else {
    console.error("使用方法:onnx.html?id=[模型ID]")
    return;
  }

  document.body.title = onnxModelName;
  var accessToken = localStorage.getItem("accessToken");
  var http = new XMLHttpRequest();
  http.responseType = "json"

  http.open('GET', MODELDOWNLOADAPI + `${url}/download`);
  http.setRequestHeader("Authorization", accessToken);
  http.onload = function () {
    if (http.status == 200) {
      var reader = proto.protobuf.Reader.create(base64ToUint8Array(http.response.data.file_content));
      var onnxProtoModel = window.proto.onnx.ModelProto.decode(reader);
      let { locations, graphs } = window.getExternalLocations(onnxProtoModel);
      let model = new window.onnx.Model(onnxProtoModel, graphs, new Map());

      let host = window;
      let view = new window.View(host);
      view.start().then((data) => {
        view.open(model, onnxModelName);
      }).catch((err) => {
        console.log(err);
      })
      window.mainView = view;
    }
  };
  http.send();
}

function base64ToUint8Array(base64String) {
  try {
    let padding = '='.repeat((4 - base64String.length % 4) % 4);
    let base64 = (base64String + padding)
      .replace(/\-/g, '+')
      .replace(/_/g, '/');
    let rawData = atob(base64);
    let outputArray = new Uint8Array(rawData.length);
    for (let i = 0; i < rawData.length; ++i) {
      outputArray[i] = rawData.charCodeAt(i);
    }
    return outputArray;
  }
  catch (e) {
    throw e;
  }
}

init();