@duckduckgo/autoconsent 14.71.0 → 14.72.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/puppeteer.md CHANGED
@@ -8,27 +8,50 @@ Autoconsent can be used with [Puppeteer](https://pptr.dev) via the bundled `dist
8
8
  Puppeteer's `page.exposeFunction` and `page.evaluateOnNewDocument` bridge the gap.
9
9
 
10
10
  ```javascript
11
- const puppeteer = require('puppeteer')
12
- const path = require('path')
13
- const fs = require('fs')
11
+ import puppeteer from 'puppeteer'
12
+ import fs from 'fs'
13
+ import { dirname, resolve } from 'path'
14
+ import { fileURLToPath } from 'url'
14
15
 
15
16
  const autoconsentScript = fs.readFileSync(
16
- path.resolve(
17
- path.dirname(require.resolve('@duckduckgo/autoconsent')),
17
+ resolve(
18
+ dirname(fileURLToPath(import.meta.resolve('@duckduckgo/autoconsent'))),
18
19
  'autoconsent.playwright.js'
19
20
  ),
20
21
  'utf8'
21
22
  )
22
23
 
23
- // See https://github.com/duckduckgo/autoconsent/blob/main/api.md
24
+ // Load the rule bundles shipped with the package
25
+ const rules = JSON.parse(
26
+ fs.readFileSync(
27
+ fileURLToPath(import.meta.resolve('@duckduckgo/autoconsent/rules/rules.json')),
28
+ 'utf8'
29
+ )
30
+ )
31
+
32
+ // See https://github.com/duckduckgo/autoconsent/blob/main/docs/api.md
24
33
  const autoconsentConfig = {
25
34
  enabled: true,
26
35
  autoAction: 'optOut',
36
+ disabledCmps: [],
27
37
  enablePrehide: true,
28
38
  enableCosmeticRules: true,
29
39
  enableGeneratedRules: true,
30
- enableHeuristicAction: true,
31
40
  detectRetries: 20,
41
+ isMainWorld: false,
42
+ prehideTimeout: 2000,
43
+ enableFilterList: false,
44
+ enableHeuristicDetection: true,
45
+ enableHeuristicAction: true,
46
+ logs: {
47
+ lifecycle: false,
48
+ rulesteps: false,
49
+ detectionsteps: false,
50
+ evals: false,
51
+ errors: true,
52
+ messages: false,
53
+ waits: false,
54
+ },
32
55
  }
33
56
 
34
57
  const sendMessage = (page, message) =>
@@ -44,13 +67,39 @@ async function setupAutoconsent(page) {
44
67
  await page.exposeFunction('autoconsentSendMessage', async message => {
45
68
  if (!message || typeof message !== 'object') return
46
69
 
47
- if (message.type === 'init') {
48
- return sendMessage(page, { type: 'initResp', config: autoconsentConfig })
49
- }
70
+ switch (message.type) {
71
+ case 'init':
72
+ return sendMessage(page, {
73
+ type: 'initResp',
74
+ config: autoconsentConfig,
75
+ rules, // must include rules or no CMPs will be detected
76
+ })
77
+
78
+ case 'eval': {
79
+ const result = await page.evaluate(message.code)
80
+ return sendMessage(page, {
81
+ type: 'evalResp',
82
+ id: message.id,
83
+ result,
84
+ })
85
+ }
50
86
 
51
- if (message.type === 'eval') {
52
- const result = await page.evaluate(message.code)
53
- return sendMessage(page, { type: 'evalResp', id: message.id, result })
87
+ // informational messages log or act on as needed
88
+ case 'cmpDetected':
89
+ console.log(`CMP detected: ${message.cmp}`)
90
+ break
91
+ case 'popupFound':
92
+ console.log(`Popup found: ${message.cmp}`)
93
+ break
94
+ case 'optOutResult':
95
+ console.log(`Opt-out result: ${message.result}`)
96
+ break
97
+ case 'autoconsentDone':
98
+ console.log(`Autoconsent done: ${message.cmp}`)
99
+ break
100
+ case 'autoconsentError':
101
+ console.error(`Autoconsent error:`, message.details)
102
+ break
54
103
  }
55
104
  })
56
105
 
@@ -77,6 +126,31 @@ async function setupAutoconsent(page) {
77
126
 
78
127
  1. **`page.exposeFunction`** binds `window.autoconsentSendMessage` in the page, giving the content script a way to call back into Node.
79
128
  2. **`page.evaluateOnNewDocument`** injects the content script so it runs before any page JavaScript, enabling early CMP detection and prehiding.
80
- 3. When the content script starts, it sends an `init` message. The Node-side handler replies with `initResp` containing the config, which triggers CMP detection and automatic opt-out.
81
- 4. `eval` messages are handled by running `page.evaluate(message.code)` in the main world, matching how the [Playwright test runner](./playwright/runner.ts) handles them.
129
+ 3. When the content script starts, it sends an `init` message. The Node-side handler replies with `initResp` containing both the config **and the rule bundles**, which triggers CMP detection and automatic opt-out.
130
+ 4. `eval` messages are handled by running `page.evaluate(message.code)` in the main world, matching how the [Playwright test runner](../playwright/runner.ts) handles them.
82
131
  5. A post-navigation `page.evaluate(autoconsentScript)` re-triggers the script on the current document, covering cases where the page loaded before the `evaluateOnNewDocument` hook was set up.
132
+
133
+ ## Config reference
134
+
135
+ See the full `Config` type in [`lib/types.ts`](../lib/types.ts) and the [API documentation](./api.md) for details on all available options.
136
+
137
+ Key options:
138
+
139
+ | Option | Type | Default | Description |
140
+ |--------|------|---------|-------------|
141
+ | `enabled` | boolean | — | Master switch for autoconsent |
142
+ | `autoAction` | `'optOut' \| 'optIn' \| null` | — | Action to perform automatically when a popup is found |
143
+ | `enablePrehide` | boolean | — | Inject CSS early to prevent popup flicker |
144
+ | `enableCosmeticRules` | boolean | — | Enable rules that hide popups via CSS |
145
+ | `enableGeneratedRules` | boolean | — | Include auto-generated rules |
146
+ | `detectRetries` | number | — | How many times to retry CMP detection |
147
+ | `enableFilterList` | boolean | — | Enable ABP/uBO cosmetic filter support (requires the `/extra` build) |
148
+
149
+ ## Rules
150
+
151
+ The `initResp` message **must** include a `rules` object. Without rules, no CMPs will be detected. The package ships pre-built rule bundles:
152
+
153
+ - `@duckduckgo/autoconsent/rules/rules.json` — full rules (includes `autoconsent` and `consentomatic` arrays)
154
+ - `@duckduckgo/autoconsent/rules/compact-rules.json` — compact encoding for opt-out only (smaller payload)
155
+
156
+ Load either file and pass it as the `rules` field in the `initResp` message.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duckduckgo/autoconsent",
3
- "version": "14.71.0",
3
+ "version": "14.72.0",
4
4
  "description": "",
5
5
  "types": "./dist/types/web.d.ts",
6
6
  "exports": {
@@ -23,7 +23,9 @@
23
23
  ],
24
24
  "optOut": [
25
25
  {
26
- "waitForThenClick": "#s-rall-bn"
26
+ "if": { "exists": "#s-rall-bn" },
27
+ "then": [{ "waitForThenClick": "#s-rall-bn" }],
28
+ "else": [{ "waitForThenClick": "#c-s-bn" }, { "waitForThenClick": "#s-sv-bn" }]
27
29
  }
28
30
  ],
29
31
  "test": [