gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:24:23 56 4
gpt4 key购买 nike

我尝试使用contents.findInPage
我在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);
}
}

我在此 answer的示例上编写了此代码。
但是功能发现不起作用。我不明白如何发送要搜索的文本和要搜索的词。

如何使用findInPage函数?

最佳答案

对不起,我对另一个问题的回答还不够清楚(那是2年前!我不太记得了,但我会给你一个机会)

这是webcontentsIPCMain的文档

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

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监听器:
ipcRenderer.on('find_request', () => {
const modalbox = document.getElementById('modalbox');
if (modalbox.style.display === 'block') {
modalbox.style.display = 'none';
} else {
modalbox.style.display = 'block';
}
});

然后,我做了一个模式搜索框:
const searchBox = (
<div
id="modalbox"
style={{ display: 'none', position: 'fixed', zIndex: 1 }}
><input type="text" onChange={Calls.searchPage} />
</div>);

onchange将输入文本发送到ipc监听器:
static searchPage(event) {
ipcRenderer.send('search-text', event.target.value);
}

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

关于electron - 如何将搜索文本发送到Electron中的findInPage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57100685/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com