-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpatchright_nodejs_patch.js
133 lines (116 loc) · 5.37 KB
/
patchright_nodejs_patch.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Project, SyntaxKind, IndentationText } from "ts-morph";
const project = new Project({
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
},
});
// ----------------------------
// client/clientHelper.ts
// ----------------------------
const clientHelperSourceFile = project.addSourceFileAtPath(
"packages/playwright-core/src/client/clientHelper.ts",
);
// ------- addSourceUrlToScript Function -------
const addSourceUrlToScriptFunction = clientHelperSourceFile.getFunction("addSourceUrlToScript");
addSourceUrlToScriptFunction.setBodyText(`return script`);
// ----------------------------
// client/browserContext.ts
// ----------------------------
const clientBrowserContextSourceFile = project.addSourceFileAtPath(
"packages/playwright-core/src/client/browserContext.ts",
);
// ------- BrowserContext Class -------
const clientBrowserContextClass = clientBrowserContextSourceFile.getClass("BrowserContext");
clientBrowserContextClass.addProperty({ name: "routeInjecting", type: "boolean", initializer: "false" });
// -- addInitScript Method --
const clientAddInitScriptMethod = clientBrowserContextClass.getMethod("addInitScript");
const clientAddInitScriptMethodBody = clientAddInitScriptMethod.getBody();
clientAddInitScriptMethodBody.insertStatements(0, "await this.installInjectRoute();");
// -- exposeBinding Method --
const clientExposeBindingMethod = clientBrowserContextClass.getMethod("exposeBinding");
const clientExposeBindingMethodBody = clientExposeBindingMethod.getBody();
clientExposeBindingMethodBody.insertStatements(0, "await this.installInjectRoute();");
// -- exposeFunction Method --
const clientExposeFunctionMethod = clientBrowserContextClass.getMethod("exposeFunction");
const clientExposeFunctionMethodBody = clientExposeFunctionMethod.getBody();
clientExposeFunctionMethodBody.insertStatements(0, "await this.installInjectRoute();");
// -- installInjectRoute Method --
clientBrowserContextClass.addMethod({
name: "installInjectRoute",
isAsync: true,
});
const clientBrowserContextInstallInjectRouteMethod = clientBrowserContextClass.getMethod("installInjectRoute");
clientBrowserContextInstallInjectRouteMethod.setBodyText(`
if (this.routeInjecting) return;
await this.route('**/*', async route => {
if (route.request().resourceType() === 'document' && route.request().url().startsWith('http')) {
try {
const response = await route.fetch({ maxRedirects: 0 });
await route.fulfill({ response: response });
} catch (e) {
await route.continue();
}
} else {
await route.continue();
}
});
this.routeInjecting = true;
`);
// ----------------------------
// client/page.ts
// ----------------------------
const clientPageSourceFile = project.addSourceFileAtPath(
"packages/playwright-core/src/client/page.ts",
);
// ------- Page Class -------
const clientPageClass = clientPageSourceFile.getClass("Page");
clientPageClass.addProperty({ name: "routeInjecting", type: "boolean", initializer: "false" });
// -- addInitScript Method --
const clientPageAddInitScriptMethod = clientPageClass.getMethod("addInitScript");
const clientPageAddInitScriptMethodBody = clientPageAddInitScriptMethod.getBody();
clientPageAddInitScriptMethodBody.insertStatements(0, "await this.installInjectRoute();");
// -- exposeBinding Method --
const clientPageExposeBindingMethod = clientPageClass.getMethod("exposeBinding");
const clientPageExposeBindingMethodBody = clientPageExposeBindingMethod.getBody();
clientPageExposeBindingMethodBody.insertStatements(0, "await this.installInjectRoute();");
// -- exposeFunction Method --
const clientPageExposeFunctionMethod = clientPageClass.getMethod("exposeFunction");
const clientPageExposeFunctionMethodBody = clientPageExposeFunctionMethod.getBody();
clientPageExposeFunctionMethodBody.insertStatements(0, "await this.installInjectRoute();");
// -- installInjectRoute Method --
clientPageClass.addMethod({
name: "installInjectRoute",
isAsync: true,
});
const clientPageInstallInjectRouteMethod = clientPageClass.getMethod("installInjectRoute");
clientPageInstallInjectRouteMethod.setBodyText(`
if (this.routeInjecting || this.context().routeInjecting) return;
await this.route('**/*', async route => {
if (route.request().resourceType() === 'document' && route.request().url().startsWith('http')) {
try {
const response = await route.fetch({ maxRedirects: 0 });
await route.fulfill({ response: response });
} catch (e) {
await route.continue();
}
} else {
await route.continue();
}
});
this.routeInjecting = true;
`);
// ----------------------------
// client/clock.ts
// ----------------------------
const clientClockSourceFile = project.addSourceFileAtPath(
"packages/playwright-core/src/client/clock.ts",
);
// ------- Page Class -------
const clientClockClass = clientClockSourceFile.getClass("Clock");
// -- install Method --
const clientInstallMethod = clientClockClass.getMethod("install");
const clientInstallMethodBody = clientInstallMethod.getBody();
clientInstallMethodBody.insertStatements(0, "await this._browserContext.installInjectRoute()");
// Here the Driver Patch will be added by fetching the code from the main Driver Repository (in the workflow).
// The URL from which the code is added is: https://raw.githubusercontent.com/Kaliiiiiiiiii-Vinyzu/patchright/refs/heads/main/patchright_driver_patch.js
// Note: The Project is also synced (saved) in this code, so we dont need to add it here.