UNPKG

@knowcode/screenshotfetch

Version:

Web application spider with screenshot capture and customer journey documentation. Automate user flow documentation with authentication support.

214 lines (190 loc) 6.08 kB
class CookieHandler { constructor() { this.commonSelectors = [ // Most common patterns 'button[onclick*="accept"]', 'button[class*="accept-all"]', 'button[id="onetrust-accept-btn-handler"]', 'button[class="cc-btn cc-dismiss"]', 'button[data-action="accept"]', 'a[class*="cc-btn cc-dismiss"]', // Platform specific 'button[class*="cookie-consent-accept"]', 'button[class*="cookie-accept"]', 'button[class*="cookiebot-accept"]', // Generic patterns '[aria-label*="accept" i]', '[aria-label*="agree" i]', '[aria-label*="consent" i]', 'button[class*="accept" i]', 'button[class*="agree" i]', 'button[class*="allow" i]', // Data attributes '[data-testid*="accept" i]', '[data-action*="accept" i]', '[data-cookie-consent="accept"]' ]; this.bannerSelectors = [ // Generic cookie banners '.cookie-banner', '.cookie-consent', '.cookie-notice', '.cookie-popup', '.gdpr-banner', '.gdpr-consent', '.privacy-banner', '.privacy-popup', '#cookie-banner', '#cookie-consent', '#cookie-notice', '#gdpr-banner', '[class*="cookieconsent"]', '[id*="cookieconsent"]', '[class*="cookie-banner"]', '[class*="cookie-consent"]', '[class*="cookie-notice"]', '[class*="gdpr"]', '[class*="privacy-banner"]', // Specific services '.cc-banner', '.cc-window', '#cookieConsent', '.cookieConsent', '.onetrust-pc-dark-filter', '#onetrust-banner-sdk', '.optanon-alert-box-wrapper', '.trustarc-banner', '.quantcast-banner', '.cookiebot-banner', '.termly-consent-banner', '.iubenda-cs-container' ]; this.blockedDomains = [ 'cookiebot.com', 'cookieconsent.com', 'cookieyes.com', 'onetrust.com', 'trustarc.com', 'quantcast.com', 'consensu.org', 'privacy-center.org', 'cookiepro.com', 'termly.io', 'iubenda.com', 'osano.com', 'usercentrics.com' ]; } async dismissCookieConsent(page) { // Try clicking accept buttons for (const selector of this.commonSelectors) { try { // Wait for selector with short timeout await page.waitForSelector(selector, { timeout: 3000, visible: true }); // Click the button await page.click(selector); console.log(`✅ Clicked cookie consent: ${selector}`); // Wait for animation/removal await new Promise(resolve => setTimeout(resolve, 1500)); return true; } catch (e) { // Try next selector } } // Try clicking by text content try { const clicked = await page.evaluate(() => { const buttons = Array.from(document.querySelectorAll('button, a')); const acceptButton = buttons.find(btn => { const text = btn.textContent.toLowerCase().trim(); return (text.includes('accept') || text.includes('agree') || text.includes('allow') || text.includes('got it') || text.includes('ok')) && text.length < 30; }); if (acceptButton) { acceptButton.click(); return true; } return false; }); if (clicked) { console.log('✅ Clicked cookie consent by text content'); await new Promise(resolve => setTimeout(resolve, 1500)); return true; } } catch (e) { // Continue } return false; } async removeCookieBanners(page) { // CSS injection approach - more reliable await page.addStyleTag({ content: ` ${this.bannerSelectors.join(', ')} { display: none !important; visibility: hidden !important; opacity: 0 !important; pointer-events: none !important; height: 0 !important; width: 0 !important; overflow: hidden !important; } ` }); // JavaScript removal approach await page.evaluate((selectors) => { selectors.forEach(selector => { document.querySelectorAll(selector).forEach(el => { el.style.display = 'none'; el.remove(); }); }); // Remove fixed/sticky elements that might be cookie banners document.querySelectorAll('*').forEach(el => { const style = window.getComputedStyle(el); const text = el.textContent.toLowerCase(); if ((style.position === 'fixed' || style.position === 'sticky') && (text.includes('cookie') || text.includes('privacy') || text.includes('consent') || text.includes('gdpr')) && text.length < 1000) { el.style.display = 'none'; el.remove(); } }); }, this.bannerSelectors); console.log('✅ Removed cookie banners via CSS and JavaScript'); } async blockCookieServices(page) { await page.setRequestInterception(true); const blockedPatterns = this.blockedDomains.map(domain => new RegExp(domain.replace('.', '\\.'))); page.on('request', (request) => { const url = request.url(); if (blockedPatterns.some(pattern => pattern.test(url))) { console.log(`🚫 Blocked cookie service: ${url.substring(0, 50)}...`); request.abort(); } else { request.continue(); } }); console.log('✅ Cookie consent services blocking enabled'); } // Add custom selectors for specific sites addCustomSelectors(selectors) { this.commonSelectors.push(...selectors); } // Add custom banner selectors addCustomBannerSelectors(selectors) { this.bannerSelectors.push(...selectors); } // Add domains to block addBlockedDomains(domains) { this.blockedDomains.push(...domains); } } module.exports = CookieHandler;