Click here to Skip to main content
15,887,256 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello. Here is the problem of my code.
There is a html content with pages labels on the bottom , I can click the page labels, then the content changed.
I'm using js code to simulate this operation , click each page label , then fetch the content displayed and something like that.
BUT , It seemed the content did not updated immidiately , the content fetched repeat the first page.
The fetching operation should be done after the page content has been changed. I tried the 'Promise', seems not code correct, the result just not right.

What I have tried:

JavaScript
if(window.location.href.includes('https://www.hide-my-ip.com/proxylist.shtml')) {
    //overview each page label
    for( var i of document.querySelectorAll('span a.paginate_button')) {
        new Promise((resolve,reject) => {
            //simulate the clickin on page label
            i.click();
            //alert(i.innerText);
            resolve();
        }).then( () => {
            console.log("current page index :"+ document.querySelector('.current').innerText);
            //fetch the content of switched page
            for(var item of document.querySelector('table tbody').children) {
                item = item.children;
                console.log(item[0].innerText + ':' + item[1].innerText);
            }
        });
    };
}


As the comment referred , Mutation observer can somehow deal with it.
JavaScript
if(window.location.href.includes('https://www.hide-my-ip.com/proxylist.shtml')) {
    new MutationObserver(function(mutation,observer) {
        for(let mu of mutation) {
            //content fetch.
            if(mu.addedNodes.length > 0) {
                for(var item of document.querySelector('table tbody').children) {
                    item = item.children;
                    console.log(item[0].innerText + ':' + item[1].innerText);
                }
            }
        }
        
        //get next page label and click
        var curr_pageidx=parseInt(document.querySelector('.current').innerText);
        var next_pageidx=curr_pageidx+1;
        var page_nodes=document.querySelectorAll('span a.paginate_button');
        for(let idx=0;idx<page_nodes.length;idx++) {
            if(parseInt(page_nodes[idx].innerText) == next_pageidx) {
                page_nodes[idx].click();
                break;
            }
        }
    }).observe(document.querySelector('tbody'),{childList: true});        
}
Posted
Updated 17-Nov-23 3:17am
v4
Comments
Richard Deeming 16-Nov-23 8:08am    
Your code is looking for the element with class="current". But there is nothing in your code that changes the class on any elements.

There is also nothing in your code which would change the content of the HTML document.

Either you're missing some relevant code from your question, or you're missing some code from your page.
Yount_0701 16-Nov-23 21:22pm    
Yeah, my code did nothing to alter the html content , the page label click event did it.
All I do is to simulate the page label click event. without my code , you can click the page label directly , then you will see the content changed , which all have been done by the original html js code itself. Here I'm doing is using extra injected js code to simulate this operation.
Richard Deeming 17-Nov-23 3:35am    
Then there's no way to know what the problem is without looking at the click event handler.

At a guess, it's making an asychronous request to the server to get the updated content, and your promise is resolving before that content has loaded. In which case, there may not be any way to know when the request has completed short of using a MutationObserver[^] on the element that gets updated.
Yount_0701 17-Nov-23 9:14am    
That's a good ideal, I tried and it works. I paste the alter code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900