Full text search in Electron BrowserWindow

I know this is an old thread, but might still be relevant for people out there. Had the same problem, and first fixed by using electron-in-page-search, but this component doesn't work properly with Electron 2 or greater.

Then finally found electron-find resolved my problem. Using with Electron 4.

You just add the component to your project:

npm install electron-find --save

Add a global shortcut in your Electron main process to send an event to the renderer in a ctrl+f:

globalShortcut.register('CommandOrControl+F', () => {
    window.webContents.send('on-find');
});

And then you can add this to your page (the renderer process)

const remote = require('electron').remote;
const FindInPage = require('electron-find').FindInPage;

let findInPage = new FindInPage(remote.getCurrentWebContents());

ipcRenderer.on('on-find', (e, args) => {
  findInPage.openFindWindow()
})

Hope that helps.


Instead of using global shortcuts , use Accelerators ( normal Keyboard shortcut )

{
label : 'help',  
   click : function(){. 
                electron.shell.openExternal('http://....'). 
           },
   accelerator : 'CmdOrCtrl+ Shift + H'
}

The above shown is just an example of How to use accelerator


There is an issue with the solution Robson Hermes offered. globalShortcut is, by definition, global, so the shortcut will be detected even when the app is not focused. This will result in the Ctrl+F shortcut being "stolen" from everywhere else.

I have found no ideal solution (see this issue on the electron repository), but a hacky one can be achieved by doing what Robson said and adding

win.on('focus', () => {
  globalShortcut.register('CommandOrControl+F', () => windows.main.send('on-find'))
})
win.on('blur', () => {
  globalShortcut.unregister('CommandOrControl+F')
}

Note that as seen here, this is not ideal and can lead to several issues:

  1. Other applications can get a lock on the shortcut when you lose focus, i.e. the shortcut will magically stop working when you switch back to the app later.
  2. Some apps can appear on screen without taking focus (spotlight I believe has this behavior) and during the app's appearance the shortcuts will still be captured by your application.
  3. There's also gonna be those weird one in a thousand situations where somehow you switch focus and the shortcut is not removed.

Try webContents.findInPage just added in the latest version.