Home > OS >  open link search then close javascript
open link search then close javascript

Time:11-08

I try to open links, what i found, in a new window, then search for a text, and then close the new window. But its not working, the new window not closed, and the search also not working. and just open the first window.

 links = document.getElementsByTagName("a");
for (var i = 0, l = links.length; i < l; i  ) {
  var link = links[i];
  if (link.href.indexOf("jooble") !== -1) {
        var newWindow = window.open(links[i].href);
        setTimeout(function(){ 
        console.log("5s")
         if (
                (newWindow.document.documentElement.textContent || newWindow.document.documentElement.innerText).indexOf('Jófogás') > -1) {
            console.log("Jófogás")
             
}   if(newWindow){
        newWindow.close();
    }
}, 5000);
                                            }
                                                }`

CodePudding user response:

unfortunately you cant's access to a window.open html contents.

var mywindow = window.open('https://www.wikipedia.org/', 'title', 'height=500, width=500'); 
console.log(mywindow.document) //you will get an empty html tag

You have to use a Web Scraper library like puppeteer for that

CodePudding user response:

You not creating variable that your problem, place let before links and try again

let links = document.getElementsByTagName("a");
for (var i = 0, l = links.length; i < l; i  ) {
  var link = links[i];
  if (link.href.indexOf("jooble") !== -1) {
        var newWindow = window.open(links[i].href);
        setTimeout(function(){ 
        console.log("5s")
         if (
                (newWindow.document.documentElement.textContent || newWindow.document.documentElement.innerText).indexOf('Jófogás') > -1) {
            console.log("Jófogás")
             
}   if(newWindow){
        newWindow.close();
    }
}, 5000);
                                        }
                                            }
  • Related