Search code examples
javascriptjqueryeventsdom-eventstampermonkey

How to catch/watch append trigger in Javascript in Tampermonkey?


I'm writing Tampermonkey script in which in a web page I want to run a function which should only run whenever some script append html code in body tag.

It can be from:

$('body').append('<div>new html</div>');
//or
document.getElementsByTagName('body')[0].innerHTML += '<div>new html</div>';
//or
document.getElementsByTagName('body')[0]insertAdjacentHTML('afterend', '<div>new html</div>')
//or from any other library

Now in Tampermonkey I want run script which should catch/watch these and run my custom Tampermonkey function after it.

I'm already using unsafewindow in Tampermonkey to access web pages Javascript.

The reason of me asking this question is that I failed to find anything related like this.

I only need a way to catch these events, not the whole code.


Solution

  • Have you tried using a MutationObserver?