diff --git a/web-interface/server.js b/web-interface/server.js index fc6b51f..567706e 100644 --- a/web-interface/server.js +++ b/web-interface/server.js @@ -34,7 +34,7 @@ app.use(express.static(path.join(__dirname, 'public'), { } })); -// Middleware para injetar fix de roteador em sites clonados (SPAs como React, Vue, Angular, Vite) +// Middleware para injetar fix de roteador em sites clonados (SPAs como React, Vue, Angular, Vite) e reescrever JS app.use((req, res, next) => { if (!req.path.startsWith('/api') && req.path !== '/' && req.path !== '/old') { let urlPath = req.path; @@ -48,6 +48,7 @@ app.use((req, res, next) => { urlPath += 'index.html'; } + // Interceptação de arquivos HTML (Camada 1 - DOM Mock Universal) if (urlPath.endsWith('.html')) { const filePath = path.join(__dirname, '..', 'cloned-sites', urlPath); if (fs.existsSync(filePath)) { @@ -58,32 +59,50 @@ app.use((req, res, next) => { @@ -115,6 +134,29 @@ app.use((req, res, next) => { } } } + + // Interceptação de arquivos JS de sites clonados (Camada 2 - JS Rewriter Supremo) + if (urlPath.endsWith('.js') && (urlPath.includes('_202') || urlPath.includes('/cloned-sites/'))) { + const filePath = path.join(__dirname, '..', 'cloned-sites', urlPath); + if (fs.existsSync(filePath)) { + try { + let jsContent = fs.readFileSync(filePath, 'utf8'); + + // Substitui chamadas diretas a window.location.pathname e location.pathname + jsContent = jsContent.replace(/window\.location\.pathname/g, '(window.__mockLocationPathname || window.location.pathname)'); + jsContent = jsContent.replace(/location\.pathname/g, '(window.__mockLocationPathname || window.location.pathname)'); + + // Substitui chamadas diretas a window.location.href e location.href + jsContent = jsContent.replace(/window\.location\.href/g, '(window.__mockLocationHref || window.location.href)'); + jsContent = jsContent.replace(/location\.href/g, '(window.__mockLocationHref || window.location.href)'); + + res.setHeader('Content-Type', 'application/javascript'); + return res.send(jsContent); + } catch (err) { + console.error('Erro ao reescrever JS do clone:', err); + } + } + } } next(); });