如何拦截包括表单提交在内的所有http请求 [英] How to intercept all http requests including form submits
问题描述
我想拦截从我的网页发出的所有 http 请求,并向请求正文添加一个参数.我的页面包含表单 - 我还想捕获表单提交.我曾尝试使用 Jquery ajaxSend 和 Javascript 的 setRequestHeader,但两者都不适合我.我如何实现这一目标?
I would like to intercept all http requests going out from my web page and add a parameter to the request body. My page includes forms - I also want to capture form submits. I have tried using Jquery ajaxSend and Javascript's setRequestHeader but both did not work for me. How do I achieve this?
谢谢
推荐答案
https://developer.mozilla.org/en/docs/Web/API/Service_Worker_API
Service Worker 本质上充当位于 Web 应用程序、浏览器和网络(如果可用)之间的代理服务器.
Service workers essentially act as proxy servers that sit between web applications, and the browser and network (when available).
它采用JavaScript文件的形式,可以控制与其关联的网页/站点,拦截和修改导航和资源请求
It takes the form of a JavaScript file that can control the web page/site it is associated with, intercepting and modifying navigation and resource requests
您在应用程序代码中从名为 sw.js
的文件注册一个 Service Worker,方法如下:
You register a service worker in your application code from a file named, e.g., sw.js
by doing:
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('sw.js').then(function(registration) {
console.log('Service worker registered with scope: ', registration.scope);
}, function(err) {
console.log('ServiceWorker registration failed: ', err);
});
});
}
在 sw.js
文件(实际的 service-worker 代码)中:为了拦截请求,你将一个 fetch
事件监听器附加到调用respondWith()
方法并对事件对象中的 .request
成员执行某些操作:
And in the sw.js
file (the actual service-worker code): To intercept requests, you attach a fetch
event listener to the service worker that calls the respondWith()
method and does something with the .request
member from the event object:
self.addEventListener('fetch', function(event) {
event.respondWith(
// intercept requests by handling event.request here
);
});
一个简单的 Service Worker 如下所示:
A simple service worker that just passes through requests unchanged looks like this:
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request)
);
});
要在请求正文中添加参数,您需要:
To add a param to the request body, you need to:
- 序列化请求.
- 修改该序列化请求.
- 反序列化修改后的请求以创建新请求.
- 使用该新请求调用
fetch(...)
.
因此,一个完成所有这些工作的 Service Worker 看起来像这样(未经测试):
So, a service worker that does all that would look like this (untested):
self.addEventListener('fetch', function(event) {
event.respondWith(
fetchWithParamAddedToRequestBody(event.request)
);
});
function fetchWithParamAddedToRequestBody(request) {
serialize(request).then(function(serialized) {
// modify serialized.body here to add your request parameter
deserialize(serialized).then(function(request) {
return fetch(request);
});
}); // fixed this
}
function serialize(request) {
var headers = {};
for (var entry of request.headers.entries()) {
headers[entry[0]] = entry[1];
}
var serialized = {
url: request.url,
headers: headers,
method: request.method,
mode: request.mode,
credentials: request.credentials,
cache: request.cache,
redirect: request.redirect,
referrer: request.referrer
};
if (request.method !== 'GET' && request.method !== 'HEAD') {
return request.clone().text().then(function(body) {
serialized.body = body;
return Promise.resolve(serialized);
});
}
return Promise.resolve(serialized);
}
function deserialize(data) {
return Promise.resolve(new Request(data.url, data));
}
注意: https://serviceworke.rs/request-deferrer_service-worker_doc.html,Service Worker Cookbook 中的一个页面,是我在其中提取serialize(...)
代码/方法来自——通过 https://stackoverflow.com/questions/35420980/how-to-alter-the-headers-of-a-request/35421644#35421644—和 值得一看,因为那里的代码有详细的注释,解释了它在做什么
Note: https://serviceworke.rs/request-deferrer_service-worker_doc.html, a page from the Service Worker Cookbook, is where I lifted that serialize(…)
code/approach from—by way of the answer at https://stackoverflow.com/questions/35420980/how-to-alter-the-headers-of-a-request/35421644#35421644—and it’s worth taking a look at, because the code there has detailed annotations explaining what it’s all doing
这篇关于如何拦截包括表单提交在内的所有http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!