Detecting the need of queuing scripts and styles is not possible since this requires access to the final post output which is not available before the scripts and styles got queued already.
However before queuing scripts and styles the raw post content could be checked if there is a Wordpress gallery shortcode or a Gutenberg gallery block used. This could be used to offer an option like "enable only if post or page contains a Wordpress or Gutenberg gallery".
Also see https://wordpress.org/support/topic/include-lightbox-js-only-when-gallery-is-used-on-that-page
Detecting the need of queuing scripts and styles is not possible since this requires access to the final post output which is not available before the scripts and styles got queued already.
However before queuing scripts and styles the raw post content could be checked if there is a Wordpress gallery shortcode or a Gutenberg gallery block used. This could be used to offer an option like "enable only if post or page contains a Wordpress or Gutenberg gallery".