Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improved module loader #27

Merged
merged 1 commit into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@types/ws": "^7.4.0",
"@typescript-eslint/eslint-plugin": "^4.15.2",
"@typescript-eslint/parser": "^4.15.2",
"@wppconnect-team/loader": "^1.0.0",
"commitizen": "^4.2.3",
"concurrently": "^5.3.0",
"conventional-changelog-cli": "^2.1.1",
Expand Down
1 change: 0 additions & 1 deletion src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class HostLayer {
this.log('error', 'Page Closed', { type: 'page' });
});
this.log('info', 'Initializing...');
this.initialize();
}

protected log(level: LogLevel, message: string, meta: object = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/process-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function processFiles(chat, blobs) {
chatParticipantCount: chat.getParticipantCount(),
});

await mediaCollection.processFiles(
await mediaCollection.processAttachments(
Debug.VERSION === '0.4.613'
? blobs
: blobs.map((blob) => {
Expand Down
135 changes: 0 additions & 135 deletions src/lib/wapi/store/get-store.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/lib/wapi/store/store-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export const storeObjects = [
conditions: (module) =>
module.default &&
module.default.prototype &&
(module.default.prototype.processFiles !== undefined ||
module.default.prototype.processAttachments !== undefined)
module.default.prototype.processAttachments !== undefined
? module.default
: null,
},
Expand Down
70 changes: 27 additions & 43 deletions src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,50 +143,36 @@ import {
_serializeRawObj,
_profilePicfunc,
} from './serializers';
import { getStore, getStore2 } from './store/get-store';
import { storeObjects } from './store/store-objects';

import * as WPPConnectLoader from '@wppconnect-team/loader';

window['webpackJsonp'] = window['webpackJsonp'] || [];
window['webpackChunkbuild'] = window['webpackChunkbuild'] || [];

if (typeof window.Store === 'undefined') {
window.Store = {};
var loadParasite = function () {
function injectParasite() {
const parasite = `parasite${Date.now()}`;
window['webpackJsonp'].push([
[parasite],
{
[parasite]: (x, y, z) => getStore(z),
},
[[parasite]],
]);
const parasite2 = `999999${Date.now()}`;
window['webpackChunkbuild'].push([[parasite2], {}, (z) => getStore2(z)]);
}

injectParasite();

setInterval(() => {
try {
const last = window['webpackJsonp'].length - 1;
if (!/^parasite/.test(window['webpackJsonp'][last][0][0])) {
injectParasite();
}
} catch (e) {}
try {
const last = window['webpackChunkbuild'].length - 1;
if (!/^999999/.test(window['webpackChunkbuild'][last][0][0])) {
injectParasite();
window.Store.promises = {};
const loader = new WPPConnectLoader();

for (const store of storeObjects) {
window.Store.promises[store.id] = loader
.waitForModule(store.conditions)
.then((m) => {
if (store.id === 'Store') {
window.Store = Object.assign({}, window.Store, m);
} else {
window.Store[store.id] = m;
}
} catch (e) {}
}, 1000);
};

if (document.readyState === 'complete') {
loadParasite();
} else {
window.addEventListener('load', () => loadParasite());
});
}

window.Store.sendMessage = function (e) {
return window.Store.SendTextMsgToChat(this, ...arguments);
};
window.Store.sendAddMessage = function (e) {
return window.Store.addAndSendMsgToChat(this, ...arguments);
};
}

if (typeof window.WAPI === 'undefined') {
Expand Down Expand Up @@ -538,17 +524,15 @@ if (typeof window.WAPI === 'undefined') {
const promises = missing.map((s) => {
if (!window.WAPI.storePromises[s]) {
window.WAPI.storePromises[s] = new Promise((resolve) => {
let time = null;
const listen = (e) => {
const name = (e && e.detail) || '';
if (name === s || !isUndefined(s)) {
window.removeEventListener('storeLoaded', listen);
clearInterval(time);
const storePromise =
window.Store.promises[s] || window.Store.promises['Store'];

storePromise.then(() => {
resolve(true);
}
});
};
window.addEventListener('storeLoaded', listen);
time = setInterval(listen, 1000);
});
}
return window.WAPI.storePromises[s];
Expand Down