JavaScript, often abbreviated as JS, is a high-level, interpreted programming language that conforms to the ECMAScript specification.
jQuery is a JavaScript library designed to simplify HTML DOM tree traversal and manipulation.
We can use them to do a wide range of things which we do manually and is tiresome.
Examples:
Note: On first run of the script a popup will come. We have to allow popups because Chrome wants to know if we want multiple tabs to be used or not. Then re-run the script.
How to run:
Search for anything on Google. On Google search results page paste the script on the developer tools console window and press Enter and see the magic.
Let’s see the code:
// dynamically loading the jQuery library javascript: var fileref = document.createElement( 'script' );
fileref.setAttribute( "type" , "text/javascript" );
fileref.setAttribute( "src" ,
document.body.appendChild(fileref); var arr = []; //this array will contain the links of google search results.
setTimeout( function () {
// We are using setTimeout function so that // jQuery library is loaded fully before running //rest part of the code // Accessing the DOM and using it to extract
// links from the search results
var count1 = $( "div.bkWMgd" ).length;
var j;
// There are many div's with this class "div.bkWMgd" but we
// will be using only the div's which will be
// containing the search results links,
// so we have to filter out the div's by performing if-else.
for (j = 0; j < count1; j++) {
if ($( "div.bkWMgd" )[j].firstChild == null ) {
// not using this section because it does not contain the links
continue ;
} else if
($( "div.bkWMgd" )[j].firstChild.className.toString().trim() === "srg" ) {
// This div contains the search result links
var count2 = $( "div.bkWMgd" )[j].firstChild.children.length;
var i;
for (i = 0; i < count2; i++) {
arr.push(
$( "div.bkWMgd" )[j].firstChild.children[i].children[0].
firstChild.firstChild.firstChild.href.toString().trim());
// inserting search results links into the array
}
count2 = 0;
continue ;
} else if
($( "div.bkWMgd" )[j].firstChild.innerHTML.toString().trim()=== "Web results" ) {
// This div also contains the search result links
$( "div.bkWMgd" )[j].firstChild.remove();
var count2 = $( "div.bkWMgd" )[j].firstChild.children.length;
try { // Handling error if encountered using try catch block
var i;
for (i = 0; i < count2; i++) {
arr.push(
$( "div.bkWMgd" )[j].firstChild.children[i].children[0].
firstChild.firstChild.firstChild.href.toString().trim());
} // inserting links into the array
count2 = 0;
} catch (err) {
console.log('Error handled in : ', j + ' loop');
}
}
}
}, 1000); // opening multiple tabs with the links in the array function open_win() {
for ( var i = 0; i < arr.length; i++) {
console.log(arr[i]);
window.open(arr[i]);
}
} // in 2.5 seconds multiple tabs will get // open with the search results links setTimeout( function () {
open_win();
}, 2500); |
Live results