问题描述
我刚刚开始使用Electron,并具有使用node-webkit(nw.js)的经验.
I'm just getting started with Electron, with prior experience with node-webkit (nw.js).
在nw.js中,我能够创建iframe,然后访问所述iframe的DOM,以获取诸如标题,favicon和& c之类的内容.几天前,当我拿起Electron将其nw.js应用程序移植到它时,我看到了使用Webview而不是iframe的建议,这仅仅是因为它们更好.现在,我上面提到的功能在nw.js中相对容易实现,但是我不知道如何在Electron中实现(示例几乎没有).有人可以帮忙吗?
In nw.js, I was able to create iframes and then access the DOM of said iframe in order to grab things like the title, favicon, &c. When I picked up Electron a few days ago to port my nw.js app to it, I saw advice to use webviews instead of iframes, simply because they were better. Now, the functionality I mentioned above was relatively easy to do in nw.js, but I don't know how to do it in Electron (and examples are slim to none). Can anyone help?
此外,我有用于Web视图的后退/前进按钮(并且我打算使用多个按钮).我在文档中看到可以在Webview上调用用于执行此操作的函数,但是我也没有尝试过(也没有发现它们在野外使用的示例).
Also, I have back/forward buttons for my webview (and I intend on having more than one). I saw in the documentation that I could call functions for doing so on a webview, but nothing I have tried worked either (and, I haven't found examples of them being used in the wild).
推荐答案
在来宾旁边以 NetOperatorWibby ,从主持人到来宾也非常有用.目前唯一的方法是使用< webview> .executeJavaScript(code,userGesture).这个api有点粗糙,但是可以用.
Besides guest to host IPC calls as NetOperatorWibby, it is also very useful to go from host to guest. The only way to do this at present is to use the <webview>.executeJavaScript(code, userGesture). This api is a bit crude but it works.
如果您与远程来宾一起工作,例如扩展"第三方网页,则还可以使用Webview preload 属性,该属性将在页面上运行任何其他脚本之前执行自定义脚本.请注意,出于安全原因,预加载api将在自定义脚本完成时对自定义JS文件的根名称空间中创建的所有函数进行核对,但是此托管过程不会对您在根中声明的任何对象进行核对.因此,如果您希望自定义函数得以保留,请将它们捆绑到一个singleton对象中,并且自定义API将在页面完全加载后保留.
If you are working with a remote guest, like "extending" a third party web page, you can also utilize webview preload attribute which executes your custom script before any other scripts are run on the page. Just note that the preload api, for security reasons, will nuke any functions that are created in the root namespace of your custom JS file when your custom script finishes, however this custodial process will not nuke any objects you declare in the root. So if you want your custom functions to persist, bundle them into a singleton object and your custom APIs will persist after the page fully loads.
[更新]这是一个我刚刚写完的简单示例:电子Webview-Host-to-Guest-RPC样本
[update] Here is a simple example that I just finished writing: Electron-Webview-Host-to-Guest-RPC-Sample
这篇关于如何访问< webview>的DOM.在电子学中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!