Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In HOOK_preprocess_block()
there is a variable $variables['block_html_id']
available which is the unique HTML ID of the block. Ideally the javascript should use that value, as it would allow the same block to be embedded twice, eg via Panels.
I haven't looked too thoroughly, but I'm guessing you would still need to record the block module and delta (maybe via a data-attribute) so you can retrieve the block from the database.
Does this sound as simple as I think it does?