电子操纵/拦截WebView请求和响应 [英] Electron Manipulate/Intercept WebView Requests and Responses

查看:247
本文介绍了电子操纵/拦截WebView请求和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个电子应用程序,该应用程序将使用 webview 来显示第三方的内容

I want to create an Electron app that will use webview to display 3rd party content.

我希望能够拦截来自此Webview的所有请求和响应。有时候我想操纵这些内容,有时我想记录它,而有时我却什么也不做。

I would like to be able to intercept all requests and responses from this 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.

作为回应的一个例子,也许Web服务器将使用TypeScript代码进行响应,也许我想接受该响应,并将其编译为标准JavaScript。

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.

我研究了此页面,但看起来像只能取消请求并处理标头。 WebRequest API 看起来并不符合我的用例需求,因为它只允许

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.

我还考虑过设置一些可以充当代理的Web服务器,但是对此我感到担心。我想维护用户的隐私,并且要确保托管第3方内容的Web服务器看起来好像请求来自类似环境的浏览器(例如Electron Webview),而不是服务器。我知道我可以使用发送的标题等来处理请求,但是整个解决方案变得越来越复杂,然后我想,但是可能是唯一的选择。

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. 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.

有什么更好的方法可以实现这一点,并且可以更好地控制Electron Webview?

Any better ways to achieve this, and have more control over the Electron webview?

推荐答案

我认为您应该看一下放入协议API 。它在内部用作代理。
说您想让用户打开 http://www.google.com 并看到类似的内容,您已经被限制了!

I think you should look into The Protocol API. it works as a proxy internally. say you wanna the user open http://www.google.com and see content like 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
});

WebRequest API 。但这比独立的本地代理要简单得多。

there's lots of work to do, comparing to the WebRequest API. but it's much simpler than a independent local proxy.

这篇关于电子操纵/拦截WebView请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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