there is such a piece of script that gets h3 with the class "r" and displays true / false, but now r is always there and I want to do so - if one r is false, if two r is true. Tell me how to add a check for the presence of two classes "r" at the same time. I would be very grateful, good code to everyone.

function isIndexed(page,ignoreCase) { var urls=[],RETRIES=5; if(!page)return; if(page.indexOf("://")!==-1)page=page.split("://")[1].trim(); var url='https://www.google.com/search?q='+encodeURIComponent(page)+'&fp=1&dpr=2&sns=1&pf=p&tch=1&filter=0'; if(page.slice(-1)=="/")page=page.slice(0,-1); try { var serp=UrlFetchApp.fetch(url,{muteHttpExceptions:true,method:"GET",followRedirects:true}); var result=serp.getContentText().split('/*""*/'); var searchResults,serpResults,isIndexed=false; for(var i=2;i<result.length-1;i++) { searchResults=JSON.parse(result[i]); serpResults=searchResults.d.split('<h3 class="r"><a href="/url?q='); isIndexed=findIndexedURL(serpResults, page, ignoreCase); if(isIndexed) return "Yes"; } return "No"; } catch(e) { return "No"; } } 

    2 answers 2

     function isIndexed(page,ignoreCase) { var urls=[],RETRIES=5; if(!page)return; if(page.indexOf("://")!==-1)page=page.split("://")[1].trim(); var url='https://www.google.com/search?q='+encodeURIComponent(page)+'&fp=1&dpr=2&sns=1&pf=p&tch=1&filter=0'; if(page.slice(-1)=="/")page=page.slice(0,-1); try { var serp=UrlFetchApp.fetch(url,{muteHttpExceptions:true,method:"GET",followRedirects:true}); var result=serp.getContentText().split('/*""*/'); var searchResults,serpResults,isIndexed=false; var count // количество r for(var i=2;i<result.length-1;i++) { searchResults=JSON.parse(result[i]); serpResults=searchResults.d.split('<h3 class="r"><a href="/url?q='); isIndexed=findIndexedURL(serpResults, page, ignoreCase); if(isIndexed) count++ // добавляет +1 к количеству r } // если r двое if (count === 2) return "Yes"; return "No"; } catch(e) { return "No"; } 

      Like so.

       var r1 = document.getElementsByTagName('h1')[0].classList.contains('r'); var r2 = document.getElementsByTagName('h1')[1].classList.contains('r'); console.log(r1 && r2); 
       <h1 class="r">First</h1> <h1 class="r">Second</h1>