Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

442
Visualizações
Electron: Sending app.getPath data from the main process to the renderer process

Given:

webPreferences: {
  nodeIntegration: false,
  contextIsolation: true,
  sandbox: true,
}

I need to access the result of app.getPath("appData") in my renderer process.

app.getPath("appData") only works in the main process.

contextBridge.exposeInMainWorld() only works on the preload script.

How do I expose the result of app.getPath("appData") to the renderer if the preload script can't access app.getPath("appData") and the main process can't access contextBridge.exposeInMainWorld()?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can

  • set up the preload script to call exposeInMainWorld to put a Promise-invoking function on the window. When invoked, make a request to the main process
  • set up the main process to respond to requests from the preload script with the desired info
// Main process:
ipcMain.handle('getPath', () => app.getPath("appData"));
// Preload:
contextBridge.exposeInMainWorld('electronAPI', {
    getPath: () => ipcRenderer.invoke('getPath')
});
// Renderer:
window.electronAPI.getPath()
  .then((appDataPath) => {
    // ...
  })
  .catch(handleErrors);

The preload script essentially acts as a bridge between the sandboxed page and the privileged main process - often, most of an application's logic will live either in the main process or the renderer, and the preload only bridges them.

For a more generalized approach of sending messages from the renderer to main (possibly while waiting for a response), I've found the following to work pretty well:

// Preload:
contextBridge.exposeInMainWorld('electronAPI', {
    sendMessageToMainProcess: (channel: string, payload: unknown) => ipcRenderer.invoke(channel, payload),
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda