Skip to content

Commit

Permalink
Support custom session
Browse files Browse the repository at this point in the history
  • Loading branch information
fytriht committed Jul 31, 2021
1 parent 31bf863 commit 137ffdb
Showing 1 changed file with 13 additions and 11 deletions.
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

0 comments on commit 137ffdb

Please sign in to comment.