Add webContents.getFocusedWebContents()
authorKevin Sawicki <kevinsawicki@gmail.com>
Wed, 13 Jul 2016 19:15:30 +0000 (12:15 -0700)
committerKevin Sawicki <kevinsawicki@gmail.com>
Wed, 13 Jul 2016 21:16:56 +0000 (14:16 -0700)
lib/browser/api/web-contents.js

index e418077316fff7c3581fb2300f88f6f78f766fa5..8da8a2bd6960e839b3c4880c31ef6551fbd9c9e5 100644 (file)
@@ -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
   }
 }