diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index e418077316f..8da8a2bd696 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -2,6 +2,7 @@ const {EventEmitter} = require('events') const {app, ipcMain, session, Menu, NavigationController} = require('electron') +const {getAllWebContents} = process.atomBinding('web_contents') // session is not used here, the purpose is to make sure session is initalized // before the webContents module. @@ -245,7 +246,18 @@ module.exports = { fromId (id) { return binding.fromId(id) }, - getAllWebContents () { - return binding.getAllWebContents() + + getFocusedWebContents () { + let focused = null + for (let contents of getAllWebContents()) { + if (!contents.isFocused()) continue + + // Return webview web contents which may be embedded inside another + // web contents that is also reporting as focused + if (contents.getType() === 'webview') return contents + + if (focused == null) focused = contents + } + return focused } }