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

0

I'm doing JavaScript tests using my browser and would like to take the following steps:

  • Create an array that contains 3 tabs

    janelas = Array();
    janelas.push(window.open(link_site1));
    janelas.push(window.open(link_site2));
    janelas.push(window.open(link_site3));
    
  • I want as soon as the page loads completely 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 executing all content before loading the page, causing if(janela.jQuery("h1:contains(Plugins)")[0].innerHTML == 'Plugins'){} to always be false.

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

The full 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);
    
asked by anonymous 02.08.2017 / 22:35

0 answers