electron icon indicating copy to clipboard operation
electron copied to clipboard

Detecting electron

Open albertreed opened this issue 2 years ago • 1 comments

Apologies if this is in the documentation, I've looked through them and couldn't find the answer. What is the appropriate way of detecting that capacitor is running through electron? The best way I've found is that Capacitor.getPlatform() !== "web" AND Device.getInfo() (from the @capacitor/device plugin) returns an object with platform set to "web" which is pretty weird!

albertreed avatar Jul 20 '22 15:07 albertreed

You could try window.CapacitorCustomPlatform?.name === 'electron'. Or you could add the following to the preload.ts file:

import { addPlatform, setPlatform } from "@capacitor/core";

addPlatform('electron', {
	name: 'electron',
	isNativePlatform: () => true,
	getPlatform: () => 'electron',
});

setPlatform('electron');

On the most recent version Capacitor.getPlatform() === 'electron' should just work, but I have experienced Capacitor being a bit flaky with this.

jdgjsag67251 avatar Aug 02 '22 16:08 jdgjsag67251