diff --git a/index.d.ts b/index.d.ts index 26836fb..a9bd8f7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,5 @@ import * as Electron from 'electron'; +import { WebContentsView } from './index'; export { ClientRequest, @@ -57,6 +58,9 @@ export var Tray: typeof Electron.Tray; export var webContents: typeof Electron.webContents; export var webFrameMain: typeof Electron.webFrameMain; +// Taken from `RemoteMainInterface` but WebContentsView is only available in Electron >= 29.0.0 +export { WebContentsView }; + // Taken from `Remote` export function getCurrentWebContents(): Electron.WebContents; export function getCurrentWindow(): Electron.BrowserWindow; diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..6c0ff30 --- /dev/null +++ b/index.ts @@ -0,0 +1,17 @@ +import * as Electron from 'electron'; +import { isVersionGreaterOrEqual } from './src/common/utils'; + +const electronVersion = process.versions.electron; + +// Check if WebContentsView exists and if the version is >= 29.0.0 +let WebContentsView_: any; // Use 'any' for dynamic checking + +if (isVersionGreaterOrEqual('29.0.0', electronVersion) && 'WebContentsView' in Electron) { + WebContentsView_ = Electron.WebContentsView; // Assign if it exists +} else { + console.warn("WebContentsView is not available in this version of Electron."); + WebContentsView_ = undefined; // Explicitly set to undefined if not available +} + +// Export the WebContentsView for use in other parts of your application +export const WebContentsView = WebContentsView_; diff --git a/src/common/utils.ts b/src/common/utils.ts new file mode 100644 index 0000000..e57621a --- /dev/null +++ b/src/common/utils.ts @@ -0,0 +1,11 @@ +export function isVersionGreaterOrEqual(requiredVersion: string, currentVersion: string): boolean { + const required = requiredVersion.split('.').map(Number); + const current = currentVersion.split('.').map(Number); + + for (let i = 0; i < required.length; i++) { + if (current[i] > required[i]) return true; + if (current[i] < required[i]) return false; + } + + return true; +} \ No newline at end of file