简体   繁体   English

电子操纵/拦截 WebView 请求和响应

[英]Electron Manipulate/Intercept WebView Requests and Responses

I want to create an Electron app that will use webview to display 3rd party content.我想创建一个使用webview来显示 3rd 方内容的 Electron 应用程序。

I would like to be able to intercept all requests and responses from this webview.我希望能够拦截来自这个 webview 的所有请求和响应。 Sometimes I would like to manipulate this content, other times I would like to log it, and other times I'd like to do nothing.有时我想操纵这些内容,有时我想记录它,有时我什么也不想做。

As one example for the responses, maybe a web server will respond with TypeScript code, maybe I want to take that response, and compile it to standard JavaScript.作为响应的一个示例,也许 Web 服务器将使用 TypeScript 代码进行响应,也许我想获取该响应,并将其编译为标准 JavaScript。

I have looked into this page but it looks like it is only possible to cancel requests, and manipulate the headers.我查看了此页面,但看起来只能取消请求并操作标头。 The WebRequest API doesn't look to fit the needs of my use case since it only allows very minor manipulations of requests and responses. WebRequest API看起来不适合我的用例的需求,因为它只允许对请求和响应进行非常小的操作。

I have also considered setting up some time of web server that can act as a proxy, but I have concerns about that.我也考虑过设置一些可以充当代理的网络服务器,但我对此感到担忧。 I want to maintain user privacy, and I want to ensure that to the web servers that host the 3rd party content it looks like the request is coming from a browser like environment (ex. Electron webview) instead of a server.我想维护用户隐私,并且我想确保托管第 3 方内容的 Web 服务器看起来请求来自类似浏览器的环境(例如 Electron webview)而不是服务器。 I know I can manipulate requests with the headers I send and such, but this whole solution is getting to be a lot more complicated, then I would like, but might be the only option.我知道我可以使用我发送的标头等操作请求,但是整个解决方案变得更加复杂,然后我想要,但可能是唯一的选择。

Any better ways to achieve this, and have more control over the Electron webview?有没有更好的方法来实现这一点,并且可以更好地控制 Electron webview?

I think you should look into the Protocol API .我认为您应该研究一下Protocol API It works as a proxy internally.它在内部充当代理。

Say you want the user, when opening http://www.google.com , to see content like you've been conned!假设您希望用户在打开http://www.google.com时看到you've been conned! :

 const { protocol } = require("electron"); const content = new Buffer("you've been conned!"); protocol.interceptBufferProtocol("http", (request, result) => { if (request.url === "http://www.google.com") return result(content); ... // fetch other http protocol content and return to the electron });

There's lots of work to do, compared to the WebRequest API , but it's much simpler than an independent local proxy.WebRequest API相比,还有很多工作要做,但它比独立的本地代理要简单得多。

To get the request body of any http network call made by your electron app:要获取您的电子应用程序发出的任何 http 网络调用的请求正文:

session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
        if (details.uploadData) {
            const buffer = Array.from(details.uploadData)[0].bytes;
            console.log('Request body: ', buffer.toString());
        }
        callback(details);
      })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM