import path from 'path'; import { exec } from 'child_process'; import puppeteer from 'puppeteer-core'; import fetch from 'node-fetch'; import { createWriteStream } from 'fs'; import fs from 'fs'; import { app, BrowserWindow } from 'electron'; import pie from 'puppeteer-in-electron'; // Configuración const CONFIG = { URL: 'https://rpa.mind.brm.co/app', APP_CODE: 'SHP030', URL_CONFIRM: 'https://cs.shopee.com.mx/portal/inhouse/' }; // Initialize puppeteer with electron before app is ready (async () => { await pie.initialize(app); })(); async function queryEndpoint(user) { try { const response = await fetch(CONFIG.URL, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ user: user, code: CONFIG.APP_CODE }) }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return data; } catch (error) { console.error('Error querying endpoint:', error); throw error; } } // Remove the initialize function since we're initializing at startup // and modify fillForm to remove the initialize call async function fillForm(url_app, user, password, xpath_user, xpath_pass) { try { // Remove the initialize() call from here // Create Electron window const window = new BrowserWindow({ width: 1200, height: 800, webPreferences: { nodeIntegration: true, contextIsolation: false } }); // Get puppeteer browser and page const browser = await pie.connect(app, puppeteer); const page = await pie.getPage(browser, window); // Navigate to the form page console.log('Abriendo plataforma'); await page.goto(url_app); console.log('Plataforma abierta'); console.log('Esperando boton'); await page.waitForSelector('.login-button'); await page.click('.login-button'); console.log('Boton encontrado'); try { console.log('Verificando si ya hay una cuenta'); const account = await page.waitForSelector('::-p-xpath(//h1[contains(text(), "Choose an account")])'); console.log('Si hay una cuenta'); const account_active = await page.waitForSelector('::-p-xpath(//span[contains(text(), "'+user+'")])'); account_active.click(); console.log('Click en cuenta activa'); await new Promise(resolve => setTimeout(resolve, 5000)); try { console.log('Esperando boton continue'); const input_continue_top = await page.waitForSelector('::-p-xpath(//button[contains(text(), "Allow")])'); await input_continue_top.click(); console.log('Boton continue clickado'); } catch (error) { console.error('Error al encontrar el botón "Continue"'); } } catch (error) { console.log('No hay una cuenta'); } let startTime_page = Date.now(); let isLoggedIn_page = false; while (Date.now() - startTime_page < 10000) { // 10 seconds timeout const url_actual_page = page.url(); console.log(url_actual_page); if (url_actual_page.includes(CONFIG.URL_CONFIRM)) { console.log('Ya se encuentra en la pagina de inicio'); console.log('Logueado'); isLoggedIn_page = true; break; } await new Promise(resolve => setTimeout(resolve, 500)); // Check every 500ms } if (!isLoggedIn_page) { console.log('No logueado'); } if (isLoggedIn_page !== true) { console.log('Esperando input user'); const input_user = await page.waitForSelector('::-p-xpath(' + xpath_user + ')'); await input_user.type(user); await input_user.press('Enter'); console.log('Input user diligenciado'); await new Promise(resolve => setTimeout(resolve, 5000)); console.log('Esperando input password'); const input_pass = await page.waitForSelector('::-p-xpath(' + xpath_pass + ')'); await input_pass.type(password); await input_pass.press('Enter'); console.log('Input password diligenciado'); await new Promise(resolve => setTimeout(resolve, 5000)); try { console.log('Esperando boton continue'); const input_continue = await page.waitForSelector('::-p-xpath(//button[contains(text(), "Allow")])'); await input_continue.click(); console.log('Boton continue clickado'); } catch (error) { console.error('Error al encontrar el botón "Continue"'); } await new Promise(resolve => setTimeout(resolve, 5000)); let startTime = Date.now(); let isLoggedIn = false; while (Date.now() - startTime < 10000) { // 10 seconds timeout const url_actual = page.url(); console.log(url_actual); if (url_actual.includes(CONFIG.URL_CONFIRM)) { console.log('Ya se encuentra en la pagina de inicio'); console.log('Logueado'); isLoggedIn = true; break; } await new Promise(resolve => setTimeout(resolve, 500)); // Check every 500ms } if (!isLoggedIn) { console.log('No logueado'); } } } catch (error) { console.error('Error filling form:', error); throw error; } } // Modified main process app.on('ready', async () => { console.log('Aplicacion lista'); try { const data_user = await queryEndpoint('45957768'); console.log(data_user); if (data_user.App) { console.log('Se obtuvo la info del usuario'); await fillForm(data_user.App, data_user.User, data_user.Password, data_user.Xpath_user, data_user.Xpath_pass); } else { console.log('No se pudo obtener la info del usuario'); } } catch (error) { console.error('Error in main process:', error); } }); // Prevent app from closing when all windows are closed app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } });