Stashed / app / electron / preload.js
preload.js
Raw
const { contextBridge, ipcRenderer } = require("electron");
const fs = require("fs");
const i18nextBackend = require("i18next-electron-fs-backend");
const Store = require("secure-electron-store").default;
const ContextMenu = require("secure-electron-context-menu").default;
const SecureElectronLicenseKeys = require("secure-electron-license-keys");

// Create the electron store to be made available in the renderer process
const store = new Store();

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld("api", {
  i18nextElectronBackend: i18nextBackend.preloadBindings(ipcRenderer),
  store: store.preloadBindings(ipcRenderer, fs),
  contextMenu: ContextMenu.preloadBindings(ipcRenderer),
  licenseKeys: SecureElectronLicenseKeys.preloadBindings(ipcRenderer),

  inventoryData: (channel, arg) => {
    let validChannels = [
      "exportInventoryData",
      "importInventoryData",
      "sendImportInventoryData",
    ];
    if (validChannels.includes(channel)) {
      if (channel == "exportInventoryData") {
        ipcRenderer.send(channel, arg);
      } else if (channel == "importInventoryData") {
        ipcRenderer.send(channel);
      } else if (channel == "receiveInventoryData") {
        ipcRenderer.on(channel, (e, ...args) => arg(...args));
      }
    }
  },
  stockxData: (channel, arg) => {
    let validChannels = [
      "getProductData",
      "receiveProductData",
      "getChartData",
      "receiveChartData",
      "getSaleData",
      "receiveSaleData",
    ];
    if (validChannels.includes(channel)) {
      if (
        channel == "getProductData" ||
        channel == "getChartData" ||
        channel == "getSaleData"
      ) {
        ipcRenderer.send(channel, arg);
      } else if (
        channel == "receiveProductData" ||
        channel == "receiveChartData" ||
        channel == "receiveSaleData"
      ) {
        ipcRenderer.on(channel, (e, ...args) => arg(...args));
      }
    }
  },
});