-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
chrome-custom.cjs
88 lines (80 loc) · 3.51 KB
/
chrome-custom.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* USED FOR SITESPEED TEST (TO test real no javascript gain)!!!
* Right now sitespeed doesn't support blocking specific content type so this is our workaround
*/
module.exports = async function (context, commands) {
cdpClient = commands.cdp.engineDelegate.getCDPClient()
await cdpClient.send('Fetch.enable', {
patterns: [
{
urlPattern: '*',
resourceType: 'Document',
requestStage: 'Response'
}
]
});
cdpClient.on('Fetch.requestPaused', async function (reqEvent) {
if (reqEvent == undefined) {
return
}
const requestId = reqEvent.requestId;
let responseHeaders = reqEvent.responseHeaders || [];
if ('webperf' in context.options) {
for (var i = 1; i <= 9; i++) {
const key = 'header0X'.replace('X', i);
if (!(key in context.options.webperf)) {
// context.log.warn("NO " + key + " OPTIONS");
continue;
}
pair = context.options.webperf[key].replaceAll('%20', ' ').split('=');
const newServerHeader = { name: pair[0].replaceAll('%3D', '='), value: pair[1].replaceAll('%3D', '=') };
const foundHeaderIndex = responseHeaders.findIndex(
h => h.name === pair[0].replaceAll('%3D', '=')
);
if (foundHeaderIndex) {
context.log.warn("ADDED HTTP HEADER: " + pair[0].replaceAll('%3D', '=') + " = " + pair[1].replaceAll('%3D', '='));
responseHeaders[foundHeaderIndex] = newServerHeader;
} else {
context.log.warn("OVERRITE HTTP HEADER: " + pair[0].replaceAll('%3D', '=') + " = " + pair[1].replaceAll('%3D', '='));
responseHeaders.push(newServerHeader);
}
}
} else {
context.log.warn("NO PLUGIN OPTIONS");
}
reqEvent.responseHeaders = responseHeaders;
// HACK: for some reason we cant get this to work, so we will use workaround by calling fulfillRequest instead
// return cdpClient.send('Fetch.continueResponse', {
// requestId: requestId,
// responseCode: reqEvent.responseStatusCode,
// responseHeaders: responseHeaders
// });
bodyResult = await cdpClient.send('Fetch.getResponseBody', {
requestId: requestId
});
body = ''
if (bodyResult.base64Encoded) {
body = atob(bodyResult.body)
}
if ('webperf' in context.options) {
for (var i = 1; i <= 9; i++) {
const key = 'HTML0X'.replace('X', i);
if (!(key in context.options.webperf)) {
// context.log.warn("NO " + key + " OPTIONS");
continue;
}
pair = context.options.webperf[key].replaceAll('%20', ' ').split('=');
context.log.warn("HTML CHANGED: " + pair[0].replaceAll('%3D', '=') + " = " + pair[1].replaceAll('%3D', '='));
body = body.replace(pair[0].replaceAll('%3D', '='), pair[1].replaceAll('%3D', '='))
}
} else {
context.log.warn("NO PLUGIN OPTIONS");
}
return cdpClient.send('Fetch.fulfillRequest', {
requestId: requestId,
responseCode: reqEvent.responseStatusCode,
responseHeaders: responseHeaders,
body: bodyResult.body
});
});
}