/* 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();
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter