Skip to content

Commit

Permalink
Merge pull request #5 from skdhg/custom-session
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca authored May 24, 2023
2 parents 4f82699 + f1d257e commit 36d9799
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LoadExtensionOptions } from "electron";
import type { LoadExtensionOptions, Session } from "electron";
import { session } from "electron";
import * as path from "path";
import * as rimraf from "rimraf";
Expand Down Expand Up @@ -82,6 +82,10 @@ export interface ExtensionOptions {
* Options passed to session.loadExtension
*/
loadExtensionOptions?: LoadExtensionOptions;
/**
* The target session on which the extension shall be installed
*/
session?: string | Session;
}

const isManifestVersion3 = async (manifestDirectory: string) => {
Expand All @@ -102,6 +106,7 @@ export const installExtension = async (
extensionReference: ExtensionReference | string | Array<ExtensionReference | string>,
options: ExtensionOptions = {},
): Promise<string | string[]> => {
const targetSession = typeof options.session === 'string' ? session.fromPartition(options.session) : options.session || session.defaultSession;
const { forceDownload, loadExtensionOptions } = options;

if (process.type !== "browser") {
Expand All @@ -124,7 +129,7 @@ export const installExtension = async (
const IDMap = getIdMap();
const extensionName = IDMap[chromeStoreID];
// todo - should we check id here?
const installedExtension = session.defaultSession.getAllExtensions().find((e) => e.name === extensionName);
const installedExtension = targetSession.getAllExtensions().find((e) => e.name === extensionName);

if (!forceDownload && installedExtension) {
return IDMap[chromeStoreID];
Expand All @@ -133,7 +138,7 @@ export const installExtension = async (
const extensionFolder = await downloadChromeExtension(chromeStoreID, Boolean(forceDownload));
// Use forceDownload, but already installed
if (installedExtension) {
session.defaultSession.removeExtension(installedExtension.id);
targetSession.removeExtension(installedExtension.id);
}

if (await isManifestVersion3(extensionFolder)) {
Expand All @@ -144,7 +149,7 @@ export const installExtension = async (
https://github.com/MarshallOfSound/electron-devtools-installer/issues/238
https://github.com/electron/electron/blob/e3b7c3024f6f70155efb1022b691954280f983cb/docs/api/extensions.md#L1`);
}
const ext = await session.defaultSession.loadExtension(extensionFolder, loadExtensionOptions);
const ext = await targetSession.loadExtension(extensionFolder, loadExtensionOptions);
return ext.name;
};
export default installExtension;
Expand Down

0 comments on commit 36d9799

Please sign in to comment.