playwright-autoconsent
Version:
Easy integration of @duckduckgo/autoconsent with Playwright for automated cookie consent handling
90 lines (81 loc) • 2.52 kB
TypeScript
import { Page } from 'playwright';
export interface ConsentResult {
/** Whether a CMP was detected and handled */
handled: boolean;
/** Name of the detected CMP (e.g., 'google.com', 'cookiebot') */
cmp?: string;
/** Whether the consent action succeeded */
success?: boolean;
/** All messages received from autoconsent */
messages: Array<any>;
}
export interface ConsentOptions {
/** Consent action to take - 'optIn' accepts all, 'optOut' rejects all, null does nothing (default: 'optOut') */
action?: 'optIn' | 'optOut' | null;
/** Enable cosmetic (CSS-only) rules to hide popups (default: true) */
enableCosmeticRules?: boolean;
/** Timeout in milliseconds to wait for consent handling (default: 5000) */
timeout?: number;
/** Enable debug logging (default: false) */
debug?: boolean;
}
export interface NavigateOptions extends ConsentOptions {
/** Navigation wait condition (default: 'commit') */
waitUntil?: 'load' | 'domcontentloaded' | 'networkidle' | 'commit';
}
/**
* Handle cookie consent on a Playwright page
*
* @example
* ```javascript
* import { handleCookieConsent } from 'playwright-autoconsent';
*
* await page.goto('https://example.com');
* const result = await handleCookieConsent(page, { action: 'optOut' });
* console.log('Consent handled:', result.handled);
* ```
*/
export function handleCookieConsent(
page: Page,
options?: ConsentOptions
): Promise<ConsentResult>;
/**
* Navigate to a URL and automatically handle cookie consent
*
* @example
* ```javascript
* import { navigateAndHandleConsent } from 'playwright-autoconsent';
*
* const result = await navigateAndHandleConsent(page, 'https://example.com', {
* action: 'optOut',
* timeout: 5000
* });
* ```
*/
export function navigateAndHandleConsent(
page: Page,
url: string,
options?: NavigateOptions
): Promise<ConsentResult>;
/**
* Initialize autoconsent on a page that's already loaded
* Useful for SPAs or when you need to handle consent after page interactions
*
* @example
* ```javascript
* import { initAutoconsent } from 'playwright-autoconsent';
*
* // After some page interaction
* const result = await initAutoconsent(page);
* ```
*/
export function initAutoconsent(
page: Page,
options?: ConsentOptions
): Promise<ConsentResult>;
declare const _default: {
handleCookieConsent: typeof handleCookieConsent;
navigateAndHandleConsent: typeof navigateAndHandleConsent;
initAutoconsent: typeof initAutoconsent;
};
export default _default;