Skip to content

Commit

Permalink
feat: Added WhatsApp function to download and format source
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Aug 25, 2021
1 parent 225106e commit 1b3919f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"simple-import-sort/exports": "error",
"simple-import-sort/imports": "error"
},
"ignorePatterns": ["webpack.config.js", "dist/*"],
"ignorePatterns": ["webpack.config.js", "dist/*", "wa-source/*"],
"overrides": [
{
"files": ["src/whatsapp/**/*.ts"],
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/docs
/node_modules
/userDataDir
/wa-source
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
"docs:build": "npm run docs:clean && npm run docs:fix && typedoc",
"docs:clean": "shx rm -rf docs",
"docs:fix": "ts-node ./docs-source/layouts/fix-docs.ts",
"lint": "npx eslint --ext .ts src",
"launch:local": "ts-node ./src/tools/launchLocal",
"lint": "npx eslint --ext .ts src",
"prepare": "husky install && npm run clean && npm run build:prd",
"release": "release-it",
"wa-source:clean": "shx rm -rf wa-source",
"wa-source:download": "ts-node ./src/tools/extractWhatsappSource.ts",
"wa-source:format": "prettier --write ./wa-source",
"wa-source": "npm run wa-source:clean && npm run wa-source:download && npm run wa-source:format",
"watch": "webpack --watch --devtool inline-source-map --mode development"
},
"dependencies": {},
Expand Down
114 changes: 114 additions & 0 deletions src/tools/extractWhatsappSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*!
* 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 jsbeautify from 'js-beautify';
import * as path from 'path';
import * as playwright from 'playwright';
import * as prettier from 'prettier';

declare global {
interface Window {
WPP: any;
}
}

const URL = 'https://web.whatsapp.com/';

const WA_DIR = path.resolve(__dirname, '../../wa-source');

async function start() {
if (!fs.existsSync(WA_DIR)) {
fs.mkdirSync(WA_DIR);
}

const browser = await playwright.chromium.launchPersistentContext(
path.resolve(__dirname, '../../userDataDir')
);

const page = browser.pages().length
? browser.pages()[0]
: await browser.newPage();

page.route('**', (route, request) => {
if (request.url() === URL) {
return route.fulfill({
status: 200,
contentType: 'text/html',
body: waVersion.getPageContent(),
});
}

return route.continue();
});

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;
}
console.log('Downloading: ', fileName);

const body = await response.body();

fs.writeFileSync(filePath, body, { encoding: 'utf8' });
});

page.addInitScript(() => {
// Remove existent service worker
navigator.serviceWorker
.getRegistrations()
.then((registrations) => {
for (const registration of registrations) {
registration.unregister();
}
})
.catch(() => null);

// Disable service worker registration
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-empty-function
navigator.serviceWorker.register = new Promise(() => {});
});

page.on('domcontentloaded', async () => {
await page.addScriptTag({
path: path.resolve(__dirname, '../../dist/wppconnect-wa.js'),
});
});

await page.goto(URL, {
waitUntil: 'domcontentloaded',
});

await page.waitForFunction(() => window.WPP?.isReady, null, {
timeout: 0,
});

await browser.close();
}
start();

0 comments on commit 1b3919f

Please sign in to comment.