|
| 1 | +import { ACCEPTED_WORDS } from "./constants.js"; |
| 2 | + |
| 3 | +export async function blockCookies(page) { |
| 4 | + await page.evaluate(async (ACCEPTED_WORDS) => { |
| 5 | + // try to press esc to close modals |
| 6 | + // Simulate pressing the Escape key to close modals |
| 7 | + document.dispatchEvent( |
| 8 | + new KeyboardEvent("keydown", { |
| 9 | + key: "Escape", |
| 10 | + code: "Escape", |
| 11 | + which: 27, |
| 12 | + keyCode: 27, |
| 13 | + bubbles: true, |
| 14 | + cancelable: true, |
| 15 | + }) |
| 16 | + ); |
| 17 | + |
| 18 | + // Wait a short time for any potential animations to complete |
| 19 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 20 | + |
| 21 | + const selectors = [ |
| 22 | + // Priority selectors for buttons in modals |
| 23 | + '.modal button, .modal input[type="button"], .modal input[type="submit"]', |
| 24 | + '.modal [role="button"]', |
| 25 | + // Close buttons |
| 26 | + 'button[class*="close"], button[aria-label="Close"], button.close', |
| 27 | + // Overlay buttons |
| 28 | + ".modal-backdrop button, .modal-overlay button, .overlay button", |
| 29 | + "button", |
| 30 | + "div", |
| 31 | + "span", |
| 32 | + ]; |
| 33 | + |
| 34 | + const flatAcceptWords = Object.values(ACCEPTED_WORDS).flat(); |
| 35 | + |
| 36 | + selectors.forEach((selector) => { |
| 37 | + const elements = document.querySelectorAll(selector); |
| 38 | + for (let i = elements.length - 1; i >= 0; i--) { |
| 39 | + const el = elements[i]; |
| 40 | + if (el.tagName.toLowerCase() === "a") continue; // Skip links |
| 41 | + const text = el.innerText.toLowerCase(); |
| 42 | + if (flatAcceptWords.some((word) => text.includes(word.toLowerCase()))) { |
| 43 | + console.log("Found accept button:", el); |
| 44 | + el.click(); |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + }); |
| 49 | + }, ACCEPTED_WORDS); |
| 50 | + |
| 51 | + // Wait for any animations to complete |
| 52 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 53 | +} |
0 commit comments