📜  在 Chrome 的新标签页中同时打开谷歌搜索结果

📅  最后修改于: 2021-11-24 04:22:11             🧑  作者: Mango

JavaScript,通常缩写为 JS,是一种符合 ECMAScript 规范的高级解释型编程语言。
jQuery 是一个 JavaScript 库,旨在简化 HTML DOM 树的遍历和操作。

我们可以使用它们来做各种各样的事情,而这些事情我们手动完成并且很烦人。

例子:

如何运行:
在 Google 上搜索任何内容。在 Google 搜索结果页面上,将脚本粘贴到开发人员工具控制台窗口上,然后按 Enter 即可查看效果。

让我们看看代码:

// dynamically loading the jQuery library
javascript: var fileref = document.createElement('script');
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", 
 "https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js");
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 serach 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);  

实时结果