I have electron app (^19.0.1) that opens website via url in BrowserWindow. I need to create text search functionality like standard Chrome searchbar.
At this moment i can find and mark searchable text via webContents.findInPage, but i couldn't jump to next result, what i'm doing wrong?
My main.js:
win.webContents.on('found-in-page', (event, result) => {
console.log(result)
});
ipcMain.on('search-text', (event, arg) => {
let nextRes = arg.direction == 'next' ? true : false
const requestId = win.webContents.findInPage(arg.searchText, {
forward: true,
findNext: nextRes,
matchCase: false
});
});
ipcMain.on('stop-search', (event, arg) => {
win.webContents.stopFindInPage('clearSelection');
});
My preload.js:
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('electronAPI', {
searchText: (searchText, direction) => ipcRenderer.send('search-text', { searchText, direction }),
stopSearch: () => ipcRenderer.send('stop-search')
})
function search(direction) {
let searched = document.getElementById("searchInput").value.trim();
if(searched.length > 0){
window.electronAPI.searchText(searched,direction)
}
document.getElementById("searchInput").focus()
}