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

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

问题描述

我想创建一个使用 webview 来显示第 3 方内容的 Electron 应用.

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

我希望能够拦截来自此 web 视图的所有请求和响应.有时我想操纵这些内容,有时我想记录它,有时我什么都不做.

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.

我也考虑过设置一些可以充当代理的网络服务器,但我对此感到担忧.我想维护用户隐私,并且我想确保托管第 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 并查看像 youve been conned! 这样的内容.

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 请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)