Your event listener script is a page load event (DOMContentLoaded). Since the script is looking for the appropriate containers (based off the ID prefix), there’s a chance that the RG elements are rendering after the script runs.

You don’t need the DOM load event and instead run the main code in a Run Javascript action (Toolbox plugin). The Run Javascript action should be in a WF that you are sure that the RG has been fully loaded.