barebrowse 0.14.0 → 0.17.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/CHANGELOG.md +157 -0
- package/README.md +33 -1
- package/barebrowse.context.md +5 -2
- package/cli.js +10 -2
- package/mcp-server.js +14 -2
- package/package.json +1 -1
- package/src/auth.js +27 -13
- package/src/ax-snapshot.js +287 -0
- package/src/bidi.js +143 -0
- package/src/consent-firefox.js +125 -0
- package/src/consent-patterns.js +154 -0
- package/src/consent.js +5 -144
- package/src/daemon.js +162 -51
- package/src/firefox-page.js +445 -0
- package/src/firefox.js +203 -0
- package/src/index.js +88 -6
- package/src/network-idle.js +66 -28
- package/src/readable.js +32 -20
- package/src/stealth-firefox.js +42 -0
- package/src/stealth.js +107 -69
- package/types/auth.d.ts +18 -1
- package/types/ax-snapshot.d.ts +35 -0
- package/types/bidi.d.ts +10 -0
- package/types/consent-firefox.d.ts +9 -0
- package/types/consent-patterns.d.ts +12 -0
- package/types/daemon.d.ts +34 -0
- package/types/firefox-page.d.ts +21 -0
- package/types/firefox.d.ts +41 -0
- package/types/index.d.ts +13 -2
- package/types/network-idle.d.ts +16 -8
- package/types/readable.d.ts +8 -0
- package/types/stealth-firefox.d.ts +9 -0
- package/types/stealth.d.ts +31 -0
package/types/bidi.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a BiDi client connected to the given /session WebSocket URL and open
|
|
3
|
+
* a session. Firefox prints its BiDi endpoint to stderr as
|
|
4
|
+
* "WebDriver BiDi listening on ws://HOST:PORT"; the direct-connection socket
|
|
5
|
+
* (no WebDriver-classic handshake) lives at that URL + "/session".
|
|
6
|
+
*
|
|
7
|
+
* @param {string} wsUrl - BiDi session WebSocket URL (ws://HOST:PORT/session)
|
|
8
|
+
* @returns {Promise<object>} BiDi client ({ send, evaluate, on, once, subscribe, sessionId, close })
|
|
9
|
+
*/
|
|
10
|
+
export function createBiDi(wsUrl: string): Promise<object>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Try to dismiss a cookie consent dialog in a reconstructed Firefox AX tree.
|
|
3
|
+
*
|
|
4
|
+
* @param {object} root - Spliced AX tree root (from firefox-page's buildTree).
|
|
5
|
+
* @param {(ref: string) => Promise<void>} click - Clicks the element for a ref
|
|
6
|
+
* (firefox-page injects one that routes to pointerClick / performActions).
|
|
7
|
+
* @returns {Promise<boolean>} true if an accept button was found and clicked.
|
|
8
|
+
*/
|
|
9
|
+
export function dismissConsentFirefox(root: object, click: (ref: string) => Promise<void>): Promise<boolean>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* consent-patterns.js — Shared cookie-consent detection data.
|
|
3
|
+
*
|
|
4
|
+
* The engine-agnostic half of consent handling: the multilingual regex sets
|
|
5
|
+
* and dialog roles used to recognise a consent dialog and its "accept" button.
|
|
6
|
+
* Both the CDP walker (consent.js) and the BiDi walker (consent-firefox.js)
|
|
7
|
+
* import these so the language coverage is single-sourced — a new language or
|
|
8
|
+
* pattern is added once and both engines get it.
|
|
9
|
+
*/
|
|
10
|
+
export const ACCEPT_PATTERNS: RegExp[];
|
|
11
|
+
export const DIALOG_ROLES: Set<string>;
|
|
12
|
+
export const CONSENT_DIALOG_HINTS: RegExp[];
|
package/types/daemon.d.ts
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build the argv for the detached daemon child. Pure + exported so the
|
|
3
|
+
* flag-forwarding contract is unit-testable: a forward that's silently
|
|
4
|
+
* dropped here (as `--incognito` was in v0.15.0, turning `open --incognito`
|
|
5
|
+
* into a fully-authenticated session) becomes a failing test, not a leak.
|
|
6
|
+
* @param {object} opts - the session options parsed in cli.js cmdOpen
|
|
7
|
+
* @param {string} absDir - resolved output directory
|
|
8
|
+
* @param {string|undefined} initialUrl - URL to navigate on start (may be undefined)
|
|
9
|
+
* @param {string} cliPath - path to cli.js for the child to run
|
|
10
|
+
* @returns {string[]}
|
|
11
|
+
*/
|
|
12
|
+
export function buildDaemonArgs(opts: object, absDir: string, initialUrl: string | undefined, cliPath: string): string[];
|
|
13
|
+
/**
|
|
14
|
+
* Wire Firefox/BiDi console + network capture onto the daemon's log arrays.
|
|
15
|
+
* The BiDi analogue of the CDP `Runtime.consoleAPICalled` / `Network.*`
|
|
16
|
+
* capture — measured event shapes: `log.entryAdded` carries {method, level,
|
|
17
|
+
* args:[{type,value}], text}; the network events key in-flight requests by
|
|
18
|
+
* `request.request` and expose `response.{status,statusText,mimeType}` /
|
|
19
|
+
* top-level `errorText`. Pure + exported so the shape mapping (warn→warning
|
|
20
|
+
* normalization, arg extraction, orphan-safe response/error pairing) is
|
|
21
|
+
* unit-testable against a fake bidi without launching Firefox.
|
|
22
|
+
*
|
|
23
|
+
* @param {object} bidi - BiDi client (async .subscribe(), .on(method, cb))
|
|
24
|
+
* @param {object} sinks
|
|
25
|
+
* @param {Array} sinks.consoleLogs - push-target for console entries
|
|
26
|
+
* @param {Array} sinks.networkLogs - push-target for completed/failed requests
|
|
27
|
+
* @param {Map} sinks.pendingRequests - in-flight request bookkeeping
|
|
28
|
+
* @returns {Promise<void>}
|
|
29
|
+
*/
|
|
30
|
+
export function attachBiDiCapture(bidi: object, { consoleLogs, networkLogs, pendingRequests }: {
|
|
31
|
+
consoleLogs: any[];
|
|
32
|
+
networkLogs: any[];
|
|
33
|
+
pendingRequests: Map<any, any>;
|
|
34
|
+
}): Promise<void>;
|
|
1
35
|
/**
|
|
2
36
|
* Spawn a detached child process that runs the daemon.
|
|
3
37
|
* Parent polls for session.json, then exits.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build a Firefox/BiDi-backed page object.
|
|
3
|
+
* @param {object} bidi - BiDi client from createBiDi()
|
|
4
|
+
* @param {object} [opts]
|
|
5
|
+
* @param {'act'|'browse'|'navigate'|'full'|'read'} [opts.pruneMode='act'] - Default prune mode.
|
|
6
|
+
* @param {{allowLocalUrls?: boolean, blockPrivateNetwork?: boolean}} [opts.urlGuard] - Navigation safety policy, applied on every goto().
|
|
7
|
+
* @param {string} [opts.uploadDir] - When set, upload() rejects files outside this dir.
|
|
8
|
+
* @param {boolean} [opts.incognito=false] - Clean session: injectCookies() is a no-op.
|
|
9
|
+
* @param {boolean} [opts.consent=true] - Auto-dismiss cookie consent dialogs after goto().
|
|
10
|
+
* @returns {Promise<object>} page object
|
|
11
|
+
*/
|
|
12
|
+
export function createFirefoxPage(bidi: object, opts?: {
|
|
13
|
+
pruneMode?: "act" | "browse" | "navigate" | "full" | "read" | undefined;
|
|
14
|
+
urlGuard?: {
|
|
15
|
+
allowLocalUrls?: boolean;
|
|
16
|
+
blockPrivateNetwork?: boolean;
|
|
17
|
+
} | undefined;
|
|
18
|
+
uploadDir?: string | undefined;
|
|
19
|
+
incognito?: boolean | undefined;
|
|
20
|
+
consent?: boolean | undefined;
|
|
21
|
+
}): Promise<object>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find the first available Firefox binary on the system.
|
|
3
|
+
* @returns {string} Path to the binary
|
|
4
|
+
* @throws {Error} If no Firefox is found
|
|
5
|
+
*/
|
|
6
|
+
export function findFirefox(): string;
|
|
7
|
+
/**
|
|
8
|
+
* Launch Firefox with WebDriver BiDi enabled and return its session WS URL.
|
|
9
|
+
*
|
|
10
|
+
* @param {object} [opts]
|
|
11
|
+
* @param {string} [opts.binary] - Firefox binary (auto-detected if omitted)
|
|
12
|
+
* @param {number} [opts.port=0] - Remote-agent port (0 = OS-assigned)
|
|
13
|
+
* @param {boolean} [opts.headed=false] - Launch with a visible window
|
|
14
|
+
* @param {string} [opts.proxy] - Proxy 'host:port' or 'scheme://host:port'
|
|
15
|
+
* (http/https → HTTP+SSL proxy; socks/socks5/socks4 → SOCKS), via prefs
|
|
16
|
+
* @param {{width:number,height:number}} [opts.viewport] - Initial window size
|
|
17
|
+
* @returns {Promise<{wsUrl: string, process: import('node:child_process').ChildProcess, port: number, ownedProfileDir: string}>}
|
|
18
|
+
*/
|
|
19
|
+
export function launchFirefox(opts?: {
|
|
20
|
+
binary?: string | undefined;
|
|
21
|
+
port?: number | undefined;
|
|
22
|
+
headed?: boolean | undefined;
|
|
23
|
+
proxy?: string | undefined;
|
|
24
|
+
viewport?: {
|
|
25
|
+
width: number;
|
|
26
|
+
height: number;
|
|
27
|
+
} | undefined;
|
|
28
|
+
}): Promise<{
|
|
29
|
+
wsUrl: string;
|
|
30
|
+
process: import("node:child_process").ChildProcess;
|
|
31
|
+
port: number;
|
|
32
|
+
ownedProfileDir: string;
|
|
33
|
+
}>;
|
|
34
|
+
/**
|
|
35
|
+
* Kill a launched Firefox and remove its temp profile dir.
|
|
36
|
+
* @param {{process: import('node:child_process').ChildProcess, ownedProfileDir?: string}} browser
|
|
37
|
+
*/
|
|
38
|
+
export function cleanupFirefox(browser: {
|
|
39
|
+
process: import("node:child_process").ChildProcess;
|
|
40
|
+
ownedProfileDir?: string;
|
|
41
|
+
}): Promise<void>;
|
package/types/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
* @param {object} [opts]
|
|
7
7
|
* @param {'headless'|'headed'|'hybrid'} [opts.mode='headless'] - Browser mode
|
|
8
8
|
* @param {boolean} [opts.cookies=true] - Inject user's cookies (Phase 2)
|
|
9
|
+
* @param {boolean} [opts.incognito=false] - Clean, unauthenticated session:
|
|
10
|
+
* skip cookie/auth injection entirely (equivalent to cookies:false here).
|
|
9
11
|
* @param {boolean} [opts.prune=true] - Apply ARIA pruning (Phase 2)
|
|
10
12
|
* @param {number} [opts.timeout=30000] - Navigation timeout in ms
|
|
11
13
|
* @param {boolean} [opts.blockAds=true] - Block ~120 common ad/tracker
|
|
@@ -27,8 +29,9 @@
|
|
|
27
29
|
* @returns {Promise<string>} ARIA snapshot text
|
|
28
30
|
*/
|
|
29
31
|
export function browse(url: string, opts?: {
|
|
30
|
-
mode?: "
|
|
32
|
+
mode?: "headed" | "headless" | "hybrid" | undefined;
|
|
31
33
|
cookies?: boolean | undefined;
|
|
34
|
+
incognito?: boolean | undefined;
|
|
32
35
|
prune?: boolean | undefined;
|
|
33
36
|
timeout?: number | undefined;
|
|
34
37
|
blockAds?: boolean | undefined;
|
|
@@ -83,11 +86,17 @@ export function browse(url: string, opts?: {
|
|
|
83
86
|
* @param {boolean} [opts.consent=true] - Auto-dismiss cookie consent dialogs.
|
|
84
87
|
* @param {string} [opts.storageState] - Path to a storage-state JSON file
|
|
85
88
|
* (cookies + localStorage) to load before navigation.
|
|
89
|
+
* @param {boolean} [opts.incognito=false] - Clean, unauthenticated session:
|
|
90
|
+
* skip storageState loading and make injectCookies() a no-op, so no auth
|
|
91
|
+
* ever enters the session even if a caller injects unconditionally.
|
|
86
92
|
* @param {'act'|'browse'|'navigate'|'full'|'read'} [opts.pruneMode='act'] - Pruning mode.
|
|
93
|
+
* @param {'chromium'|'firefox'} [opts.engine='chromium'] - Browser engine.
|
|
94
|
+
* 'firefox' drives over WebDriver BiDi (a separate transport / page object);
|
|
95
|
+
* 'chromium' (default) uses CDP.
|
|
87
96
|
* @returns {Promise<object>} Page handle with goto, snapshot, close
|
|
88
97
|
*/
|
|
89
98
|
export function connect(opts?: {
|
|
90
|
-
mode?: "
|
|
99
|
+
mode?: "headed" | "headless" | "hybrid" | undefined;
|
|
91
100
|
port?: number | undefined;
|
|
92
101
|
downloadPath?: string | undefined;
|
|
93
102
|
blockAds?: boolean | undefined;
|
|
@@ -104,7 +113,9 @@ export function connect(opts?: {
|
|
|
104
113
|
} | undefined;
|
|
105
114
|
consent?: boolean | undefined;
|
|
106
115
|
storageState?: string | undefined;
|
|
116
|
+
incognito?: boolean | undefined;
|
|
107
117
|
pruneMode?: "act" | "browse" | "navigate" | "full" | "read" | undefined;
|
|
118
|
+
engine?: "chromium" | "firefox" | undefined;
|
|
108
119
|
}): Promise<object>;
|
|
109
120
|
/**
|
|
110
121
|
* Apply Network.setBlockedURLs for ad/tracker blocking on a session.
|
package/types/network-idle.d.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* Tracks in-flight requests by requestId in a Set, so an orphan
|
|
5
|
-
* loadingFinished/Failed (event for a request whose requestWillBeSent
|
|
6
|
-
* arrived before our listener attached) is a harmless no-op instead of
|
|
7
|
-
* driving a counter negative and resolving prematurely.
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
2
|
+
* CDP: wait for network idle over `Network.*` events.
|
|
10
3
|
* @param {object} session - CDP session-scoped handle with .on() returning unsub
|
|
11
4
|
* @param {object} [opts]
|
|
12
5
|
* @param {number} [opts.timeout=30000] - Max wait time before reject
|
|
@@ -17,3 +10,18 @@ export function waitForNetworkIdle(session: object, opts?: {
|
|
|
17
10
|
timeout?: number | undefined;
|
|
18
11
|
idle?: number | undefined;
|
|
19
12
|
}): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Firefox/BiDi: wait for network idle over `network.*` events. Subscribes the
|
|
15
|
+
* events first (idempotent — safe even if the daemon already subscribed for
|
|
16
|
+
* log capture), then runs the same orphan-resilient idle loop. The in-flight
|
|
17
|
+
* key is `params.request.request` (the BiDi request id).
|
|
18
|
+
* @param {object} bidi - BiDi client with async .subscribe() and .on() returning unsub
|
|
19
|
+
* @param {object} [opts]
|
|
20
|
+
* @param {number} [opts.timeout=30000] - Max wait time before reject
|
|
21
|
+
* @param {number} [opts.idle=500] - Required idle duration before resolve
|
|
22
|
+
* @returns {Promise<void>}
|
|
23
|
+
*/
|
|
24
|
+
export function waitForNetworkIdleBiDi(bidi: object, opts?: {
|
|
25
|
+
timeout?: number | undefined;
|
|
26
|
+
idle?: number | undefined;
|
|
27
|
+
}): Promise<void>;
|
package/types/readable.d.ts
CHANGED
|
@@ -7,6 +7,13 @@
|
|
|
7
7
|
* @returns {string}
|
|
8
8
|
*/
|
|
9
9
|
export function formatReadable(r: object): string;
|
|
10
|
+
/**
|
|
11
|
+
* Turn the raw in-page extraction result into the public readable() shape,
|
|
12
|
+
* applying the advisory confidence rule. Transport-agnostic.
|
|
13
|
+
* @param {object} r - raw result from EXTRACT_EXPRESSION
|
|
14
|
+
* @returns {object} public readable() result
|
|
15
|
+
*/
|
|
16
|
+
export function finalizeReadable(r: object): object;
|
|
10
17
|
/**
|
|
11
18
|
* Extract the main article from the current page.
|
|
12
19
|
* @param {object} session - CDP session-scoped handle (.send()).
|
|
@@ -16,3 +23,4 @@ export function formatReadable(r: object): string;
|
|
|
16
23
|
* confidence: 'high'|'low', readerable, hint? } — extracted article
|
|
17
24
|
*/
|
|
18
25
|
export function readable(session: object): Promise<object>;
|
|
26
|
+
export const EXTRACT_EXPRESSION: string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Register the Firefox stealth patches so they run before any page script.
|
|
3
|
+
* Must be called before the first navigation. addPreloadScript is global
|
|
4
|
+
* (applies to every browsing context / navigation), so a single registration
|
|
5
|
+
* covers the whole session.
|
|
6
|
+
*
|
|
7
|
+
* @param {object} bidi - BiDi client from createBiDi()
|
|
8
|
+
*/
|
|
9
|
+
export function applyFirefoxStealth(bidi: object): Promise<void>;
|
package/types/stealth.d.ts
CHANGED
|
@@ -12,3 +12,34 @@
|
|
|
12
12
|
* @param {object} session - Session-scoped CDP handle
|
|
13
13
|
*/
|
|
14
14
|
export function applyStealth(session: object): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* stealth.js — Anti-detection patches for headless Chromium.
|
|
17
|
+
*
|
|
18
|
+
* Uses Page.addScriptToEvaluateOnNewDocument so patches run before
|
|
19
|
+
* any page scripts (unlike Runtime.evaluate which runs after).
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* navigator.webdriver hiding — the one automation tell shared by every engine
|
|
23
|
+
* (Chromium headless AND Firefox-under-BiDi both report `true`). Split out so
|
|
24
|
+
* the Firefox stealth path (stealth-firefox.js) reuses the exact same patch
|
|
25
|
+
* instead of duplicating it. Measured baseline on both engines: `true`.
|
|
26
|
+
*
|
|
27
|
+
* A naive `Object.defineProperty(navigator, 'webdriver', …)` hides the *value*
|
|
28
|
+
* but leaves an advanced tell: it creates an OWN property on the instance, so
|
|
29
|
+
* `navigator.hasOwnProperty('webdriver')` becomes `true` where a real browser
|
|
30
|
+
* (property lives on Navigator.prototype) reports `false` — sophisticated
|
|
31
|
+
* anti-bot checks (e.g. sannysoft's "WebDriver New") catch that inconsistency.
|
|
32
|
+
* So we DELETE the getter off the prototype instead: then `navigator.webdriver`
|
|
33
|
+
* is `undefined` AND `'webdriver' in navigator` AND `hasOwnProperty` are both
|
|
34
|
+
* `false`, matching a stock browser. POC-verified on Chromium and Firefox
|
|
35
|
+
* (own/in both false, prototype descriptor gone). The defineProperty fallback
|
|
36
|
+
* only fires if the descriptor is somehow non-configurable (delete a no-op).
|
|
37
|
+
*/
|
|
38
|
+
export const WEBDRIVER_PATCH: "\n try {\n const proto = Object.getPrototypeOf(navigator);\n const desc = proto && Object.getOwnPropertyDescriptor(proto, 'webdriver');\n if (desc && desc.configurable) delete proto.webdriver;\n if (navigator.webdriver) {\n Object.defineProperty(navigator, 'webdriver', { get: () => undefined, configurable: true });\n }\n } catch {}\n";
|
|
39
|
+
/**
|
|
40
|
+
* Canvas-fingerprint noise — browser-agnostic (standard Canvas2D API), so both
|
|
41
|
+
* the Chromium and Firefox stealth scripts compose it. This block carries the
|
|
42
|
+
* subtle double-XOR-cancellation fixes (see git history / project memory); keep
|
|
43
|
+
* it SINGLE-SOURCED here so a fix never has to be made in two places.
|
|
44
|
+
*/
|
|
45
|
+
export const CANVAS_NOISE_PATCH: "\n // Canvas fingerprinting \u2014 sites render standard text/shapes, then read\n // pixels via toDataURL or getImageData. The output is stable per machine\n // (GPU, font rasterizer, anti-aliasing) but unique across machines, which\n // makes it the second-most-common fingerprint after WebGL. Defense: nudge\n // a few RGB channels by \u00B11 per session so the hash changes each visit\n // while the canvas still looks identical to the human eye. The per-tab\n // seed keeps reads stable within a session so legitimate canvas use\n // (image processing, games) doesn't flicker.\n // crypto.getRandomValues is guaranteed unique per browsing context; using\n // Math.random alone can collide when two fresh V8 contexts start within\n // microseconds of each other (real-world: parallel tests, real-world hit:\n // we observed it). performance.now adds a wall-clock anchor as a belt-and-\n // braces guard against contexts where crypto is somehow stubbed.\n const _seedBuf = new Uint32Array(1);\n crypto.getRandomValues(_seedBuf);\n const CANVAS_SEED = (_seedBuf[0] ^ ((performance.now() * 1e6) | 0)) >>> 0;\n function shiftPixels(data) {\n // Touch ~1 byte per 64-byte stride. The bit we XOR with is taken from a\n // position-dependent SLICE of a seed-mixed hash, not its low bit \u2014 a\n // naive 'mix & 1' collapses to only two possible outputs per seed\n // parity because every stride index is even (the position multiplier\n // is odd, so the low bit only depends on seed parity). Indexing the\n // hash by (i/64) mod 32 makes every stride position sample a different\n // bit, so two distinct seeds produce different mask patterns.\n for (let i = 0; i < data.length; i += 64) {\n const h = ((CANVAS_SEED * 2654435761) ^ (i * 1597334677)) >>> 0;\n const bit = (h >>> ((i >>> 6) & 31)) & 1;\n data[i] = (data[i] ^ bit) & 0xff;\n }\n return data;\n }\n // Capture originals BEFORE replacing \u2014 toDataURL must read pixels via the\n // native getImageData (not the patched one), otherwise the patch double-\n // applies and the second XOR cancels the first, leaving output unchanged.\n const origGetImageData = CanvasRenderingContext2D.prototype.getImageData;\n const origToDataURL = HTMLCanvasElement.prototype.toDataURL;\n HTMLCanvasElement.prototype.toDataURL = function() {\n const ctx = this.getContext('2d');\n if (ctx && this.width > 0 && this.height > 0) {\n try {\n const img = origGetImageData.call(ctx, 0, 0, this.width, this.height);\n // Snapshot the original bytes so we can restore them after encoding.\n // Without this, repeated toDataURL() alternates noisy/clean: call 1\n // XORs the canvas in place, call 2 reads the noisy canvas and XORs\n // again (self-inverse), call 3 again, etc. Same XOR-cancellation\n // class as the earlier double-apply bug, just through canvas state\n // rather than method composition. The restore also keeps the\n // bitmap idempotent for any downstream legitimate canvas reads.\n const original = new Uint8ClampedArray(img.data);\n shiftPixels(img.data);\n ctx.putImageData(img, 0, 0);\n const result = origToDataURL.apply(this, arguments);\n img.data.set(original);\n ctx.putImageData(img, 0, 0);\n return result;\n } catch {\n // Tainted canvas (cross-origin image) \u2014 can't read; skip the nudge\n // and fall through to the original call so the page sees the\n // expected SecurityError instead of silent corruption.\n }\n }\n return origToDataURL.apply(this, arguments);\n };\n CanvasRenderingContext2D.prototype.getImageData = function() {\n const img = origGetImageData.apply(this, arguments);\n shiftPixels(img.data);\n return img;\n };\n";
|