@soymaycol/mayreplit
Version:
Mantén tu Replit activo 24/7 sin APIs - Creado por SoyMaycol
125 lines (103 loc) • 3.2 kB
JavaScript
import { chromium } from 'playwright-chromium'
import fs from 'fs/promises'
class MayReplit {
constructor(options = {}) {
this.replitUrl = options.replitUrl || null
this.cookiesPath = options.cookiesPath || './cookies.json'
this.screenshotInterval = options.screenshotInterval || 5 * 60 * 1000 // 5 minutos por defecto
this.headless = options.headless !== undefined ? options.headless : true
this.browser = null
this.context = null
this.page = null
this.screenshotBuffer = null
}
async readCookies() {
try {
const content = await fs.readFile(this.cookiesPath, 'utf8')
return JSON.parse(content)
} catch (error) {
console.error(`❌ Error leyendo cookies desde ${this.cookiesPath}:`, error.message)
throw error
}
}
async start() {
if (!this.replitUrl) {
throw new Error('❌ URL de Replit es requerida. Usa: mayReplit.setReplitUrl("tu-url-aqui")')
}
try {
console.log('🚀 Iniciando MayReplit...')
// Lanzar navegador
this.browser = await chromium.launch({ headless: this.headless })
this.context = await browser.newContext()
// Cargar cookies
const cookies = await this.readCookies()
await this.context.addCookies(cookies)
// Crear página y navegar
this.page = await this.context.newPage()
await this.page.goto(this.replitUrl, { waitUntil: 'networkidle' })
console.log('(◍•ᴗ•◍)❤ ¡Ya estamos dentro del Replit!')
// Tomar screenshot inicial
this.screenshotBuffer = await this.page.screenshot()
console.log('📸 Screenshot inicial capturada')
// Configurar intervalo de screenshots
this.setupScreenshotInterval()
// Mantener la pestaña abierta
await this.keepAlive()
} catch (error) {
console.error('❌ Error iniciando MayReplit:', error.message)
throw error
}
}
setupScreenshotInterval() {
setInterval(async () => {
try {
if (this.page) {
this.screenshotBuffer = await this.page.screenshot()
console.log('📸 Screenshot actualizada')
}
} catch (error) {
console.error('❌ Error actualizando screenshot:', error.message)
}
}, this.screenshotInterval)
}
async keepAlive() {
// Mantener la pestaña abierta indefinidamente
await new Promise(() => {})
}
setReplitUrl(url) {
this.replitUrl = url
return this
}
setCookiesPath(path) {
this.cookiesPath = path
return this
}
setScreenshotInterval(ms) {
this.screenshotInterval = ms
return this
}
setHeadless(headless) {
this.headless = headless
return this
}
getScreenshot() {
return this.screenshotBuffer
}
async stop() {
try {
if (this.page) {
await this.page.close()
}
if (this.context) {
await this.context.close()
}
if (this.browser) {
await this.browser.close()
}
console.log('🛑 MayReplit detenido correctamente')
} catch (error) {
console.error('❌ Error deteniendo MayReplit:', error.message)
}
}
}
export default MayReplit