From 1b1adf55e92593197bc15b8446a21ce0c4ef4836 Mon Sep 17 00:00:00 2001 From: Edgard Date: Fri, 2 Dec 2022 18:14:34 -0300 Subject: [PATCH] feat: Added conn.main_init event --- src/conn/events/eventTypes.ts | 11 ++++++++++ src/conn/events/index.ts | 1 + src/conn/events/registerMainInit.ts | 31 +++++++++++++++++++++++++++++ src/gtag/index.ts | 21 +++++++++++-------- 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 src/conn/events/registerMainInit.ts diff --git a/src/conn/events/eventTypes.ts b/src/conn/events/eventTypes.ts index c991dc046c..a347edc9e7 100644 --- a/src/conn/events/eventTypes.ts +++ b/src/conn/events/eventTypes.ts @@ -30,6 +30,17 @@ export interface ConnEventTypes { */ 'conn.authenticated': undefined; 'conn.logout': undefined; + /** + * Triggered when the interface is booting + * + * @example + * ```javascript + * WPP.on('conn.main_init', () => { + * // Your code + * }); + * ``` + */ + 'conn.main_init': undefined; /** * Triggered when the main interface is loaded, but is syncing * diff --git a/src/conn/events/index.ts b/src/conn/events/index.ts index 4e5c47c6ae..cb446832df 100644 --- a/src/conn/events/index.ts +++ b/src/conn/events/index.ts @@ -17,6 +17,7 @@ import './registerAuthCodeChangeEvent'; import './registerAuthenticatedEvent'; import './registerLogoutEvent'; +import './registerMainInit'; import './registerMainLoadedEvent'; import './registerMainReadyEvent'; import './registerNeedsUpdateEvent'; diff --git a/src/conn/events/registerMainInit.ts b/src/conn/events/registerMainInit.ts new file mode 100644 index 0000000000..1112980874 --- /dev/null +++ b/src/conn/events/registerMainInit.ts @@ -0,0 +1,31 @@ +/*! + * 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 { internalEv } from '../../eventEmitter'; +import * as webpack from '../../webpack'; + +webpack.onInjected(register); + +function register() { + const check = setInterval(() => { + const version = (window as any).Debug?.VERSION; + + if (version) { + clearInterval(check); + internalEv.emit('conn.main_init'); + } + }, 100); +} diff --git a/src/gtag/index.ts b/src/gtag/index.ts index 350d5d4563..a0327b49a0 100644 --- a/src/gtag/index.ts +++ b/src/gtag/index.ts @@ -41,10 +41,13 @@ internalEv.on('webpack.injected', () => { mainTracker.setUserProperty('method', method); mainTracker.setUserProperty('wa_js', waVersion); mainTracker.setUserProperty('powered_by', config.poweredBy || '-'); - mainTracker.setUserProperty( - 'whatsapp', - (window as any).Debug?.VERSION || '-' - ); + + internalEv.on('conn.main_init', () => { + mainTracker.setUserProperty( + 'whatsapp', + (window as any).Debug?.VERSION || '-' + ); + }); mainTracker.trackEvent('page_view', { authenticated, method }); @@ -52,10 +55,12 @@ internalEv.on('webpack.injected', () => { otherTracker.setUserProperty('method', method); otherTracker.setUserProperty('wa_js', waVersion); otherTracker.setUserProperty('powered_by', config.poweredBy || '-'); - otherTracker.setUserProperty( - 'whatsapp', - (window as any).Debug?.VERSION || '-' - ); + internalEv.on('conn.main_init', () => { + otherTracker.setUserProperty( + 'whatsapp', + (window as any).Debug?.VERSION || '-' + ); + }); if (typeof config.googleAnalyticsUserProperty === 'object') { for (const key in config.googleAnalyticsUserProperty) {