Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom session #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BrowserWindow, session } from 'electron';
import { BrowserWindow, session as _session, Session } from 'electron';
import * as fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
Expand Down Expand Up @@ -36,6 +36,10 @@ interface ExtensionOptions {
* Options passed to session.loadExtension
*/
loadExtensionOptions?: Record<any, any>;
/**
* Session to install extensions, defaults to session.defaultSession. (for Electron >=9)
*/
session?: Session;
}

/**
Expand All @@ -51,7 +55,7 @@ const install = (
if (typeof options === 'boolean') {
options = { forceDownload: options };
}
const { forceDownload, loadExtensionOptions } = options;
const { forceDownload, loadExtensionOptions, session = _session.defaultSession } = options;

if (process.type !== 'browser') {
return Promise.reject(
Expand Down Expand Up @@ -87,12 +91,10 @@ const install = (
let extensionInstalled: boolean;

// For Electron >=9.
if ((session.defaultSession as any).getExtension) {
if ((session as any).getExtension) {
extensionInstalled =
!!extensionName &&
(session.defaultSession as any)
.getAllExtensions()
.find((e: { name: string }) => e.name === extensionName);
(session as any).getAllExtensions().find((e: { name: string }) => e.name === extensionName);
} else {
extensionInstalled =
!!extensionName &&
Expand All @@ -107,19 +109,19 @@ const install = (
// Use forceDownload, but already installed
if (extensionInstalled) {
// For Electron >=9.
if ((session.defaultSession as any).removeExtension) {
const extensionId = (session.defaultSession as any)
if ((session as any).removeExtension) {
const extensionId = (session as any)
.getAllExtensions()
.find((e: { name: string }) => e.name).id;
(session.defaultSession as any).removeExtension(extensionId);
(session as any).removeExtension(extensionId);
} else {
BrowserWindow.removeDevToolsExtension(extensionName);
}
}

// For Electron >=9.
if ((session.defaultSession as any).loadExtension) {
return (session.defaultSession as any)
if ((session as any).loadExtension) {
return (session as any)
.loadExtension(extensionFolder, loadExtensionOptions)
.then((ext: { name: string }) => {
return Promise.resolve(ext.name);
Expand Down