I am working on a small Wordpress plugin which uses JavaScript to add html tags with a css class to the visual text view in the Wordpress editor. I use the Classic Editor.
According to Chrome Developer tools, the visual text editor consists of a few div elements, and then an iframe element with the id "content_ifr" which loads a second body element which then contains all html tags pertaining to the post:
<body id="tinymce" class="mce-content-body content post-type-post post-status-publish post-format-standard page-template-default locale-de-de mceContentBody wp-editor wp-autoresize html5-captions" data-id="content" dir="ltr" style="overflow-y: hidden;" contenteditable="true"><p>Post content</p></body>
I have already figured out that I can access this second body element with
document.getElementById("content_ifr").contentDocument.body
But this only works with manual input in the console, after everything has loaded. It doesn't work in the JavaScript file of the plugin which the plugin's php file loads via wp_enqueue_scripts. The corresponding variable always returns null.
How can I make the contents of the Classic editor accessible to the JavaScript file of my plugin?