aaaa

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.8 kB
0
Indexable
Never
var processInterval;
var auto = false;


function process(){
    clearInterval(processInterval)

    let index = 0;
    let links = document.querySelectorAll('.product-list--current-page a[data-auto="product-tile--title"]:not(.checked)');
       
    
    let pageID = document.querySelector('.prev-next .icon-icon_whitechevronright').parentNode.href;

    processInterval = setInterval(()=>{
        
        

        let scrape = function(linkEl){

            index++;
            if(index < (links.length - 1)){

                console.log(index, links.length)

                if(!linkEl.classList.contains('checked')){

                    linkEl.classList.add('checked');

                    fetch(linkEl.href)
                        .then(function(response) {
                            // When the page is loaded convert it to text
                            return response.text()
                        })
                        .then(function(html) {

                            // Initialize the DOM parser
                            var parser = new DOMParser();
                
                            // Parse the text
                            var doc = parser.parseFromString(html, "text/html");
                
                            let fat = null;
                            let carbs = null;
                            let protein = null;
                        
                            doc.querySelectorAll('.product__info-table tr').forEach((tr)=>{
                                if(tr.querySelector('td:nth-child(2)')){
                                    
                                    let label = tr.querySelector('td:nth-child(1)').innerText.replace(/\s+/g, '').replace('(g)', '').replace(':', '')
                                    let value = parseFloat(tr.querySelector('td:nth-child(2)').innerText.replace(/\s+/g, '').replace('g','').replace('<', ''))
                                    
                                    
                                    if(label == 'Fat'){
                                        
                                        fat = value
                                    }
                                    if(label == 'Carbohydrate'){
                                        carbs = value
                                    }
                                    if(label == 'Protein'){
                                        protein = value
                                    }
                                }
                        
                            });
                        
                            if(fat !== null && carbs !== null && protein !== null){
                                let peRatio = protein/ (carbs+fat)
                                
                                linkEl.closest('.product-list--list-item').querySelector('.beans-price__subtext').innerHTML += '<p class="pe-ratio" style="color:'+(peRatio >= 1 ? 'green' : (peRatio >= 0.9 ? 'orange' : 'red'))+';">PE Ratio = '+peRatio+'</p>';
                                console.log(fat, carbs, protein, peRatio)
                                        
                            }

                            
                            scrape(links[index]);
                
                        })
                        .catch(function(err) {  
                            console.log('Failed to fetch page: ', err);  
                        });

                }
                
            
        
                

            }else if(auto){

                if(document.querySelector('.prev-next .icon-icon_whitechevronright') != null){
                    document.querySelector('.prev-next .icon-icon_whitechevronright').parentNode.click()
                    var checkForNewItems = setInterval(()=>{
                        if(document.querySelector('.prev-next .icon-icon_whitechevronright').parentNode.href != pageID && document.querySelectorAll('.product-list--current-page a[data-auto="product-tile--title"]:not(.checked)').length > 8){
                            clearInterval(checkForNewItems);
                            process();
                            
                        }
                        // console.log(document.querySelectorAll('.product-list a[data-auto="product-tile--title"]:not(.checked)').length)
                        // process();
                    }, 1000)
                }
            }

            

        
            
        }
        scrape(links[index])
    }, 1000)

}
process();

let processBtn = document.createElement('button');
processBtn.innerText = 'Process All'
document.querySelector('.pagination--page-selector-wrapper').appendChild(processBtn)

processBtn.onclick = function(){
    auto = true;
}