Funcion que hace busqueda en diferentes fuentes en internet

PHOTO EMBED

Sun Nov 27 2022 20:09:48 GMT+0000 (Coordinated Universal Time)

Saved by @modesto59 #html

/* write a function to search data from different sources in internet */
function search(query) {
  var url = 'https://www.google.com/search?q=' + encodeURIComponent(query);
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      var parser = new DOMParser();
      var doc = parser.parseFromString(xhr.responseText, 'text/html');
      var results = doc.querySelectorAll('h3.r a');
      for (var i = 0; i < results.length; i++) {
        var result = results[i];
        var url = result.getAttribute('href');
        var title = result.textContent;
        var p = document.createElement('p');
        var a = document.createElement('a');
        a.textContent = title;
        a.setAttribute('href', url);
        p.appendChild(a);
        document.body.appendChild(p);
      }
    }
  };
  xhr.send();
}
content_copyCOPY