From 79b33937688a6745b7813364bfc732b8084d26d1 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Fri, 29 Jan 2021 21:41:59 +0100 Subject: [PATCH] chore: remove bunch of usages of any (#27512) --- lib/asar/fs-wrapper.ts | 6 ++- lib/browser/api/menu.ts | 4 +- lib/browser/api/touch-bar.ts | 8 ++-- lib/browser/api/web-contents.ts | 50 ++++++++------------ lib/browser/guest-view-manager.ts | 2 +- lib/browser/guest-window-manager.ts | 32 ++++++------- lib/browser/guest-window-proxy.ts | 14 +++--- lib/browser/remote/server.ts | 4 +- lib/renderer/api/context-bridge.ts | 2 +- lib/renderer/init.ts | 2 +- lib/renderer/security-warnings.ts | 2 +- lib/renderer/web-view/web-view-attributes.ts | 2 +- lib/renderer/web-view/web-view-element.ts | 2 +- spec-main/api-context-bridge-spec.ts | 18 +++---- typings/internal-ambient.d.ts | 2 + typings/internal-electron.d.ts | 32 +++++++++++-- 16 files changed, 101 insertions(+), 81 deletions(-) diff --git a/lib/asar/fs-wrapper.ts b/lib/asar/fs-wrapper.ts index f6827fda3a..569e19ff7f 100644 --- a/lib/asar/fs-wrapper.ts +++ b/lib/asar/fs-wrapper.ts @@ -639,8 +639,10 @@ export const wrapFsWithAsar = (fs: Record) => { fs.promises.readdir = util.promisify(fs.readdir); + type ReaddirSyncOptions = { encoding: BufferEncoding | null; withFileTypes?: false }; + const { readdirSync } = fs; - fs.readdirSync = function (pathArgument: string, options: { encoding: BufferEncoding | null; withFileTypes?: false } | BufferEncoding | null) { + fs.readdirSync = function (pathArgument: string, options: ReaddirSyncOptions | BufferEncoding | null) { const pathInfo = splitPath(pathArgument); if (!pathInfo.isAsar) return readdirSync.apply(this, arguments); const { asarPath, filePath } = pathInfo; @@ -655,7 +657,7 @@ export const wrapFsWithAsar = (fs: Record) => { throw createError(AsarError.NOT_FOUND, { asarPath, filePath }); } - if (options && (options as any).withFileTypes) { + if (options && (options as ReaddirSyncOptions).withFileTypes) { const dirents = []; for (const file of files) { const childPath = path.join(filePath, file); diff --git a/lib/browser/api/menu.ts b/lib/browser/api/menu.ts index 9e633d0f9d..26969d4bb3 100644 --- a/lib/browser/api/menu.ts +++ b/lib/browser/api/menu.ts @@ -231,12 +231,12 @@ function sortTemplate (template: (MenuItemConstructorOptions | MenuItem)[]) { function generateGroupId (items: (MenuItemConstructorOptions | MenuItem)[], pos: number) { if (pos > 0) { for (let idx = pos - 1; idx >= 0; idx--) { - if (items[idx].type === 'radio') return (items[idx] as any).groupId; + if (items[idx].type === 'radio') return (items[idx] as MenuItem).groupId; if (items[idx].type === 'separator') break; } } else if (pos < items.length) { for (let idx = pos; idx <= items.length - 1; idx++) { - if (items[idx].type === 'radio') return (items[idx] as any).groupId; + if (items[idx].type === 'radio') return (items[idx] as MenuItem).groupId; if (items[idx].type === 'separator') break; } } diff --git a/lib/browser/api/touch-bar.ts b/lib/browser/api/touch-bar.ts index 0e6b92eafa..def9b8d4cc 100644 --- a/lib/browser/api/touch-bar.ts +++ b/lib/browser/api/touch-bar.ts @@ -13,14 +13,14 @@ const extendConstructHook = (target: any, hook: Function) => { }; const ImmutableProperty = >(def: (config: T extends TouchBarItem ? C : never, setInternalProp: (k: K, v: T[K]) => void) => any) => (target: T, propertyKey: keyof T) => { - extendConstructHook(target as any, function (this: T) { + extendConstructHook(target, function (this: T) { (this as any)[hiddenProperties][propertyKey] = def((this as any)._config, (k, v) => { (this as any)[hiddenProperties][k] = v; }); }); Object.defineProperty(target, propertyKey, { get: function () { - return (this as any)[hiddenProperties][propertyKey]; + return this[hiddenProperties][propertyKey]; }, set: function () { throw new Error(`Cannot override property ${name}`); @@ -31,7 +31,7 @@ const ImmutableProperty = >(def: (config: T extends }; const LiveProperty = >(def: (config: T extends TouchBarItem ? C : never) => any, onMutate?: (self: T, newValue: any) => void) => (target: T, propertyKey: keyof T) => { - extendConstructHook(target as any, function (this: T) { + extendConstructHook(target, function (this: T) { (this as any)[hiddenProperties][propertyKey] = def((this as any)._config); if (onMutate) onMutate((this as any), (this as any)[hiddenProperties][propertyKey]); }); @@ -59,7 +59,7 @@ abstract class TouchBarItem extends EventEmitter { constructor (config: ConfigType) { super(); - this._config = this._config || config || {} as any; + this._config = this._config || config || {} as ConfigType; (this as any)[hiddenProperties] = {}; const hook = (this as any)._hook; if (hook) hook.call(this); diff --git a/lib/browser/api/web-contents.ts b/lib/browser/api/web-contents.ts index e22b60297d..16b8490594 100644 --- a/lib/browser/api/web-contents.ts +++ b/lib/browser/api/web-contents.ts @@ -22,18 +22,8 @@ const getNextId = function () { type PostData = LoadURLOptions['postData'] -/* eslint-disable camelcase */ -type MediaSize = { - name: string, - custom_display_name: string, - height_microns: number, - width_microns: number, - is_default?: 'true', -} -/* eslint-enable camelcase */ - // Stock page sizes -const PDFPageSizes: Record = { +const PDFPageSizes: Record = { A5: { custom_display_name: 'A5', height_microns: 210000, @@ -90,7 +80,7 @@ const isValidCustomPageSize = (width: number, height: number) => { const defaultPrintingSetting = { // Customizable. pageRange: [] as {from: number, to: number}[], - mediaSize: {} as MediaSize, + mediaSize: {} as ElectronInternal.MediaSize, landscape: false, headerFooterEnabled: false, marginsType: 0, @@ -353,7 +343,7 @@ WebContents.prototype.printToPDF = async function (options) { } }; -WebContents.prototype.print = function (options = {}, callback) { +WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions = {}, callback) { // TODO(codebytere): deduplicate argument sanitization by moving rest of // print param logic into new file shared between printToPDF and print if (typeof options === 'object') { @@ -372,14 +362,14 @@ WebContents.prototype.print = function (options = {}, callback) { throw new Error('height and width properties must be minimum 352 microns.'); } - (options as any).mediaSize = { + options.mediaSize = { name: 'CUSTOM', custom_display_name: 'Custom', height_microns: height, width_microns: width }; } else if (PDFPageSizes[pageSize]) { - (options as any).mediaSize = PDFPageSizes[pageSize]; + options.mediaSize = PDFPageSizes[pageSize]; } else { throw new Error(`Unsupported pageSize: ${pageSize}`); } @@ -428,7 +418,7 @@ WebContents.prototype.setWindowOpenHandler = function (handler: (details: Electr this._windowOpenHandler = handler; }; -WebContents.prototype._callWindowOpenHandler = function (event: any, url: string, frameName: string, rawFeatures: string): BrowserWindowConstructorOptions | null { +WebContents.prototype._callWindowOpenHandler = function (event: Electron.Event, url: string, frameName: string, rawFeatures: string): BrowserWindowConstructorOptions | null { if (!this._windowOpenHandler) { return null; } @@ -458,21 +448,21 @@ WebContents.prototype._callWindowOpenHandler = function (event: any, url: string } }; -const addReplyToEvent = (event: any) => { +const addReplyToEvent = (event: Electron.IpcMainEvent) => { const { processId, frameId } = event; - event.reply = (...args: any[]) => { - event.sender.sendToFrame([processId, frameId], ...args); + event.reply = (channel: string, ...args: any[]) => { + event.sender.sendToFrame([processId, frameId], channel, ...args); }; }; -const addSenderFrameToEvent = (event: any) => { +const addSenderFrameToEvent = (event: Electron.IpcMainEvent | Electron.IpcMainInvokeEvent) => { const { processId, frameId } = event; Object.defineProperty(event, 'senderFrame', { get: () => webFrameMain.fromId(processId, frameId) }); }; -const addReturnValueToEvent = (event: any) => { +const addReturnValueToEvent = (event: Electron.IpcMainEvent) => { Object.defineProperty(event, 'returnValue', { set: (value) => event.sendReply(value), get: () => {} @@ -521,7 +511,7 @@ WebContents.prototype._init = function () { this.setMaxListeners(0); // Dispatch IPC messages to the ipc module. - this.on('-ipc-message' as any, function (this: Electron.WebContents, event: any, internal: boolean, channel: string, args: any[]) { + this.on('-ipc-message' as any, function (this: Electron.WebContents, event: Electron.IpcMainEvent, internal: boolean, channel: string, args: any[]) { addSenderFrameToEvent(event); if (internal) { ipcMainInternal.emit(channel, event, ...args); @@ -532,7 +522,7 @@ WebContents.prototype._init = function () { } }); - this.on('-ipc-invoke' as any, function (event: any, internal: boolean, channel: string, args: any[]) { + this.on('-ipc-invoke' as any, function (event: Electron.IpcMainInvokeEvent, internal: boolean, channel: string, args: any[]) { addSenderFrameToEvent(event); event._reply = (result: any) => event.sendReply({ result }); event._throw = (error: Error) => { @@ -547,7 +537,7 @@ WebContents.prototype._init = function () { } }); - this.on('-ipc-message-sync' as any, function (this: Electron.WebContents, event: any, internal: boolean, channel: string, args: any[]) { + this.on('-ipc-message-sync' as any, function (this: Electron.WebContents, event: Electron.IpcMainEvent, internal: boolean, channel: string, args: any[]) { addSenderFrameToEvent(event); addReturnValueToEvent(event); if (internal) { @@ -559,13 +549,13 @@ WebContents.prototype._init = function () { } }); - this.on('-ipc-ports' as any, function (event: any, internal: boolean, channel: string, message: any, ports: any[]) { + this.on('-ipc-ports' as any, function (event: Electron.IpcMainEvent, internal: boolean, channel: string, message: any, ports: any[]) { event.ports = ports.map(p => new MessagePortMain(p)); ipcMain.emit(channel, event, message); }); // Handle context menu action request from pepper plugin. - this.on('pepper-context-menu' as any, function (event: any, params: {x: number, y: number, menu: Array<(MenuItemConstructorOptions) | (MenuItem)>}, callback: () => void) { + this.on('pepper-context-menu' as any, function (event: ElectronInternal.Event, params: {x: number, y: number, menu: Array<(MenuItemConstructorOptions) | (MenuItem)>}, callback: () => void) { // Access Menu via electron.Menu to prevent circular require. const menu = require('electron').Menu.buildFromTemplate(params.menu); menu.popup({ @@ -596,8 +586,8 @@ WebContents.prototype._init = function () { if (this.getType() !== 'remote') { // Make new windows requested by links behave like "window.open". - this.on('-new-window' as any, (event: any, url: string, frameName: string, disposition: string, - rawFeatures: string, referrer: any, postData: PostData) => { + this.on('-new-window' as any, (event: ElectronInternal.Event, url: string, frameName: string, disposition: string, + rawFeatures: string, referrer: Electron.Referrer, postData: PostData) => { openGuestWindow({ event, embedder: event.sender, @@ -614,7 +604,7 @@ WebContents.prototype._init = function () { }); let windowOpenOverriddenOptions: BrowserWindowConstructorOptions | null = null; - this.on('-will-add-new-contents' as any, (event: any, url: string, frameName: string, rawFeatures: string) => { + this.on('-will-add-new-contents' as any, (event: ElectronInternal.Event, url: string, frameName: string, rawFeatures: string) => { windowOpenOverriddenOptions = this._callWindowOpenHandler(event, url, frameName, rawFeatures); if (!event.defaultPrevented) { const secureOverrideWebPreferences = windowOpenOverriddenOptions ? { @@ -632,7 +622,7 @@ WebContents.prototype._init = function () { // Create a new browser window for the native implementation of // "window.open", used in sandbox and nativeWindowOpen mode. - this.on('-add-new-contents' as any, (event: any, webContents: Electron.WebContents, disposition: string, + this.on('-add-new-contents' as any, (event: ElectronInternal.Event, webContents: Electron.WebContents, disposition: string, _userGesture: boolean, _left: number, _top: number, _width: number, _height: number, url: string, frameName: string, referrer: Electron.Referrer, rawFeatures: string, postData: PostData) => { const overriddenOptions = windowOpenOverriddenOptions || undefined; diff --git a/lib/browser/guest-view-manager.ts b/lib/browser/guest-view-manager.ts index a88305cd2d..6aa9ed8213 100644 --- a/lib/browser/guest-view-manager.ts +++ b/lib/browser/guest-view-manager.ts @@ -197,7 +197,7 @@ const attachGuest = function (event: Electron.IpcMainInvokeEvent, // Inherit certain option values from embedder const lastWebPreferences = embedder.getLastWebPreferences(); for (const [name, value] of inheritedWebPreferences) { - if ((lastWebPreferences as any)[name] === value) { + if (lastWebPreferences[name as keyof Electron.WebPreferences] === value) { (webPreferences as any)[name] = value; } } diff --git a/lib/browser/guest-window-manager.ts b/lib/browser/guest-window-manager.ts index be4707884c..036e4a7aa8 100644 --- a/lib/browser/guest-window-manager.ts +++ b/lib/browser/guest-window-manager.ts @@ -85,7 +85,7 @@ export function openGuestWindow ({ event, embedder, guest, referrer, disposition httpReferrer: referrer, ...(postData && { postData, - extraHeaders: formatPostDataHeaders(postData) + extraHeaders: formatPostDataHeaders(postData as Electron.UploadRawData[]) }) }); } @@ -134,7 +134,7 @@ const handleWindowLifecycleEvents = function ({ embedder, guest, frameName }: { * `did-create-window` in 11.0.0. Will be removed in 12.0.0. */ function emitDeprecatedNewWindowEvent ({ event, embedder, guest, windowOpenArgs, browserWindowOptions, additionalFeatures, disposition, referrer, postData }: { - event: { sender: WebContents, defaultPrevented: boolean }, + event: { sender: WebContents, defaultPrevented: boolean, newGuest?: BrowserWindow }, embedder: WebContents, guest?: WebContents, windowOpenArgs: WindowOpenArgs, @@ -145,10 +145,10 @@ function emitDeprecatedNewWindowEvent ({ event, embedder, guest, windowOpenArgs, postData?: PostData, }): boolean { const { url, frameName } = windowOpenArgs; - const isWebViewWithPopupsDisabled = embedder.getType() === 'webview' && (embedder as any).getLastWebPreferences().disablePopups; + const isWebViewWithPopupsDisabled = embedder.getType() === 'webview' && embedder.getLastWebPreferences().disablePopups; const postBody = postData ? { data: postData, - headers: formatPostDataHeaders(postData) + headers: formatPostDataHeaders(postData as Electron.UploadRawData[]) } : null; embedder.emit( @@ -166,14 +166,14 @@ function emitDeprecatedNewWindowEvent ({ event, embedder, guest, windowOpenArgs, postBody ); - const { newGuest } = event as any; + const { newGuest } = event; if (isWebViewWithPopupsDisabled) return true; if (event.defaultPrevented) { if (newGuest) { if (guest === newGuest.webContents) { // The webContents is not changed, so set defaultPrevented to false to // stop the callers of this event from destroying the webContents. - (event as any).defaultPrevented = false; + event.defaultPrevented = false; } handleWindowLifecycleEvents({ @@ -224,7 +224,7 @@ function makeBrowserWindowOptions ({ embedder, features, frameName, isNativeWind ...parsedOptions, ...overrideOptions, webPreferences: makeWebPreferences({ embedder, insecureParsedWebPreferences: parsedWebPreferences, secureOverrideWebPreferences: overrideOptions && overrideOptions.webPreferences, useDeprecatedBehaviorForOptionInheritance: true }) - } + } as Electron.BrowserViewConstructorOptions }; } @@ -239,13 +239,13 @@ export function makeWebPreferences ({ embedder, secureOverrideWebPreferences = { useDeprecatedBehaviorForOptionInheritance?: boolean }) { const deprecatedInheritedOptions = getDeprecatedInheritedOptions(embedder); - const parentWebPreferences = (embedder as any).getLastWebPreferences(); - const securityWebPreferencesFromParent = Object.keys(securityWebPreferences).reduce((map, key) => { - if (securityWebPreferences[key] === parentWebPreferences[key]) { - map[key] = parentWebPreferences[key]; + const parentWebPreferences = embedder.getLastWebPreferences(); + const securityWebPreferencesFromParent = (Object.keys(securityWebPreferences).reduce((map, key) => { + if (securityWebPreferences[key] === parentWebPreferences[key as keyof Electron.WebPreferences]) { + (map as any)[key] = parentWebPreferences[key as keyof Electron.WebPreferences]; } return map; - }, {} as any); + }, {} as Electron.WebPreferences)); const openerId = parentWebPreferences.nativeWindowOpen ? null : embedder.id; return { @@ -270,18 +270,18 @@ export function makeWebPreferences ({ embedder, secureOverrideWebPreferences = { * only critical security preferences will be inherited by default. */ function getDeprecatedInheritedOptions (embedder: WebContents) { - if (!(embedder as any).browserWindowOptions) { + if (!embedder.browserWindowOptions) { // If it's a webview, return just the webPreferences. return { - webPreferences: (embedder as any).getLastWebPreferences() + webPreferences: embedder.getLastWebPreferences() }; } - const { type, show, ...inheritableOptions } = (embedder as any).browserWindowOptions; + const { type, show, ...inheritableOptions } = embedder.browserWindowOptions; return inheritableOptions; } -function formatPostDataHeaders (postData: any) { +function formatPostDataHeaders (postData: Electron.UploadRawData[]) { if (!postData) return; let extraHeaders = 'content-type: application/x-www-form-urlencoded'; diff --git a/lib/browser/guest-window-proxy.ts b/lib/browser/guest-window-proxy.ts index c8f91bfc43..4645c830d6 100644 --- a/lib/browser/guest-window-proxy.ts +++ b/lib/browser/guest-window-proxy.ts @@ -28,7 +28,7 @@ const getGuestWindow = function (guestContents: WebContents) { }; const isChildWindow = function (sender: WebContents, target: WebContents) { - return (target as any).getLastWebPreferences().openerId === sender.id; + return target.getLastWebPreferences().openerId === sender.id; }; const isRelatedWindow = function (sender: WebContents, target: WebContents) { @@ -43,7 +43,7 @@ const isScriptableWindow = function (sender: WebContents, target: WebContents) { }; const isNodeIntegrationEnabled = function (sender: WebContents) { - return (sender as any).getLastWebPreferences().nodeIntegration === true; + return sender.getLastWebPreferences().nodeIntegration === true; }; // Checks whether |sender| can access the |target|: @@ -65,15 +65,15 @@ ipcMainInternal.on( features: string ) => { // This should only be allowed for senders that have nativeWindowOpen: false - const lastWebPreferences = (event.sender as any).getLastWebPreferences(); + const lastWebPreferences = event.sender.getLastWebPreferences(); if (lastWebPreferences.nativeWindowOpen || lastWebPreferences.sandbox) { - (event as any).returnValue = null; + event.returnValue = null; throw new Error( 'GUEST_WINDOW_MANAGER_WINDOW_OPEN denied: expected native window.open' ); } - const browserWindowOptions = (event.sender as any)._callWindowOpenHandler(event, url, frameName, features); + const browserWindowOptions = event.sender._callWindowOpenHandler(event, url, frameName, features); if (event.defaultPrevented) { return; } @@ -82,7 +82,7 @@ ipcMainInternal.on( embedder: event.sender, referrer: { url: '', policy: 'default' }, disposition: 'new-window', - overrideBrowserWindowOptions: browserWindowOptions, + overrideBrowserWindowOptions: browserWindowOptions!, windowOpenArgs: { url: url || 'about:blank', frameName: frameName || '', @@ -90,7 +90,7 @@ ipcMainInternal.on( } }); - (event as any).returnValue = guest ? guest.webContents.id : null; + event.returnValue = guest ? guest.webContents.id : null; } ); diff --git a/lib/browser/remote/server.ts b/lib/browser/remote/server.ts index 1e8116874c..5dd28bed3e 100644 --- a/lib/browser/remote/server.ts +++ b/lib/browser/remote/server.ts @@ -206,7 +206,7 @@ const removeRemoteListenersAndLogWarning = (sender: any, callIntoRenderer: (...a if (remoteEvents.length > 0) { message += `\nRemote event names: ${remoteEvents.join(', ')}`; remoteEvents.forEach((eventName) => { - sender.removeListener(eventName as any, callIntoRenderer); + sender.removeListener(eventName, callIntoRenderer); }); } } @@ -365,7 +365,7 @@ handleRemoteCommand(IPC_MESSAGES.BROWSER_REQUIRE, function (event, contextId, mo if (customEvent.defaultPrevented) { throw new Error(`Blocked remote.require('${moduleName}')`); } else { - customEvent.returnValue = (process as any).mainModule.require(moduleName); + customEvent.returnValue = process.mainModule.require(moduleName); } } diff --git a/lib/renderer/api/context-bridge.ts b/lib/renderer/api/context-bridge.ts index 2d99e1a97b..2dcc5bf92e 100644 --- a/lib/renderer/api/context-bridge.ts +++ b/lib/renderer/api/context-bridge.ts @@ -12,7 +12,7 @@ const contextBridge: Electron.ContextBridge = { checkContextIsolationEnabled(); return binding.exposeAPIInMainWorld(key, api); } -} as any; +}; export default contextBridge; diff --git a/lib/renderer/init.ts b/lib/renderer/init.ts index d098b41ece..9b7be2868f 100644 --- a/lib/renderer/init.ts +++ b/lib/renderer/init.ts @@ -144,7 +144,7 @@ if (nodeIntegration) { // We do not want to add `uncaughtException` to our definitions // because we don't want anyone else (anywhere) to throw that kind // of error. - global.process.emit('uncaughtException' as any, error as any); + global.process.emit('uncaughtException', error as any); return true; } else { return false; diff --git a/lib/renderer/security-warnings.ts b/lib/renderer/security-warnings.ts index 92c98130f1..7c2d265145 100644 --- a/lib/renderer/security-warnings.ts +++ b/lib/renderer/security-warnings.ts @@ -79,7 +79,7 @@ const isLocalhost = function () { */ const isUnsafeEvalEnabled: () => Promise = function () { // Call _executeJavaScript to bypass the world-safe deprecation warning - return (webFrame as any)._executeJavaScript(`(${(() => { + return webFrame._executeJavaScript(`(${(() => { try { eval(window.trustedTypes.emptyScript); // eslint-disable-line no-eval } catch { diff --git a/lib/renderer/web-view/web-view-attributes.ts b/lib/renderer/web-view/web-view-attributes.ts index 196119d9a1..764088bb11 100644 --- a/lib/renderer/web-view/web-view-attributes.ts +++ b/lib/renderer/web-view/web-view-attributes.ts @@ -60,7 +60,7 @@ export class WebViewAttribute implements MutationHandler { } // Called when the attribute's value changes. - public handleMutation: MutationHandler['handleMutation'] = () => undefined as any + public handleMutation: MutationHandler['handleMutation'] = () => undefined } // An attribute that is treated as a Boolean. diff --git a/lib/renderer/web-view/web-view-element.ts b/lib/renderer/web-view/web-view-element.ts index e24d9d0973..6034ef587b 100644 --- a/lib/renderer/web-view/web-view-element.ts +++ b/lib/renderer/web-view/web-view-element.ts @@ -94,7 +94,7 @@ const registerWebViewElement = (v8Util: NodeJS.V8UtilBinding, webViewImpl: typeo // The customElements.define has to be called in a special scope. webViewImpl.webFrame.allowGuestViewElementDefinition(window, () => { window.customElements.define('webview', WebViewElement); - (window as any).WebView = WebViewElement; + window.WebView = WebViewElement; // Delete the callbacks so developers cannot call them and produce unexpected // behavior. diff --git a/spec-main/api-context-bridge-spec.ts b/spec-main/api-context-bridge-spec.ts index d5ed0622cc..c3224ea99a 100644 --- a/spec-main/api-context-bridge-spec.ts +++ b/spec-main/api-context-bridge-spec.ts @@ -981,7 +981,7 @@ describe('contextBridge', () => { describe('overrideGlobalValueFromIsolatedWorld', () => { it('should override top level properties', async () => { await makeBindingWindow(() => { - contextBridge.internalContextBridge.overrideGlobalValueFromIsolatedWorld(['open'], () => ({ you: 'are a wizard' })); + contextBridge.internalContextBridge!.overrideGlobalValueFromIsolatedWorld(['open'], () => ({ you: 'are a wizard' })); }); const result = await callWithBindings(async (root: any) => { return root.open(); @@ -991,7 +991,7 @@ describe('contextBridge', () => { it('should override deep properties', async () => { await makeBindingWindow(() => { - contextBridge.internalContextBridge.overrideGlobalValueFromIsolatedWorld(['document', 'foo'], () => 'I am foo'); + contextBridge.internalContextBridge!.overrideGlobalValueFromIsolatedWorld(['document', 'foo'], () => 'I am foo'); }); const result = await callWithBindings(async (root: any) => { return root.document.foo(); @@ -1008,7 +1008,7 @@ describe('contextBridge', () => { callCount++; return true; }; - contextBridge.internalContextBridge.overrideGlobalPropertyFromIsolatedWorld(['isFun'], getter); + contextBridge.internalContextBridge!.overrideGlobalPropertyFromIsolatedWorld(['isFun'], getter); contextBridge.exposeInMainWorld('foo', { callCount: () => callCount }); @@ -1022,7 +1022,7 @@ describe('contextBridge', () => { it('should not make a setter if none is provided', async () => { await makeBindingWindow(() => { - contextBridge.internalContextBridge.overrideGlobalPropertyFromIsolatedWorld(['isFun'], () => true); + contextBridge.internalContextBridge!.overrideGlobalPropertyFromIsolatedWorld(['isFun'], () => true); }); const result = await callWithBindings(async (root: any) => { root.isFun = 123; @@ -1038,7 +1038,7 @@ describe('contextBridge', () => { callArgs.push(args); return true; }; - contextBridge.internalContextBridge.overrideGlobalPropertyFromIsolatedWorld(['isFun'], () => true, setter); + contextBridge.internalContextBridge!.overrideGlobalPropertyFromIsolatedWorld(['isFun'], () => true, setter); contextBridge.exposeInMainWorld('foo', { callArgs: () => callArgs }); @@ -1056,7 +1056,7 @@ describe('contextBridge', () => { describe('overrideGlobalValueWithDynamicPropsFromIsolatedWorld', () => { it('should not affect normal values', async () => { await makeBindingWindow(() => { - contextBridge.internalContextBridge.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['thing'], { + contextBridge.internalContextBridge!.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['thing'], { a: 123, b: () => 2, c: () => ({ d: 3 }) @@ -1070,7 +1070,7 @@ describe('contextBridge', () => { it('should work with getters', async () => { await makeBindingWindow(() => { - contextBridge.internalContextBridge.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['thing'], { + contextBridge.internalContextBridge!.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['thing'], { get foo () { return 'hi there'; } @@ -1085,7 +1085,7 @@ describe('contextBridge', () => { it('should work with setters', async () => { await makeBindingWindow(() => { let a: any = null; - contextBridge.internalContextBridge.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['thing'], { + contextBridge.internalContextBridge!.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['thing'], { get foo () { return a; }, @@ -1103,7 +1103,7 @@ describe('contextBridge', () => { it('should work with deep properties', async () => { await makeBindingWindow(() => { - contextBridge.internalContextBridge.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['thing'], { + contextBridge.internalContextBridge!.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['thing'], { a: () => ({ get foo () { return 'still here'; diff --git a/typings/internal-ambient.d.ts b/typings/internal-ambient.d.ts index b280e882af..85f9d1253c 100644 --- a/typings/internal-ambient.d.ts +++ b/typings/internal-ambient.d.ts @@ -232,6 +232,7 @@ declare namespace NodeJS { _firstFileName?: string; helperExecPath: string; + mainModule: NodeJS.Module; } } @@ -271,6 +272,7 @@ declare interface Window { completeURL: (project: string, path: string) => string; } }; + WebView: typeof ElectronInternal.WebViewElement; ResizeObserver: ResizeObserver; trustedTypes: TrustedTypePolicyFactory; } diff --git a/typings/internal-electron.d.ts b/typings/internal-electron.d.ts index 6019a5ea45..851eb5a733 100644 --- a/typings/internal-electron.d.ts +++ b/typings/internal-electron.d.ts @@ -37,8 +37,12 @@ declare namespace Electron { removeListener(event: '-touch-bar-interaction', listener: (event: Event, itemID: string, details: any) => void): this; } + interface BrowserWindowConstructorOptions { + webContents?: WebContents; + } + interface ContextBridge { - internalContextBridge: { + internalContextBridge?: { contextIsolationEnabled: boolean; overrideGlobalValueFromIsolatedWorld(keys: string[], value: any): void; overrideGlobalValueWithDynamicPropsFromIsolatedWorld(keys: string[], value: any): void; @@ -88,6 +92,7 @@ declare namespace Electron { } interface WebFrame { + _executeJavaScript(code: string, userGesture?: boolean): Promise; getWebFrameId(window: Window): number; allowGuestViewElementDefinition(window: Window, context: any): void; } @@ -100,7 +105,7 @@ declare namespace Electron { interface WebPreferences { guestInstanceId?: number; - openerId?: number; + openerId?: number | null; disablePopups?: boolean; preloadURL?: string; embedder?: Electron.WebContents; @@ -147,9 +152,14 @@ declare namespace Electron { acceleratorWorksWhenHidden?: boolean; } + interface IpcMainEvent { + sendReply(value: any): void; + } + interface IpcMainInvokeEvent { + sendReply(value: any): void; _reply(value: any): void; - _throw(error: Error): void; + _throw(error: Error | string): void; } const deprecate: ElectronInternal.DeprecationUtil; @@ -250,10 +260,26 @@ declare namespace ElectronInternal { once(channel: string, listener: (event: IpcMainInternalEvent, ...args: any[]) => void): this; } + interface Event extends Electron.Event { + sender: WebContents; + } + interface LoadURLOptions extends Electron.LoadURLOptions { reloadIgnoringCache?: boolean; } + interface WebContentsPrintOptions extends Electron.WebContentsPrintOptions { + mediaSize?: MediaSize; + } + + type MediaSize = { + name: string, + custom_display_name: string, + height_microns: number, + width_microns: number, + is_default?: 'true', + } + type ModuleLoader = () => any; interface ModuleEntry {