25 lines
828 B
JavaScript
25 lines
828 B
JavaScript
const browser = require('../browser');
|
|
|
|
module.exports = async ({ url, waitUntil = 'domcontentloaded' }) => {
|
|
if (!url) throw new Error('URL é obrigatória');
|
|
await browser.start();
|
|
const p = await browser.ensurePage();
|
|
const currentUrl = p.url();
|
|
|
|
if (currentUrl && currentUrl !== 'about:blank') {
|
|
try {
|
|
const targetUrl = new URL(url);
|
|
if (currentUrl.includes(targetUrl.hostname)) {
|
|
const result = await browser.goto(url, { waitUntil });
|
|
return { success: true, ...result, tab: 'reused' };
|
|
}
|
|
} catch (e) {}
|
|
await browser.newTab(url);
|
|
const newPage = browser.page;
|
|
return { success: true, url, title: await newPage.title(), tab: 'new' };
|
|
}
|
|
|
|
const result = await browser.goto(url, { waitUntil });
|
|
return { success: true, ...result, tab: 'existing' };
|
|
};
|