Chrome extension API support for Electron, built for Peersky.
Npm: @p2plabs/peersky-chrome-extensions.
Electron provides basic support for Chrome extensions out of the box. However, it only supports a subset of APIs with a focus on DevTools. Concepts like tabs, popups, and extension actions aren't known to Electron.
This library aims to bring extension support in Electron up to the level you'd come to expect from a browser like Google Chrome. API behavior is customizable so you can define how to handle things like tab or window creation specific to your application's needs.
npm install @p2plabs/peersky-chrome-extensions
| VeePN | Ghostery Reader |
|---|---|
![]() |
![]() |
Simple browser using Electron's default session and one tab.
const { app, BrowserWindow } = require('electron')
const { ElectronChromeExtensions } = require('@p2plabs/peersky-chrome-extensions')
app.whenReady().then(() => {
const extensions = new ElectronChromeExtensions({
license: 'GPL-3.0',
})
const browserWindow = new BrowserWindow()
// Adds the active tab of the browser
extensions.addTab(browserWindow.webContents, browserWindow)
browserWindow.loadURL('https://samuelmaddock.com')
browserWindow.show()
})Multi-tab browser with full support for Chrome extension APIs.
For a complete example, see the
electron-browser-shellproject.
const { app, session, BrowserWindow } = require('electron')
const { ElectronChromeExtensions } = require('@p2plabs/peersky-chrome-extensions')
app.whenReady().then(() => {
const browserSession = session.fromPartition('persist:custom')
const extensions = new ElectronChromeExtensions({
license: 'GPL-3.0',
session: browserSession,
createTab(details) {
// Optionally implemented for chrome.tabs.create support
},
selectTab(tab, browserWindow) {
// Optionally implemented for chrome.tabs.update support
},
removeTab(tab, browserWindow) {
// Optionally implemented for chrome.tabs.remove support
},
createWindow(details) {
// Optionally implemented for chrome.windows.create support
},
removeWindow(browserWindow) {
// Optionally implemented for chrome.windows.remove support
},
requestPermissions(extension, permissions) {
// Optionally implemented for chrome.permissions.request support
},
})
const browserWindow = new BrowserWindow({
webPreferences: {
// Use same session given to Extensions class
session: browserSession,
// Required for extension preload scripts
sandbox: true,
// Recommended for loading remote content
contextIsolation: true,
},
})
// Adds the active tab of the browser
extensions.addTab(browserWindow.webContents, browserWindow)
browserWindow.loadURL('https://samuelmaddock.com')
browserWindow.show()
})This module uses a preload script. When packaging your application, it's required that the preload script is included. This can be handled in two ways:
- Include
node_modulesin your packaged app. This allows@p2plabs/peersky-chrome-extensions/preloadto be resolved. - In the case of using JavaScript bundlers, you may need to copy the preload script next to your app's entry point script. You can try using copy-webpack-plugin, vite-plugin-static-copy, or rollup-plugin-copy depending on your app's configuration.
Here's an example for webpack configurations:
module.exports = {
entry: './index.js',
plugins: [
new CopyWebpackPlugin({
patterns: [require.resolve('@p2plabs/peersky-chrome-extensions/preload')],
}),
],
}Create main process handler for Chrome extension APIs.
optionsObjectlicenseString - Distribution license compatible with your application. See LICENSE.md for more details.
Valid options includeGPL-3.0,Patron-License-2020-11-19sessionElectron.Session (optional) - Session which should support Chrome extension APIs.session.defaultSessionis used by default.createTab(details) => Promise<[Electron.WebContents, Electron.BrowserWindow]>(optional) - Called whenchrome.tabs.createis invoked by an extension. Allows the application to handle how tabs are created.detailschrome.tabs.CreateProperties
selectTab(webContents, browserWindow)(optional) - Called whenchrome.tabs.updateis invoked by an extension with the option to set the active tab.webContentsElectron.WebContents - The tab to be activated.browserWindowElectron.BrowserWindow - The window which owns the tab.
removeTab(webContents, browserWindow)(optional) - Called whenchrome.tabs.removeis invoked by an extension.webContentsElectron.WebContents - The tab to be removed.browserWindowElectron.BrowserWindow - The window which owns the tab.
createWindow(details) => Promise<Electron.BaseWindow | [Electron.WebContents, Electron.BaseWindow]>(optional) - Called whenchrome.windows.createis invoked by an extension. Resolves with the window, or with the first tab's webContents and the window.detailschrome.windows.CreateData
removeWindow(browserWindow) => Promise<Electron.BrowserWindow>(optional) - Called whenchrome.windows.removeis invoked by an extension.browserWindowElectron.BrowserWindow
assignTabDetails(details, webContents) => void(optional) - Called whenchrome.tabscreates an object for tab details to be sent to an extension background script. Provide this function to assign custom details such asdiscarded,frozen, orgroupId.detailschrome.tabs.TabwebContentsElectron.WebContents - The tab for which details are being created.
new ElectronChromeExtensions({
license: 'GPL-3.0',
createTab(details) {
const tab = myTabApi.createTab()
if (details.url) {
tab.webContents.loadURL(details.url)
}
return [tab.webContents, tab.browserWindow]
},
createWindow(details) {
const window = new BrowserWindow()
return window
},
})For a complete usage example, see the browser implementation in the
electron-browser-shell
project.
tabElectron.WebContents - A tab that the extension system should keep track of.windowElectron.BrowserWindow - The window which owns the tab.
Makes the tab accessible from the chrome.tabs API.
tabElectron.WebContents
Notify the extension system that a tab has been selected as the active tab.
tabElectron.WebContents - The tab from which the context-menu event originated.paramsElectron.ContextMenuParams - Parameters from thecontext-menuevent.
Returns Electron.MenuItem[] -
An array of all extension context menu items given the context.
Returns Object which maps special URL types to an extension URL. See chrome_urls_overrides for a list of
supported URL types.
Example:
{
newtab: 'chrome-extension://<id>/newtab.html'
}Returns:
popupPopupView - An instance of the popup.
Emitted when a popup is created by the chrome.browserAction API.
Returns:
urlOverridesObject - A map of url types to extension URLs.
Emitted after an extension is loaded with chrome_urls_overrides set.
The <browser-action-list> element provides a row of browser actions which may be pressed to activate the chrome.browserAction.onClicked event or display the extension popup.
To enable the element on a webpage, you must define a preload script which injects the API on specific pages.
partitionstring (optional) - TheElectron.Sessionpartition which extensions are loaded in. Defaults to the session in which<browser-action-list>lives.tabstring (optional) - The tab'sElectron.WebContentsID to use for displaying the relevant browser action state. Defaults to the active tab of the current browser window.alignmentstring (optional) - How the popup window should be aligned relative to the extension action. Defaults tobottom left. Use any assortment oftop,bottom,left, andright.
Inject the browserAction API to make the <browser-action-list> element accessible in your application.
import { injectBrowserAction } from '@p2plabs/peersky-chrome-extensions/browser-action'
// Inject <browser-action-list> element into our page
if (location.href === 'webui://browser-chrome.html') {
injectBrowserAction()
}The use of
importimplies that your preload script must be compiled using a JavaScript bundler like Webpack.
Add the <browser-action-list> element with attributes appropriate for your application.
<!-- Show actions for the same session and active tab of current window. -->
<browser-action-list></browser-action-list>
<!-- Show actions for custom session and active tab of current window. -->
<browser-action-list partition="persist:custom"></browser-action-list>
<!-- Show actions for custom session and a specific tab of current window. -->
<browser-action-list partition="persist:custom" tab="1"></browser-action-list>
<!-- If extensions are displayed in the bottom left of your browser UI, then
you can align the popup to the top right of the extension action. -->
<browser-action-list alignment="top right"></browser-action-list>For extension icons to appear in the list, the crx:// protocol needs to be handled in the Session
where it's intended to be displayed.
import { app, session } from 'electron'
import { ElectronChromeExtensions } from '@p2plabs/peersky-chrome-extensions'
app.whenReady().then(() => {
// Provide the session where your app will display <browser-action-list>
const appSession = session.defaultSession
ElectronChromeExtensions.handleCRXProtocol(appSession)
})The <browser-action-list> element is a Web Component. Its styles are encapsulated within a Shadow DOM. However, it's still possible to customize its appearance using the CSS shadow parts selector ::part(name).
Accessible parts include action and badge.
/* Layout action buttons vertically. */
browser-action-list {
flex-direction: column;
}
/* Modify size of action buttons. */
browser-action-list::part(action) {
width: 16px;
height: 16px;
}
/* Modify hover styles of action buttons. */
browser-action-list::part(action):hover {
background-color: red;
border-radius: 0;
}Every API below answers as Chrome 157 does: the same values, events, argument checks and error messages. Two test extensions in spec/fixtures/chrome-api-probe and spec/fixtures/chrome-api-probe-peer were run in Chrome for Testing 157, and what Chrome answered is kept in spec/fixtures/chrome-api-probe/chrome-results.json. spec/chrome-api-parity-spec.ts runs the same extensions in the shell on every test run and fails on any answer that differs. The few places where the shell differs on purpose are listed, each with its reason, in spec/fixtures/chrome-api-probe/known-differences.json.
An API shows up only for an extension that asks for its permission, as in Chrome, and every call is checked again in the main process. An extension sees a tab's url, title and favicon only with the tabs permission, host access to that page, or activeTab. Host access follows Chrome's match patterns, and activeTab is granted when the person clicks the extension's toolbar button, a context menu item of its, its keyboard shortcut or its omnibox keyword.
| API | Notes for the host |
|---|---|
chrome.action, chrome.browserAction |
Toolbar state for <browser-action-list>. isActionOnToolbar hook. |
chrome.alarms |
Kept per session. Chrome's 30 second minimum for extensions that are not unpacked (getExtensionInstallType hook). |
chrome.bookmarks |
A tree like Chrome's, kept per session. Follow changes with the bookmarks-changed event; push the host's own bookmarks with replaceBookmarks() or importBookmarks(). |
chrome.browsingData |
Removes the session's data. Return the host's own pages from the getProtectedOrigins hook so they are never cleared. |
chrome.commands |
Call api.commands.execute(extensionId, name) when the person presses a shortcut. |
chrome.contentSettings |
Rules kept per session. JavaScript, images and cookies apply on the webRequest bridge; ask getContentSetting(type, url) from the host's permission and window-open handlers. The getContentSetting hook gives the host's own site settings where no extension rule decides. |
chrome.contextMenus |
getContextMenuItems(webContents, params) builds the items. |
chrome.cookies |
Only cookies of hosts the extension has access to. |
chrome.debugger |
Only tabs and their own frames and workers, never browser pages or other extensions' pages, and only the DevTools commands Chrome allows an extension. |
chrome.declarativeContent |
Rules kept per session; ShowAction, SetIcon and RequestContentScript. |
chrome.declarativeNetRequest |
Static, dynamic and session rules on the webRequest bridge. A regexFilter that could backtrack for long runs under a time limit. |
chrome.desktopCapture |
chooseDesktopMedia hook for the host's picker; without it every request is cancelled. |
chrome.downloads |
Kept per session. setDownloadUiEnabled hook. |
chrome.extension, chrome.runtime |
onInstalled, onStartup, onExtensionLoaded and onEnabled fire as in Chrome. |
chrome.fontSettings |
Kept per session. Pass getFontWebPreferences() to new tabs. |
chrome.history, chrome.topSites |
Visits are recorded only while an extension with history or topSites is loaded. Call clearHistory() from the host's own "clear history". |
chrome.i18n, chrome.idle, chrome.power, chrome.storage |
local and session are Electron's own stores, shared with content scripts as in Chrome; sync is the shell's, encrypted with safeStorage; managed reads the getManagedStorage hook. |
chrome.identity |
launchWebAuthFlow; no Google account sign-in. |
chrome.management |
setExtensionEnabled, confirmUninstall and uninstallExtension hooks. |
chrome.notifications |
Shown with Electron's Notification. |
chrome.offscreen |
One document per extension, in a view outside any window. |
chrome.omnibox |
Drive it from the address bar with getOmniboxKeywords(), omniboxInputStarted(), omniboxInputChanged(), omniboxInputEntered() and omniboxInputCancelled(). |
chrome.pageCapture, chrome.tabCapture |
|
chrome.permissions |
Optional permissions need a user gesture and the person's yes (the requestPermissions hook, or a system dialog), and are kept across restarts. |
chrome.privacy |
Kept per session. Read the rest with getPrivacySettings(). |
chrome.proxy |
Applied with session.setProxy and kept per session; an extension's proxy counts again once it loads after a restart. getProxySettings() and the proxy-changed event tell the host which proxy counts and which extension set it, for example to apply it before extensions load; restoreExtensionProxy() hands back one the host kept itself under 1.x. |
chrome.readingList, chrome.search |
getSearchUrl hook for the host's search engine. |
chrome.scripting, chrome.userScripts |
userScripts is off until the host's isUserScriptsAllowed hook allows it for the extension, as Chrome's "Allow User Scripts" switch does; while off, chrome.userScripts is not there. Local files are reached only when the isFileSchemeAccessAllowed hook allows it, as Chrome's "Allow access to file URLs" does, whatever the extension's patterns say. |
chrome.sessions |
Tabs and windows closed in this run. |
chrome.sidePanel |
openSidePanel and closeSidePanel hooks. |
chrome.system.cpu, .memory, .display, .storage |
|
chrome.tabGroups |
updateTabGroup hook and tab-group-* events to show groups. |
chrome.tabs, chrome.windows |
createTab, selectTab, removeTab, createWindow, removeWindow, getTabIndex, moveTab, highlightTabs, pinTab and discardTab hooks. |
chrome.tts |
Speaks from a hidden page. |
chrome.webNavigation, chrome.webRequest |
The host passes every request to the notifyWebRequest* methods, since Electron gives each session.webRequest event to one handler only. |
Not supported: Chrome's sign-in (identity.getAuthToken), chrome.gcm, ChromeOS-only APIs, split view, and chrome.dom (Electron gives script no way to reach a closed shadow root).
All hooks on the ElectronChromeExtensions options are optional; each has a safe default, documented in src/browser/impl.ts. Extension state (alarms, permissions, rules, settings, bookmarks, storage) is kept in the session's own folder, so two sessions never share it and an in-memory session never writes it to disk. Version 1.x kept it in one folder under userData; 2.0 reads that once.
From the repository root:
npm ci
npm run build
npm testThe test suite runs Electron + Mocha (see spec/index.js). It expects a dev install that includes yargs (listed in devDependencies).
- Build and test in one run:
npm run test:ci - Against Chrome:
PROBE_SUITES=tabs,windows PROBE_RESULTS_OUT=/tmp/shell.json npm test -- --files spec/chrome-api-parity-spec.tsruns some of the probe suites, andnode script/probe-diff.js /tmp/shell.json tabs,windowsshows where the shell differs from Chrome. To record Chrome's answers again after changing a suite, runCHROME=<Chrome for Testing binary> node script/chrome-parity.mjs --only=tabs(branded Chrome no longer loads unpacked extensions from the command line). Chrome runs headless with a temporary profile. - Native messaging tests (
spec/chrome-nativeMessaging-spec.ts) build a small SEA host underscript/native-messaging-host/. That step can be slow or flaky on some machines: setSKIP_NATIVE_MESSAGING=1to run the rest of the suite without them, and run them on a machine wherenode script/native-messaging-host/build.js <extensionId>completes.
- Electron 43 or later (
peerDependencies.electronis>=43.0.0), which is what the tests run on. - Content scripts run on Electron's own bindings, which the shell cannot add to:
chrome.storage.syncis not available in them.
- Electron gives each
session.webRequestevent to one handler only, so the host owns them and passes each request to thenotifyWebRequest*methods; requests it does not pass on are never seen bychrome.webRequest,declarativeNetRequest,chrome.privacyorchrome.contentSettings. - Chrome extensions are not supported in non-persistent/incognito sessions.
chrome.webNavigation.onDOMContentLoadedis only emitted for the top frame until support for iframes is added.- Service worker preload scripts require Electron's sandbox to be enabled. This is the default behavior, but might be overridden by the
--no-sandboxflag orsandbox: falsein thewebPreferencesof aBrowserWindow. Check for the--no-sandboxflag usingps -eaf | grep <appname>.
GPL-3
This package is published from the Peersky fork (github.com/p2plabsxyz/peersky-browser-shell); see LICENSE.md in the tarball.
For proprietary use, please contact me or sponsor me on GitHub under the appropriate tier to acquire a proprietary-use license. These contributions help make development and maintenance of this project more sustainable and show appreciation for the work thus far.


