Execute a script on page render

Suppose I am writing a plugin that needs to fetch some data via webviewApi.postMessage. Very much like joplin/markdownItTestPlugin.ts at dev · laurent22/joplin · GitHub but this one sends a message on button click, I want to do it when the note is rendered. Is there any way to achive this?

I've tried setting document.onload but that didn't work for me (though I'm not an expert on JS, might have done something wrong).

I do it in a bit hacky way : insert empty <style> tag with onload that will trigger embedded webviewApi

 const postMessageWithResponseTest = `
     webviewApi.postMessage('${contentScriptId}','${token.content.trim()}').then(function(response) {
     document.getElementById('embed-search').innerHTML=response;
                            });return false;`;
        
     return `//returns token
        <div id="embed-search"></div>
        <style onload="${postMessageWithResponseTest.replace(/\n/g, ' ')}"></style>             
                        `;

Yes, thank you. I have already seen this in your backlinks plugin today.

Just one thing that i noted a moment ago - style tag must but closed with matching </style> - if its self enclosed - like <style /> it will break the rendering of note.

So example code should be
<style onload="${postMessageWithResponseTest.replace(/\n/g, ' ')}"></style>
not
<style onload="${postMessageWithResponseTest.replace(/\n/g, ' ')}"/>