【问题标题】:Get selected text from electron webview从电子网络视图中获取选定的文本
【发布时间】:2018-05-29 02:38:24
【问题描述】:

如何从电子应用程序的 web 视图中获取选定的文本? 我正在使用 Angular 和 Electron。所以我有一个有 webview 的组件:

<webview id="foo" attr.src={{activeUrl}} style="height: 600px"></webview>

这是我用来获取选定文本的:

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() 不适用于 web 视图中的选定文本。它仅适用于 webview 之外的文本。

【问题讨论】:

    标签: javascript angular webview electron


    【解决方案1】:

    webView 是 Electron 中的一种特殊标签。正如文件 (https://electronjs.org/docs/api/webview-tag) 所说,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 是否建立。具体来说,您可能对 ipcRenderer.sendToHost 感兴趣,用于渲染器主机和 web 视图。

    【讨论】:

    • 谢谢!我在这里找到了一个教程:ourcodeworld.com/articles/read/201/…
    • @Mate 你找到解决方案了吗,我没有看到如何获取所选文本的教程,我想对所选文本执行“使用 google 搜索”。
    • @Wasima。您可以使用ipcRenderer.sendtoHost 函数。示例:ipcRenderer.on('request', function(){ ipcRenderer.sendToHost('selection', window.getSelection().toString()); });
    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多