如何访问< webview>的DOM.在电子学中? [英] How can I access the DOM of a <webview> in Electron?

查看:74
本文介绍了如何访问< webview>的DOM.在电子学中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用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

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.

[更新]这是一个我刚刚写完的简单示例:

[update] Here is a simple example that I just finished writing: Electron-Webview-Host-to-Guest-RPC-Sample

这篇关于如何访问&lt; webview&gt;的DOM.在电子学中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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