How to execute code after the page is fully loaded on messenger.com

Viewed 29

I'm trying to execute code after the page is fully loaded using this chrome extension https://chrome.google.com/webstore/detail/user-javascript-and-css/nbhcbdghjpllgmfilhnhkllmkecfmpld Tried window.onload, document.addEventListener('load',...), document.addEventListener('DOMContentLoaded',...), I have no more ideas

Code above works

setTimeout(() => {
    let placeForButtons = document.body.querySelector('[class="bdao358l om3e55n1 g4tp4svg alzwoclg jg3vgc78 i15ihif8 aeinzg81 sl27f92c i85zmo3j sr926ui1 jl2a5g8c sn0e7ne5 f6rbj1fe l3ldwz01 srn514ro s9xz0pwp rl78xhln c4m0enpj c7y9u1f0 f5ap8yob"]');
    console.log("place", placeForButtons);
    document.querySelector('[class="alzwoclg jcxyg2ei i85zmo3j"]').remove();
    placeForButtons.appendChild(startButton);
    placeForButtons.appendChild(stopButton);
}, 2000);

This one don't

$(window).on('load', function() {
    let placeForButtons = document.body.querySelector('[class="bdao358l om3e55n1 g4tp4svg alzwoclg jg3vgc78 i15ihif8 aeinzg81 sl27f92c i85zmo3j sr926ui1 jl2a5g8c sn0e7ne5 f6rbj1fe l3ldwz01 srn514ro s9xz0pwp rl78xhln c4m0enpj c7y9u1f0 f5ap8yob"]');
    console.log("place", placeForButtons);
    document.querySelector('[class="alzwoclg jcxyg2ei i85zmo3j"]').remove();
    placeForButtons.appendChild(startButton);
    placeForButtons.appendChild(stopButton);
});

jQuery way output

Also, I tried observer

const observer = new MutationObserver(function() {
    console.log('start');
    if(document.querySelector('[class="alzwoclg jcxyg2ei i85zmo3j"]')) {
    let placeForButtons = document.body.querySelector('[class="bdao358l om3e55n1 g4tp4svg alzwoclg jg3vgc78 i15ihif8 aeinzg81 sl27f92c i85zmo3j sr926ui1 jl2a5g8c sn0e7ne5 f6rbj1fe l3ldwz01 srn514ro s9xz0pwp rl78xhln c4m0enpj c7y9u1f0 f5ap8yob"]');
    console.log("place", placeForButtons);
    document.querySelector('[class="alzwoclg jcxyg2ei i85zmo3j"]').remove();
    placeForButtons.appendChild(startButton);
    placeForButtons.appendChild(stopButton);
    observer.disconnect();
    }
});

observer.observe(document, {childList: {
    subtree: true,
    childList: true
  }});

Observer without output

1 Answers

I tried your Mutation Observer code and I noticed that maybe you have a typo on

{
childList: {
subtree: true,
childList: true
}}

ChildList is one of the properties and not an object, so maybe if you write it to be

{ subtree: true, childList: true }

The code will run. I did some tests on random pages and applying this the code outputs "start" for every change on DOM.

Related