diff --git a/package-lock.json b/package-lock.json index 7da4ff7ce..5cd4da406 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1119,6 +1119,12 @@ "integrity": "sha512-k2p2VrONcYVX1wRRrf0f3X2VGltLWcv+JzXRBDmvCxGlCeESx4OXw91TsWeKOkp784uNoVQo313vxJFHXPPwfw==", "dev": true }, + "@wppconnect-team/loader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@wppconnect-team/loader/-/loader-1.0.0.tgz", + "integrity": "sha512-CsJdKoEno8obYgskfZu7xhj89mQdgl8CZsCQITkwN7PwedqFB9T69ikWihT7zfi8cBoYFbpiq3Cq+xBuu7N9YQ==", + "dev": true + }, "@xtuc/ieee754": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz", diff --git a/package.json b/package.json index 60da9b1db..d03d32c8f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/api/layers/host.layer.ts b/src/api/layers/host.layer.ts index 75a811341..fbfec2a77 100644 --- a/src/api/layers/host.layer.ts +++ b/src/api/layers/host.layer.ts @@ -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 = {}) { diff --git a/src/lib/wapi/functions/process-files.js b/src/lib/wapi/functions/process-files.js index 6c9ed6901..f01354ca9 100644 --- a/src/lib/wapi/functions/process-files.js +++ b/src/lib/wapi/functions/process-files.js @@ -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) => { diff --git a/src/lib/wapi/store/get-store.js b/src/lib/wapi/store/get-store.js deleted file mode 100644 index 4044ed285..000000000 --- a/src/lib/wapi/store/get-store.js +++ /dev/null @@ -1,135 +0,0 @@ -/* - * This file is part of WPPConnect. - * - * WPPConnect is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * WPPConnect is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with WPPConnect. If not, see . - */ - -import { storeObjects } from './store-objects'; - -export function getStore(modules) { - let foundCount = 0; - let neededObjects = storeObjects; - window.Store = window.Store || {}; - for (let idx in modules) { - if (typeof modules[idx] === 'object' && modules[idx] !== null) { - let first = Object.values(modules[idx])[0]; - if (typeof first === 'object' && first.exports) { - for (let idx2 in modules[idx]) { - let module = modules(idx2); - if (!module) { - continue; - } - neededObjects.forEach((needObj) => { - if (!needObj.conditions || needObj.foundedModule) return; - let neededModule = needObj.conditions(module); - if (neededModule !== null) { - foundCount++; - needObj.foundedModule = neededModule; - const event = new CustomEvent('storeLoaded', { - detail: needObj.id, - }); - if (needObj.id === 'Store') { - const oldStore = window.Store; - window.Store = needObj.foundedModule; - Object.assign(window.Store, oldStore); - } else { - window.Store[needObj.id] = needObj.foundedModule; - } - window.dispatchEvent(event); - - var index = neededObjects.indexOf(needObj); - if (index > -1) { - neededObjects.splice(index, 1); - } - } - }); - if (foundCount == neededObjects.length) { - break; - } - } - - window.Store.sendMessage = function (e) { - return window.Store.SendTextMsgToChat(this, ...arguments); - }; - window.Store.sendAddMessage = function (e) { - return window.Store.addAndSendMsgToChat(this, ...arguments); - }; - if (window.Store.MediaCollection) - window.Store.MediaCollection.prototype.processFiles = - window.Store.MediaCollection.prototype.processFiles || - window.Store.MediaCollection.prototype.processAttachments; - return window.Store; - } - } - } -} - -export function getStore2(modules) { - let foundCount = 0; - let neededObjects = storeObjects; - window.Store = window.Store || {}; - - Object.keys(modules.m).forEach(function (id) { - var module = modules(id); - if (!module) { - return; - } - - neededObjects.forEach((needObj) => { - if (!needObj.conditions || needObj.foundedModule) return; - let neededModule = null; - try { - neededModule = needObj.conditions(module); - } catch (error) { - console.log(error); - } - if (neededModule !== null) { - foundCount++; - needObj.foundedModule = neededModule; - const event = new CustomEvent('storeLoaded', { - detail: needObj.id, - }); - if (needObj.id === 'Store') { - const oldStore = window.Store; - window.Store = needObj.foundedModule; - Object.assign(window.Store, oldStore); - } else { - window.Store[needObj.id] = needObj.foundedModule; - } - window.dispatchEvent(event); - - var index = neededObjects.indexOf(needObj); - if (index > -1) { - neededObjects.splice(index, 1); - } - } - }); - }); - - if (foundCount == neededObjects.length) { - return window.Store; - } - - window.Store.sendMessage = function (e) { - return window.Store.SendTextMsgToChat(this, ...arguments); - }; - window.Store.sendAddMessage = function (e) { - return window.Store.addAndSendMsgToChat(this, ...arguments); - }; - if (window.Store.MediaCollection) - window.Store.MediaCollection.prototype.processFiles = - window.Store.MediaCollection.prototype.processFiles || - window.Store.MediaCollection.prototype.processAttachments; - return window.Store; -} diff --git a/src/lib/wapi/store/store-objects.js b/src/lib/wapi/store/store-objects.js index f8e914ea1..8a77fef8f 100644 --- a/src/lib/wapi/store/store-objects.js +++ b/src/lib/wapi/store/store-objects.js @@ -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, }, diff --git a/src/lib/wapi/wapi.js b/src/lib/wapi/wapi.js index 571068854..a373c6f59 100644 --- a/src/lib/wapi/wapi.js +++ b/src/lib/wapi/wapi.js @@ -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') { @@ -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];