Skip to content

Commit 9f4b1d1

Browse files
committed
chore: Improved VSCode debug
1 parent 1ac06c3 commit 9f4b1d1

File tree

5 files changed

+238
-27
lines changed

5 files changed

+238
-27
lines changed

.vscode/launch.json

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,49 @@
44
// Para obter mais informações, acesse: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "Launch Chrome",
9+
"request": "launch",
10+
"type": "chrome",
11+
"url": "about:blank",
12+
"disableNetworkCache": true,
13+
"webRoot": "${workspaceFolder}",
14+
"userDataDir": "${workspaceFolder}/userDataDir",
15+
"port": 9222,
16+
"runtimeArgs": ["--auto-open-devtools-for-tabs"],
17+
"pathMapping": {
18+
"webpack:///./~/*": "${workspaceFolder}/node_modules/*",
19+
"webpack:////*": "/*",
20+
"webpack://?:*/*": "${workspaceFolder}/*",
21+
"webpack:///([a-z]):/(.+)": "$1:/$2",
22+
"/dist": "${workspaceFolder}/dist",
23+
"/": "${workspaceFolder}/wa-source"
24+
}
25+
},
726
{
827
"type": "node",
928
"request": "launch",
10-
"name": "Launch Browser",
29+
"name": "Debug Chrome",
1130
"presentation": {
1231
"hidden": false,
1332
"group": "Other",
1433
"order": 2
1534
},
1635
"preLaunchTask": "npm: watch",
1736
"runtimeExecutable": "npm",
37+
"runtimeArgs": ["run", "launch:attach"]
38+
},
39+
{
40+
"type": "node",
41+
"request": "launch",
42+
"name": "Launch Browser",
43+
"presentation": {
44+
"hidden": false,
45+
"group": "Other Old",
46+
"order": 2
47+
},
48+
"preLaunchTask": "npm: watch",
49+
"runtimeExecutable": "npm",
1850
"runtimeArgs": [
1951
"run",
2052
"launch:local",
@@ -23,12 +55,12 @@
2355
]
2456
},
2557
{
26-
"type": "pwa-chrome",
58+
"type": "chrome",
2759
"request": "attach",
2860
"name": "Attach Browser",
2961
"presentation": {
3062
"hidden": false,
31-
"group": "Other",
63+
"group": "Other Old",
3264
"order": 2
3365
},
3466
"address": "127.0.0.1",
@@ -48,13 +80,13 @@
4880
],
4981
"compounds": [
5082
{
51-
"name": "Launch and debug",
83+
"name": "Launch and Debug",
5284
"presentation": {
5385
"hidden": false,
5486
"group": "Launch",
5587
"order": 1
5688
},
57-
"configurations": ["Launch Browser", "Attach Browser"]
89+
"configurations": ["Launch Chrome", "Debug Chrome"]
5890
}
5991
]
6092
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"docs:build": "npm run docs:clean && npm run docs:theme && typedoc --gitRevision main",
2424
"docs:clean": "shx rm -rf docs",
2525
"docs:theme": "cd ./docs-source && tsc",
26+
"launch:attach": "ts-node ./src/tools/launchAttach.ts",
2627
"launch:local": "ts-node ./src/tools/launchLocal.ts",
2728
"launch:protocol-log": "ts-node ./src/tools/launchProtocolLog.ts",
2829
"lint": "npx eslint --ext .ts src",

src/tools/browser.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,7 @@ type LaunchArguments = Parameters<
2929
typeof playwright.chromium.launchPersistentContext
3030
>;
3131

32-
export async function getPage(options?: LaunchArguments[1]) {
33-
let userDataDir = path.resolve(__dirname, '../../userDataDir');
34-
if (Array.isArray(options?.args)) {
35-
const index = options?.args.findIndex((a) =>
36-
a.startsWith('--user-data-dir')
37-
);
38-
if (typeof index === 'number' && index > -1) {
39-
const param = options?.args[index];
40-
options?.args.splice(index, 1);
41-
userDataDir = param?.split('=')[1] || userDataDir;
42-
}
43-
}
44-
45-
const browser = await playwright.chromium.launchPersistentContext(
46-
userDataDir,
47-
options
48-
);
49-
50-
const page = browser.pages().length
51-
? browser.pages()[0]
52-
: await browser.newPage();
53-
32+
export async function preparePage(page: playwright.Page) {
5433
page.route('https://crashlogs.whatsapp.net/**', (route) => {
5534
route.abort();
5635
});
@@ -145,6 +124,31 @@ export async function getPage(options?: LaunchArguments[1]) {
145124
);
146125
});
147126
});
127+
}
128+
129+
export async function getPage(options?: LaunchArguments[1]) {
130+
let userDataDir = path.resolve(__dirname, '../../userDataDir');
131+
if (Array.isArray(options?.args)) {
132+
const index = options?.args.findIndex((a) =>
133+
a.startsWith('--user-data-dir')
134+
);
135+
if (typeof index === 'number' && index > -1) {
136+
const param = options?.args[index];
137+
options?.args.splice(index, 1);
138+
userDataDir = param?.split('=')[1] || userDataDir;
139+
}
140+
}
141+
142+
const browser = await playwright.chromium.launchPersistentContext(
143+
userDataDir,
144+
options
145+
);
146+
147+
const page = browser.pages().length
148+
? browser.pages()[0]
149+
: await browser.newPage();
150+
151+
await preparePage(page);
148152

149153
setTimeout(async () => {
150154
await page.goto(URL, {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*!
2+
* Copyright 2021 WPPConnect Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as waVersion from '@wppconnect/wa-version';
18+
import * as fs from 'fs';
19+
import * as path from 'path';
20+
import * as prettier from 'prettier';
21+
22+
import * as wpp from '../';
23+
import { getPage, WA_DIR } from './browser';
24+
25+
declare global {
26+
interface Window {
27+
WPP: typeof wpp;
28+
}
29+
}
30+
31+
const WA_VERSION = process.env['WA_VERSION'] || waVersion.getLatestVersion();
32+
33+
async function start() {
34+
const args = process.argv.slice(2);
35+
36+
if (!fs.existsSync(WA_DIR)) {
37+
fs.mkdirSync(WA_DIR);
38+
}
39+
40+
const options = await prettier.resolveConfig(process.cwd());
41+
42+
const { browser, page } = await getPage({
43+
args,
44+
});
45+
46+
const downloads: Promise<void>[] = [];
47+
48+
page.on('response', async (response) => {
49+
const contentType = response.headers()['content-type'] || '';
50+
51+
if (!contentType.startsWith('application/javascript')) {
52+
return;
53+
}
54+
55+
const url = response.url();
56+
const fileName = path.basename(url, '.js') + '.js';
57+
const filePath = path.join(WA_DIR, fileName);
58+
59+
if (fs.existsSync(filePath)) {
60+
return;
61+
}
62+
63+
let resolve: any = null;
64+
downloads.push(
65+
new Promise((r) => {
66+
resolve = r;
67+
})
68+
);
69+
70+
console.log('Downloading: ', fileName);
71+
72+
const body = await response.body();
73+
74+
let content = body.toString('utf8');
75+
76+
content = content.replace(/([ ,:;=>(){}[\]|&])!1\b/g, '$1false');
77+
content = content.replace(/([ ,:;=>(){}[\]|&])!0\b/g, '$1true');
78+
content = content.replace(/\b(return|case)\s*!1\b/g, '$1 false');
79+
content = content.replace(/\b(return|case)\s*!0\b/g, '$1 true');
80+
content = content.replace(/\bvoid 0\b/g, 'undefined');
81+
// Remove sourcemap because it not exists in production
82+
content = content.replace(/\/\/# sourceMappingURL.*/g, '');
83+
84+
content = prettier.format(content, {
85+
...options!,
86+
parser: 'espree',
87+
printWidth: 120,
88+
});
89+
90+
content = `/*! WhatsApp Version: ${WA_VERSION} */\n` + content;
91+
92+
fs.writeFileSync(filePath, content, { encoding: 'utf8' });
93+
94+
resolve?.();
95+
});
96+
97+
await page.waitForFunction(() => window.WPP?.isReady, null, {
98+
timeout: 0,
99+
});
100+
101+
await new Promise((resolve) => setTimeout(resolve, 5000));
102+
103+
await Promise.all(downloads);
104+
105+
await browser.close();
106+
}
107+
start();

src/tools/launchAttach.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*!
2+
* Copyright 2021 WPPConnect Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as playwright from 'playwright-chromium';
18+
19+
import { preparePage, URL } from './browser';
20+
21+
async function start() {
22+
console.log('Connecting to Chrome');
23+
const browser = await playwright.chromium.connectOverCDP(
24+
'http://localhost:9222'
25+
);
26+
console.log('Connected');
27+
28+
const context = browser.contexts()[0];
29+
30+
const page = context.pages().length
31+
? context.pages()[0]
32+
: await context.newPage();
33+
34+
console.log('Preparing page');
35+
await preparePage(page);
36+
37+
setTimeout(async () => {
38+
console.log('Opening WhatsApp WEB');
39+
await page.goto(URL, {
40+
waitUntil: 'domcontentloaded',
41+
timeout: 120000,
42+
});
43+
44+
await page
45+
.waitForFunction(
46+
() => (window as any).Debug?.VERSION,
47+
{},
48+
{ timeout: 120000 }
49+
)
50+
.catch(() => null);
51+
52+
const version = await page
53+
.evaluate(() => (window as any).Debug.VERSION)
54+
.catch(() => null);
55+
56+
console.log('WhatsApp Version: ', version);
57+
}, 1000);
58+
59+
page.on('load', (page) => {
60+
const debug = process.env['DEBUG'] || '*';
61+
62+
page.evaluate((debug: string) => {
63+
localStorage.setItem('debug', debug);
64+
}, debug);
65+
});
66+
}
67+
start();

0 commit comments

Comments
 (0)