hide-if-canvas-contains
The hide-if-canvas-contains snippet conceals canvas elements or their parent containers by monitoring canvas rendering methods (fillText, strokeText, drawImage).
Parameters
|
Name |
Description |
Mandatory |
|---|---|---|
|
search |
A string or regex pattern to match against text rendered to canvas or image sources. Text from |
Yes |
|
selector |
CSS selector identifying which element to hide. Defaults to the canvas element itself. |
No |
|
clearRectBehavior |
Controls whether the accumulated text is forgotten when the site clears the canvas. See below. Defaults to an empty string, and is ignored in |
No |
|
mode |
What the |
No |
clearRectBehavior
Sites often clear their canvas before drawing new content, using clearRect. This parameter controls whether that resets the text accumulated so far.
|
Value |
Behaviour |
|---|---|
|
Omitted or empty |
The accumulated text is reset only when the entire canvas is cleared, since a full clear usually means a new render is starting. Partial clears do not reset it. Start here unless you observe problems. |
|
|
The text is forgotten whenever any part of the canvas is cleared. Use this if the snippet matches content that should not be hidden, which happens when a site clears small areas between draws and unrelated fragments combine into a false match. |
|
|
The text is never reset. Use this if the snippet misses an ad that should match, which happens when the label is drawn in several passes with full clears in between. |
When more than one filter targets the same canvas, these values are aggregated rather than applied independently: if any active filter uses always, text is forgotten on every clearRect, and text is only never forgotten when all of them specify never.
mode
|
Value |
Behaviour |
|---|---|
|
Omitted or empty |
Text mode. The pattern is matched against text drawn via |
|
|
Data-URL mode. The pattern is matched against |
In data mode, a canvas tainted by cross-origin images cannot be read — toDataURL() throws a SecurityError — and is skipped. This is a browser restriction.
Examples
-
hide-if-canvas-contains /sponsored/— hides canvas elements containing "sponsored" text -
hide-if-canvas-contains /sponsored/ .canvas-parent— hides parent elements with the specified class -
hide-if-canvas-contains /AAAAoAAAAKCA/ .canvas-parent— image matching using regex -
hide-if-canvas-contains /sponsored/ canvas never— matches even when the site clears the canvas between drawing passes -
hide-if-canvas-contains /iVBORw0KGgo/ '#ad' '' data— data-URL mode, hiding the closest#adancestor.clearRectBehavioris passed as an empty string because it is ignored here but still occupies its argument position