2017-12-14 7 views
1

電子アプリケーションでWebビューから選択したテキストを取得するにはどうすればよいですか? 私はAngular with 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()で選択したテキストのために動作しません。 webview。これはwebviewの外のテキストに対してのみ機能します。

答えて

0

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を使用することです。電子のIPCを確認して確立する。具体的には、ipcRenderer.sendToHostレンダラーホストとwebviewに興味があります。

+0

ありがとうございました!私はここでチュートリアルを見つけました:https://ourcodeworld.com/articles/read/201/how-to-send-retrieve-information-and-manipulate-the-dom-from-a-webview-with-electron-framework – Mate

関連する問題