从电子webview获取选定的文本 [英] Get selected text from electron webview

查看:193
本文介绍了从电子webview获取选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从电子申请中的网页浏览中获取所选文字?
我正在使用Angular和Electron。所以我有一个具有webview的组件:

How to get the selected text from a webview in an electron application? I am using Angular with Electron. So I have a component which has a webview:

< webview id =fooattr.src = {{activeUrl}} style = 身高:600px>< / webview>

这是我用来获取所选文字的内容:

This is what I use for getting the selected text:

let rightClickPosition = null;
const menu = new Menu();
const menuItem = new MenuItem({
  label: 'Get selected text',
  click: () => {
    // does not work for selected text in webview
    console.log(window.getSelection().toString());
  }
});
menu.append(menuItem);
window.addEventListener('contextmenu', (e) => {
  e.preventDefault();
  rightClickPosition = {x: e.x, y: e.y};
  menu.popup(remote.getCurrentWindow());
}, false);

问题: window.getSelection()。toString()不适用于webview中的选定文本。它仅适用于webview之外的文本。

The problem: window.getSelection().toString() does not work for the selected text in the webview. It works only for the text outside the webview.

推荐答案

webView是Electron中的一种特殊标记。作为文件( https://electronjs.org/docs/api/webview-tag )说,与iframe不同,webview在一个独立的进程中运行而不是你的应用程序。它与您的网页没有相同的权限,您的应用和嵌入内容之间的所有互动都将是异步的。

webView is special kind of tag in Electron. as document (https://electronjs.org/docs/api/webview-tag) says, Unlike an iframe, the webview runs in a separate process than your app. It doesn't have the same permissions as your web page and all interactions between your app and embedded content will be asynchronous..

由于它是不同的过程并且不允许直接交互,因此您可以在webview和外部框架之间使用ipc进行通信。检查Electron的ipc以确定。具体来说,您可能对渲染器主机和webview的 ipcRenderer.sendToHost 感兴趣。

Since it's different process and doesn't allow direct interaction, way you can communicate is using ipc between webview and outer frame. Check Electron's ipc to establish. Specifically you may interested in ipcRenderer.sendToHost for renderer host and webview.

这篇关于从电子webview获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆