alex8088 / electron-toolkit

Toolkit for Electron
MIT License
121 stars 6 forks source link

Undefined Electron API's #11

Closed lynxionxs closed 6 months ago

lynxionxs commented 6 months ago

Describe the bug

Why is there only 3 electron api's available with import { electronAPI } from '@electron-toolkit/preload' ? process, ipcRenderer, webFrame. Example : I want to use the api for app to get the data path app.getPath('appData') . Importing app in ../preload/index.ts import { app, contextBridge } from 'electron' shows app as undefined. Now how am i supposed to get access to those other electron api's ?

Electron-Toolkit Version

3.0.0

Electron Version

28.2.0

Validations

alex8088 commented 6 months ago

app cannot be used in preload/renderer. You should use ipc to handle.

lynxionxs commented 6 months ago

@alex8088 How to use ipc to handle ? All i want is to get the app.getPath('appData') Can you give example using IPC ?

alex8088 commented 6 months ago

https://github.com/alex8088/EvPlayer

lynxionxs commented 6 months ago

@alex8088 Thanks. Is it okay to use the ipcRenderer from electron in the preload script, or must i use the ipcRenderer from electronAPI in preload script ?

../preload/index.ts

import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {
  getAppDataPath: () => ipcRenderer.invoke('get-appData-path')
}

if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}
alex8088 commented 6 months ago

https://github.com/alex8088/electron-toolkit/tree/master/packages/preload#get-started