{"size": 8085, "ext": "js", "max_stars_repo_path": "public/electron/helpers/preload.js", "max_stars_repo_name": "ConnorWidtfeldt/thorium", "max_stars_repo_stars_event_min_datetime": "2017-06-03T14:30:34.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-23T02:57:18.000Z", "max_issues_repo_path": "public/electron/helpers/preload.js", "max_issues_repo_name": "ConnorWidtfeldt/thorium", "max_issues_repo_issues_event_min_datetime": "2016-12-01T15:00:19.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-24T19:50:59.000Z", "max_forks_repo_path": "public/electron/helpers/preload.js", "max_forks_repo_name": "ConnorWidtfeldt/thorium", "max_forks_repo_forks_event_min_datetime": "2017-05-01T18:21:53.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-27T18:47:47.000Z"}
{"max_stars_count": 129, "max_issues_count": 2191, "max_forks_count": 64, "avg_line_length": 31.8307086614, "max_line_length": 139, "alphanum_fraction": 0.6585034014}
const ipcRenderer = require("electron").ipcRenderer;
const webFrame = require("electron").webFrame;
const contextBridge = require("electron").contextBridge;
let port;
let httpOnly;

let browserCount = require("electron").remote.getCurrentWindow().browserCount;

async function getPortAndHttpOnly() {
  const results = await ipcRenderer.invoke("get-port");
  port = results.port;
  httpOnly = results.httpOnly;
}
getPortAndHttpOnly();

const key = "thorium_clientPersistentId";
let clientId = sessionStorage.getItem(key);
webFrame.setVisualZoomLevelLimits(1, 1);

setClientId();

function setClient(id) {
  sessionStorage.setItem(key, id);
  localStorage.setItem("thorium_clientId", id);
  clientId = id;
}

async function setClientId() {
  const hostname = await ipcRenderer.invoke("get-hostname");

  const id = `${hostname}${browserCount > 1 ? ` (${browserCount})` : ""}`;
  const clientList = getClientList(hostname);
  const clientIndex = clientList.indexOf(clientId);
  setClient(id);
  clientList[clientIndex] = id;
  localStorage.setItem(key, JSON.stringify(clientList));
}

function getClientList(hostname) {
  let clientList = null;
  try {
    clientList = JSON.parse(localStorage.getItem(key));
  } catch {
    // It errored - it either doesn't exist or isn't JSON.
    // If it's blank, create a new one
  }
  if (!clientList) {
    clientList = [localStorage.getItem(key) || hostname];
    localStorage.setItem(key, JSON.stringify(clientList));
  }
  return clientList;
}

document.addEventListener("DOMContentLoaded", async function () {
  if (localStorage.getItem("thorium_startKiosked") !== "false") {
    if (document.getElementById("start-kiosked")) {
      document.getElementById("start-kiosked").checked = true;
    }
  }
  const autostart = await ipcRenderer.invoke("getServerAutostart");
  const autostartEl = document.getElementById("remember-server");
  if (autostartEl) {
    autostartEl.checked = Boolean(autostart);
    autostartEl.addEventListener("change", e => {
      ipcRenderer.send("setServerAutostart", e.target.checked);
    });
  }
  if (autostart) {
    const cancelAutostartEl = document.getElementById("stop-auto-start");
    cancelAutostartEl.hidden = false;
  }
});

const splashFunctions = {
  cancelAutostart: function cancelAutostart() {
    ipcRenderer.send("cancelServerAutostart");
  },

  loadPage: function loadPage(url) {
    let auto = false;
    if (document.getElementById("remember-client").checked) auto = true;
    let kiosk = document.getElementById("start-kiosked").checked;
    localStorage.setItem("thorium_startKiosked", kiosk);
    ipcRenderer.send("loadPage", {url, auto, kiosk});
    return;
  },
  startServer: function startServer() {
    let auto = false;
    if (document.getElementById("remember-server").checked) {
      auto = true;
    }
    ipcRenderer.send("startServer", auto);
    return;
  },
  getServers: function () {
    ipcRenderer.send("getServers");
  },
  serverAddress: function serverAddress() {
    let url = document
      .getElementById("server-address")
      .value.replace("/client", "");
    if (url.indexOf(":") === -1) url = `${url}:${port}`;
    let auto = false;
    if (document.getElementById("remember-client").checked) auto = true;
    ipcRenderer.send("loadPage", {url, auto});
  },
  getIpAddress: async function (cb) {
    const ipAddress = await ipcRenderer.invoke("get-ipAddress");
    cb(ipAddress, port, httpOnly);
  },
  openBrowser: async function openBrowser() {
    await getPortAndHttpOnly();

    ipcRenderer.send("open-external", printUrl());
    return;
  },
};
function printUrl() {
  return `http${httpOnly ? "" : "s"}://localhost${
    (port === 443 && !httpOnly) || (port === 80 && httpOnly) ? "" : `:${port}`
  }`;
}

contextBridge.exposeInMainWorld("splashFunctions", splashFunctions);

ipcRenderer.on("info", function (event, data) {
  const output = document.getElementById("console");
  if (output) {
    output.innerText = `${data}\n${output.innerText}`;
  }
});

const thorium = {
  sendMessage: function (arg) {
    return ipcRenderer.send("remoteMessage", arg);
  },
  getDMXDeviceList: function () {
    return ipcRenderer.invoke("get-usbDevices");
  },
  activateDMX: function (config) {
    return ipcRenderer.send("activate-dmx", config);
  },
  sendDMXValue: function (universe) {
    return ipcRenderer.send("send-dmx-value", universe);
  },
};
window.thorium = thorium;
window.thoriumFunctions = thorium;

contextBridge.exposeInMainWorld("thorium", thorium);

document.addEventListener(
  "DOMContentLoaded",
  function () {
    // Network Settings
    const httpOnlyEl = document.getElementById("http-only");
    const portEl = document.getElementById("port");
    if (httpOnlyEl) {
      httpOnlyEl.checked = httpOnly;

      httpOnlyEl.addEventListener("change", e => {
        ipcRenderer.send("set-httpOnly", e.target.checked);
        httpOnly = e.target.checked;
        thorium.httpOnly = e.target.checked;
        if (portEl.value === "443" && httpOnly) {
          portEl.value = "80";
          ipcRenderer.send("set-port", 80);
          port = 80;
          thorium.port = 80;
        } else if (portEl.value === "80" && !httpOnly) {
          portEl.value = "443";
          ipcRenderer.send("set-port", 443);
          port = 443;
          thorium.port = 443;
        }
      });
    }
    if (portEl) {
      portEl.value = port;

      portEl.addEventListener("change", e => {
        ipcRenderer.send("set-port", e.target.value);
        port = e.target.value;
        thorium.port = e.target.value;
      });
    }
    // Server Browser Open
    const openEl = document.getElementById("open-in-browser");
    if (openEl) {
      openEl.addEventListener("click", splashFunctions.openBrowser);
    }
    const kioskEl = document.getElementById("open-client-window");
    if (kioskEl) {
      kioskEl.addEventListener("click", function openClient() {
        ipcRenderer.send("loadPage", {
          url: `${printUrl()}/client`,
          kiosk: false,
          auto: false,
        });
      });
    }

    // Auto Update
    const autoUpdateEl = document.getElementById("auto-update");
    const autoUpdateLabel = document.getElementById("auto-update-label");
    const autoUpdateButton = document.getElementById("auto-update-download");
    const autoUpdateProgress = document.getElementById("download-progress");
    if (autoUpdateEl) {
      ipcRenderer.send("check-for-updates");

      ipcRenderer.on("has-updates", function (e, {oldVersion, newVersion}) {
        autoUpdateLabel.innerText = `Your version of Thorium is outdated. Current version is ${newVersion}. Your version is ${oldVersion}`;
        autoUpdateEl.classList.add("shown");
      });

      autoUpdateButton.addEventListener("click", () => {
        ipcRenderer.send("downloadAutoUpdate");
        ipcRenderer.on("download-progress", function (e, progress) {
          autoUpdateProgress.value = progress;
        });
        ipcRenderer.on("download-complete", function () {
          autoUpdateLabel.innerText = `Update Complete! Restart the app to apply the update.`;
          autoUpdateProgress.hidden = true;
        });
        autoUpdateLabel.innerText = `Update is being downloaded. Please wait.`;
        autoUpdateButton.hidden = true;
        autoUpdateProgress.hidden = false;
      });
    }
  },
  false,
);

ipcRenderer.on("clearUrl", function () {
  localStorage.setItem("thorium_url", "");
});

ipcRenderer.on("updateServers", function updateServers(e, servers) {
  if (document.getElementById("loading")) {
    if (servers.length === 0) {
      document.getElementById("loading").classList.remove("hidden");
      document.getElementById("servers").classList.add("hidden");
    } else {
      document.getElementById("loading").classList.add("hidden");
      document.getElementById("servers").classList.remove("hidden");
    }
    const markup = servers.map(
      s =>
        `<button onclick="splashFunctions.loadPage('${s.url}')">${s.name}</button>`,
    );
    document.getElementById("serverList").innerHTML = markup;
  }
});
