Browser Console - JS - Running a JS function after the page loads completely.

Asked

Viewed 306 times

0

I am doing Javascript tests using my browser and would like to do the following steps:

  • Create an array containing 3 tabs

    janelas = Array();
    janelas.push(window.open(link_site1));
    janelas.push(window.open(link_site2));
    janelas.push(window.open(link_site3));
    
  • I want once the page is fully loaded to check if a field exists:

        for (var key in janelas) {
    if(janela.document.readyState == 'complete'){
        console.log("Site:" + janela.location.href + "Status:" + janela.document.readyState);
        setTimeout(function(){
            try{
                if(janela.jQuery("h1:contains(Plugins)")[0].innerHTML == 'Plugins'){
                    if(janela.jQuery(hrefkey)[0]){
                        janela.jQuery(hrefkey)[0].click();
                        console.log('OK: ' + janela.location.href);
                    }else{
                        console.log('OK: Plugin ativo - ' + janela.location.href);
                    }
                };
            }catch(err){
                console.log('Não foi possivel acessar: ' + janela.location.href);
            }   
        },1000);
    }
    } 
    

But even using if(janela.document.readyState == 'complete'){} it is running all content before loading the page, making the check if(janela.jQuery("h1:contains(Plugins)")[0].innerHTML == 'Plugins'){} always be false.

============================================================

The whole function I’m trying to use is this :

            // Entrar em SITES 
    function abrirSites(){
        var filtro = 'site-info.php';
        var seletor = 'a.edit';
        var sites = document.querySelectorAll(seletor);
        janelas = Array();

        for (var key in sites){
            try {
                link_site = sites[key].href;
                if(!link_site.includes(filtro)){
                    janelas.push(window.open(link_site));
                    console.log(link_site);
                }
            }catch(e){
                console.log(e);
            }

        }
    }



    // Entrar na pagina de configurações de Plugin
    function ativarConfig(janelas,hrefkey){
        hrefkey = "[href='plugins.php?action=activate&plugin=wpsitesynccontent%2Fwpsitesynccontent.php&plugin_status=all&paged=1&s&_wpnonce=e8662b15b8']";
        for (var key in janelas){
            janela = janelas[key];
            // console.log(janela.location.href);
            if(!janela.location.href.includes("plugins.php")){
                janela.location.href = janela.location.href + 'plugins.php';
            };

            // Executar quando pagina estiver pronta 
            do{
                if(janela.document.readyState == 'complete'){
                    console.log("Site:" + janela.location.href + "Status:" + janela.document.readyState);
                    setTimeout(function(){
                        try{
                            if(janela.jQuery("h1:contains(Plugins)")[0].innerHTML == 'Plugins'){
                                if(janela.jQuery(hrefkey)[0]){
                                    janela.jQuery(hrefkey)[0].click();
                                    console.log('OK: ' + janela.location.href);
                                }else{
                                    console.log('OK: Plugin ativo - ' + janela.location.href);
                                }
                            };
                        }catch(err){
                            console.log('Não foi possivel acessar: ' + janela.location.href);
                        }   
                    },1000);
                }
            }while(janela.document.readyState != 'complete');
        }
    }
    // Um de cada vez
    abrirSites();
    ativarConfig(janelas);
  • Why not add the attributedefer on the tag <script>?

  • I’m doing it right in the browser, I don’t have an html file

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.