GithubHelp home page GithubHelp logo

Comments (3)

IT-MikeS avatar IT-MikeS commented on May 19, 2024

YOUR_PROJECT_DIR/electron/preloader.js

const { contextBridge, ipcRenderer } = require('electron');

///////Do Not Edit////////
require("./node_modules/@capacitor-community/electron/dist/electron-bridge.js");
/////////////////////////

contextBridge.exposeInMainWorld('electron', {
  ipc: { ...ipcRenderer }
})

YOUR_PROJECT_DIR/electron/src/index.ts

import { app, ipcMain } from "electron";
import { createCapacitorElectronApp } from "@capacitor-community/electron";

// The MainWindow object can be accessed via myCapacitorApp.getMainWindow()
const myCapacitorApp = createCapacitorElectronApp({
  mainWindow: {
    windowOptions: {
      webPreferences: {
        contextIsolation: true,
      }
    }
  }
});

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some Electron APIs can only be used after this event occurs.
app.on("ready", () => {
  myCapacitorApp.init();
});

// Quit when all windows are closed.
app.on("window-all-closed", function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (myCapacitorApp.getMainWindow().isDestroyed()) myCapacitorApp.init();
});

// Define any IPC or other custom functionality below here
ipcMain.on('asynchronous-message', (event, arg) => {
  console.log(arg) // prints "ping"
  event.reply('asynchronous-reply', 'pong')
})

ipcMain.on('synchronous-message', (event, arg) => {
  console.log(arg) // prints "ping"
  event.returnValue = 'pong'
})

In your Ionic App [App.vue] (used vue as example):

<template>
  <ion-app>
    <ion-router-outlet />
  </ion-app>
</template>

<script lang="ts">
import { IonApp, IonRouterOutlet } from '@ionic/vue';
import { defineComponent } from 'vue';

function isElectron() {
  return window.electron !== null && window.electron !== undefined;
}

export default defineComponent({
  name: 'App',
  components: {
    IonApp,
    IonRouterOutlet
  },
  created() {
    if (isElectron()) {
       window.electron.ipc.on('asynchronous-reply', (event, arg) => {
         console.log(arg) // prints "pong"
       })
    }
  },
  mounted() {
    if (isElectron()) {
      window.electron.ipc.send('asynchronous-message', 'ping')
      console.log(window.electron.ipc.sendSync('synchronous-message', 'ping')) // prints "pong"
    }
  },
});
</script>

This is how I do it roughly, some other stuff but from here your editor or IDE will tell you if there are any issues that relate to your particular use.

Note: Questions are better suited to the Ionic forums rather than github issues

from electron.

havenotfear avatar havenotfear commented on May 19, 2024

So i tried this and i am getting the following error: window.electron.ipc.on is not a function

image

What am I missing here?

from electron.

havenotfear avatar havenotfear commented on May 19, 2024

I was able to get this to work using this const {ipcRenderer: ipc} = require('electron-better-ipc'); as the ipc

from electron.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.