-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ac06c3
commit 9f4b1d1
Showing
5 changed files
with
238 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/*! | ||
* Copyright 2021 WPPConnect Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as waVersion from '@wppconnect/wa-version'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as prettier from 'prettier'; | ||
|
||
import * as wpp from '../'; | ||
import { getPage, WA_DIR } from './browser'; | ||
|
||
declare global { | ||
interface Window { | ||
WPP: typeof wpp; | ||
} | ||
} | ||
|
||
const WA_VERSION = process.env['WA_VERSION'] || waVersion.getLatestVersion(); | ||
|
||
async function start() { | ||
const args = process.argv.slice(2); | ||
|
||
if (!fs.existsSync(WA_DIR)) { | ||
fs.mkdirSync(WA_DIR); | ||
} | ||
|
||
const options = await prettier.resolveConfig(process.cwd()); | ||
|
||
const { browser, page } = await getPage({ | ||
args, | ||
}); | ||
|
||
const downloads: Promise<void>[] = []; | ||
|
||
page.on('response', async (response) => { | ||
const contentType = response.headers()['content-type'] || ''; | ||
|
||
if (!contentType.startsWith('application/javascript')) { | ||
return; | ||
} | ||
|
||
const url = response.url(); | ||
const fileName = path.basename(url, '.js') + '.js'; | ||
const filePath = path.join(WA_DIR, fileName); | ||
|
||
if (fs.existsSync(filePath)) { | ||
return; | ||
} | ||
|
||
let resolve: any = null; | ||
downloads.push( | ||
new Promise((r) => { | ||
resolve = r; | ||
}) | ||
); | ||
|
||
console.log('Downloading: ', fileName); | ||
|
||
const body = await response.body(); | ||
|
||
let content = body.toString('utf8'); | ||
|
||
content = content.replace(/([ ,:;=>(){}[\]|&])!1\b/g, '$1false'); | ||
content = content.replace(/([ ,:;=>(){}[\]|&])!0\b/g, '$1true'); | ||
content = content.replace(/\b(return|case)\s*!1\b/g, '$1 false'); | ||
content = content.replace(/\b(return|case)\s*!0\b/g, '$1 true'); | ||
content = content.replace(/\bvoid 0\b/g, 'undefined'); | ||
// Remove sourcemap because it not exists in production | ||
content = content.replace(/\/\/# sourceMappingURL.*/g, ''); | ||
|
||
content = prettier.format(content, { | ||
...options!, | ||
parser: 'espree', | ||
printWidth: 120, | ||
}); | ||
|
||
content = `/*! WhatsApp Version: ${WA_VERSION} */\n` + content; | ||
|
||
fs.writeFileSync(filePath, content, { encoding: 'utf8' }); | ||
|
||
resolve?.(); | ||
}); | ||
|
||
await page.waitForFunction(() => window.WPP?.isReady, null, { | ||
timeout: 0, | ||
}); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 5000)); | ||
|
||
await Promise.all(downloads); | ||
|
||
await browser.close(); | ||
} | ||
start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/*! | ||
* Copyright 2021 WPPConnect Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as playwright from 'playwright-chromium'; | ||
|
||
import { preparePage, URL } from './browser'; | ||
|
||
async function start() { | ||
console.log('Connecting to Chrome'); | ||
const browser = await playwright.chromium.connectOverCDP( | ||
'http://localhost:9222' | ||
); | ||
console.log('Connected'); | ||
|
||
const context = browser.contexts()[0]; | ||
|
||
const page = context.pages().length | ||
? context.pages()[0] | ||
: await context.newPage(); | ||
|
||
console.log('Preparing page'); | ||
await preparePage(page); | ||
|
||
setTimeout(async () => { | ||
console.log('Opening WhatsApp WEB'); | ||
await page.goto(URL, { | ||
waitUntil: 'domcontentloaded', | ||
timeout: 120000, | ||
}); | ||
|
||
await page | ||
.waitForFunction( | ||
() => (window as any).Debug?.VERSION, | ||
{}, | ||
{ timeout: 120000 } | ||
) | ||
.catch(() => null); | ||
|
||
const version = await page | ||
.evaluate(() => (window as any).Debug.VERSION) | ||
.catch(() => null); | ||
|
||
console.log('WhatsApp Version: ', version); | ||
}, 1000); | ||
|
||
page.on('load', (page) => { | ||
const debug = process.env['DEBUG'] || '*'; | ||
|
||
page.evaluate((debug: string) => { | ||
localStorage.setItem('debug', debug); | ||
}, debug); | ||
}); | ||
} | ||
start(); |