Yes, that embedded script overrides the entire frame code.
A way to solve this is having an “iframe” hosted inside the HTML component (which is an “iframe” in itself). That way you can replace the content of the inner iframe while keeping the script that’s listening for changes.
Try something like this (where “theFrame” is the ID of the iframe element you put in the body of your HTML component):
document.getElementById("theFrame").srcdoc = event.data;