如何将搜索文本发送到Electron中的findInPage

如何将搜索文本发送到Electron中的findInPage

本文介绍了如何将搜索文本发送到Electron中的findInPage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用。
我在index.js中有代码:

I try using contents.findInPage.I have code in index.js:

  const { webContents } = require('electron')
  webContents.on('found-in-page', (event, result) => {
    if (result.finalUpdate) webContents.stopFindInPage('clearSelection')
  })

  const requestId = webContents.findInPage('api')
  console.log(requestId)

并在组件中编写代码:

searchText(value){
    this.query = value;
    if (this.query.length > 0) {
        ipcRenderer.send('api', this.query);
    }
}

我在这个。
但是函数查找不起作用。我不明白如何发送要搜索的文本和要搜索的单词。

I wrote this code on the example of this answer.But function find not work. I do not understand how I can send the text to be searched and the word to be searched.

如何使用findInPage函数?

推荐答案

很抱歉,我对另一个问题的回答不够清楚(那是2年前的事!我不记得了好吧,但我会给它一个机会)

sorry my answer to the other question wasn't clear enough (it was 2 years ago! I don't remember it that well but I'll give it a shot)

这是和

这是我main.development.js(mainWindow和ipc通信的全局变量)中的内容:

Here's what I have in my main.development.js (globals for the mainWindow and ipc communication):

mainWindow.on('focus', () => {
globalShortcut.register('CmdorCtrl+F', () => {
  mainWindow.webContents.send('find_request', '');
  });
});

mainWindow.webContents.on('found-in-page', (event, result) => {
if (result.finalUpdate) {
  mainWindow.webContents.stopFindInPage('keepSelection');
}
});
ipcMain.on('search-text', (event, arg) => {
  mainWindow.webContents.findInPage(arg);
});

mainWindow.on('blur', () => {
   globalShortcut.unregister('CmdorCtrl+F');
});

然后我为CmdorCtrl + F制作了一个ipc监听器:

Then I made an ipc listener for CmdorCtrl+F:

ipcRenderer.on('find_request', () => {
  const modalbox = document.getElementById('modalbox');
 if (modalbox.style.display === 'block') {
   modalbox.style.display = 'none';
} else {
  modalbox.style.display = 'block';
 }
});

然后我创建了一个模式搜索框:

Then I made a modal searchbox:

const searchBox = (
  <div
    id="modalbox"
    style={{ display: 'none', position: 'fixed', zIndex: 1 }}
  ><input type="text" onChange={Calls.searchPage} />
  </div>);

onchange将输入文本发送到ipc侦听器:

The onchange sends the input text to the ipc listener:

static searchPage(event) {
  ipcRenderer.send('search-text', event.target.value);
}

我希望这足以使您解决问题:)

I hope this is enough for you to get it fixed :)

这篇关于如何将搜索文本发送到Electron中的findInPage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:59