
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@rubenverg/electron-util
Advanced tools
An electron-util port for modern Electron.
electron-utilSince I renamed some functions to be clearer, you can use @rubenverg/electron-util/compat instead. All APIs have exactly the same name there and work exactly the same (except for darkMode which has other features)
Assuming nodeIntegration is off (as it should be), you can expose the utils via the preload script. You might also want to use @rubenverg/electron-util/safe that removes the api exposure that allows access to all main functions from the renderer. Note that this depends on electron.remote, which is deprecated. I'll try switching to ipc messages.
This is the API documentation for @rubenverg/electron-util. safe has the same API except for the lack of api, and compat uses the electron-util naming scheme.
aboutfunction about(options: {
icon?: string,
copyright?: string,
website?: string,
text?: string,
title?: string
}): void
Shows an about window for the app. Note that not all options are used for all platforms, see electron-util for more detail.
aboutMenuItemfunction aboutMenuItem(options: {
icon?: string,
copyright?: string,
website?: string,
text?: string,
title?: string
}): electron.MenuItem
Returns a MenuItem named ${title || 'About'} ${appName} that calls about(options) on click.
activeWindowfunction activeWindow(): electron.BrowserWindow | null
Returns the currently focused window.
activeWindowOrFirstfunction activeWindowOrFirst(): electron.BrowserWindow | null
Returns the currently focused window, or the first one (in BrowserWindow.getAllWindows) if none is focused.
apiAccess to the electron api, whether from main or renderer. Uses electron.remote if in a renderer process. Currently not available outside compat, as it's deprecated and doesn't really work.
centerWindowfunction centerWindow(options: {
window?: electron.BrowserWindow,
size?: electron.Size,
useFullBounds?: boolean,
animated?: boolean
})
Centers the current window.
chromeVersionconst chromeVersion: string
Chromium version
nativeThemenativeTheme.isEnabledconst isEnabled: boolean
Is the app in dark mode? (Only available for compatibility, deprecated in favor of nativeTheme.dark)
nativeTheme.darkconst dark: boolean
Is the app in dark mode?
nativeTheme.highContrastconst highContrast: boolean
Is the app in high contrast mode?
nativeTheme.invertedconst inverted: boolean
Is the app in inverted colors mode?
onChangefunction onChange(callback: () => unknown): (() => void)
Call callback on native theme change. Returns a function that can be called to remove the callback.
debugInfoReturns some info useful for debugging.
disableZoomfunction disableZoom(window: electron.BrowserWindow = activeWindow): void
Disables zooming window.
electronVersionconst electronVersion: string
Electron version
fixPathAsarfunction fixPathAsar(path: string): string
Fixes the path to point to the unpacked Asar if an Asar packed app.
getWindowBoundsCenteredfunction getWindowBoundsCentered(options: {
window?: electron.BrowserWindow,
size?: electron.Size,
useFullBounds?: boolean
})
Gets the bounds of options.window as if it were centered on the screen.
isis.macosconst macos: boolean
Is the current OS macOS?
is.linuxconst linux: boolean
Is the current OS Linux?
is.windowsconst windows: boolean
Is the current OS Windows?
is.mainconst main: boolean
Is this a main process?
is.rendererconst renderer: boolean
Is this a renderer process?
is.macAppStoreconst macAppStore: boolean
Is this a Mac App Store app?
is.windowsStoreconst windowsStore: boolean
Is this a Windows Store app?
is.usingAsarconst usingAsar: boolean
Is this app Asar packaged?
is.developmentconst development: boolean
Is this Electron app in development mode?
firstLaunchfunction firstLaunch(): boolean
Is this the first time the app is ran? (Works by storing a file in app.getPath('userData'), so only counts the first time the function, or the electron-util one, is called)
githubIssuefunction githubIssue(options: {
body?: string,
title?: string,
template?: string,
labels?: string[],
milestone?: string,
assignee?: string,
projects?: string[],
repoUrl: string,
// or
user: string,
repo: string
}): void;
Opens in the user's browser a page to create a GitHub issue. Refer to new-github-issue-url for option documentation.
urlMenuItemfunction urlMenuItem(options: electron.MenuItemConstructorOptions & { url: string }): electron.MenuItem
Returns a MenuItem that, when clicked, takes to options.url in a browser. Takes the same options as new electron.MenuItem, plus a mandatory url.
byPlatformexport function platform<T>(switcher: {
aix?: T | (() => T),
android?: T | (() => T),
macos?: T | (() => T),
freebsd?: T | (() => T),
linux?: T | (() => T),
openbsd?: T | (() => T),
sunos?: T | (() => T),
windows?: T | (() => T),
cygwin?: T | (() => T),
netbsd?: T | (() => T),
default?: T | (() => T)
}): T
Returns a T by process.platform (windows instead of win32 and macos instead of darwin). Alternatively takes a function to the same type, if you do need a function to be returned use () => (yourFunction)
executeJavaScriptfunction executeJavaScript(code: string, window: electron.BrowserWindow = activeWindow()): Promise<any>
Executes code in window.
withCspfunction withCsp(csp: string, session: electron.Session): Promise<void>
Forces the content security policy csp on the session. For some reason, lines in csp must end with a semicolon. Don't ask me why, it's compatibility :)
macPreferences// This is a bit of a hard type, here's the simplified version:
function macPreferences(pane?: string, section?: string): Promise<void>
// Or if you're a TypeScript geek:
interface SystemPreferencesPanes {
universalaccess:
| 'Captioning'
| 'Hearing'
| 'Keyboard'
| 'Media_Descriptions'
| 'Mouse'
| 'Seeing_Display'
| 'Seeing_VoiceOver'
| 'Seeing_Zoom'
| 'SpeakableItems'
| 'Switch';
security:
| 'Advanced'
| 'FDE'
| 'Firewall'
| 'General'
| 'Privacy'
| 'Privacy_Accessibility'
| 'Privacy_Advertising'
| 'Privacy_AllFiles'
| 'Privacy_Assistive'
| 'Privacy_Automation'
| 'Privacy_Calendars'
| 'Privacy_Camera'
| 'Privacy_Contacts'
| 'Privacy_DesktopFolder'
| 'Privacy_Diagnostics'
| 'Privacy_DocumentsFolder'
| 'Privacy_DownloadsFolder'
| 'Privacy_LocationServices'
| 'Privacy_Microphone'
| 'Privacy_Photos'
| 'Privacy_Reminders'
| 'Privacy_ScreenCapture';
speech:
| 'Dictation'
| 'TTS';
sharing:
| 'Internet'
| 'Services_ARDService'
| 'Services_BluetoothSharing'
| 'Services_PersonalFileSharing'
| 'Services_PrinterSharing'
| 'Services_RemoteAppleEvent'
| 'Services_RemoteLogin'
| 'Services_ScreenSharing';
}
function macPreferences<Pane extends keyof SystemPreferencesPanes>(pane?: Pane, section?: SystemPreferencePanes[Pane]): Promise<void>
Shows the macOS System Preferences, optionally with pane pane and section section visible.
openUrlfunction openUrl(url: string): void
Opens url in the user's browser. Mainly for safe mode where api.shell isn't available.
FAQs
A port of `electron-util` that works in modern Electron
We found that @rubenverg/electron-util demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.