More stability & defaults
This commit is contained in:
76
src/pageLoader.js
Normal file
76
src/pageLoader.js
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Setup and configure a Puppeteer page
|
||||
* @param {Browser} browser - Puppeteer browser instance
|
||||
* @param {Object} options - Configuration options
|
||||
* @param {number} options.width - Page width
|
||||
* @param {number} options.height - Page height
|
||||
* @returns {Promise<Page>} Configured Puppeteer page
|
||||
*/
|
||||
async function setupPage(browser, { width, height }) {
|
||||
const page = await browser.newPage();
|
||||
|
||||
// Reduce memory usage by disabling caching
|
||||
await page.setCacheEnabled(false);
|
||||
|
||||
// Inject CSS early to prevent white flash during page load
|
||||
await page.evaluateOnNewDocument(() => {
|
||||
const style = document.createElement('style');
|
||||
style.textContent = `
|
||||
html, body {
|
||||
background-color: #000 !important;
|
||||
}
|
||||
`;
|
||||
document.head?.appendChild(style) || document.documentElement.appendChild(style);
|
||||
});
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for page to be fully loaded with stylesheet
|
||||
* @param {Page} page - Puppeteer page
|
||||
* @param {string} url - URL to navigate to
|
||||
* @returns {Promise<boolean>} True if page loaded successfully
|
||||
*/
|
||||
async function waitForPageFullyLoaded(page, url) {
|
||||
try {
|
||||
// Wait for DOM content and stylesheet to load
|
||||
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 });
|
||||
console.log('Page DOM loaded, waiting for stylesheet...');
|
||||
|
||||
// Wait a brief moment for stylesheet to apply
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
console.log('Page stylesheet loaded, switching to live frames');
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error('Page load error:', err.message);
|
||||
// Still show the page even if timeout occurs
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide logo elements on the page
|
||||
* @param {Page} page - Puppeteer page
|
||||
*/
|
||||
async function hideLogo(page) {
|
||||
try {
|
||||
await page.evaluate(() => {
|
||||
const images = document.querySelectorAll('img');
|
||||
images.forEach(img => {
|
||||
if (img.src && img.src.includes('Logo3.png')) {
|
||||
img.style.display = 'none';
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Logo hide error:', err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setupPage,
|
||||
waitForPageFullyLoaded,
|
||||
hideLogo
|
||||
};
|
||||
Reference in New Issue
Block a user