hide-if-canvas-contains

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 fillText and strokeText is accumulated per canvas and the pattern is tested against the full combined string, so a label drawn one character at a time still matches.

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 data mode.

No

mode

What the search pattern is matched against. See below. Defaults to an empty string, which is text mode.

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.

always

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.

never

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 fillText / strokeText and against the image source in drawImage calls. This is the right choice for the large majority of cases.

data

Data-URL mode. The pattern is matched against canvas.toDataURL(), which is base64-encoded PNG pixel data rather than readable text — so you match a byte signature read out of a debug session, not a word like "Sponsored". Use it when the label leaves nothing useful in the text buffer or the image source, for example when it is painted pixel by pixel.

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 #ad ancestor. clearRectBehavior is passed as an empty string because it is ignored here but still occupies its argument position