google-search-helper

在新标签页打开google搜索结果

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         google-search-helper
// @namespace    github.com/leychan
// @version      0.7
// @description  在新标签页打开google搜索结果
// @author       leychan
// @match        https://www.google.com.hk/search*
// @match        https://www.google.com/search*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @run-at document-end
// @grant        none
// @license       MIT
// ==/UserScript==

(function() {
  'use strict';

  let searchResult = document.getElementById('search')

  let allSearchResultATag = searchResult.getElementsByTagName('a')
  for (let i = 0 ;i < allSearchResultATag.length; i++) {
      allSearchResultATag[i].setAttribute('target', '_blank')
  }

  // 创建一个新的MutationObserver对象,用来监听DOM的变化, 适配另一部分的搜索结果项
  const observer = new MutationObserver((mutations, obs) => {
      for (let mutation of mutations) {
          // 遍历所有被添加的节点
          for (let node of mutation.addedNodes) {
              if (node.nodeType === 1 && node.tagName.toLowerCase() === 'div') {
                  let aTagList = node.getElementsByTagName('a')
                  if (aTagList != null) {
                      for (let i = 0 ;i < aTagList.length; i++) {
                          aTagList[i].setAttribute('target', '_blank')
                      }
                  }
              }
          }
      }
  });

  // 配置observer的观察选项
  const config = {
      childList: true, // 监控目标节点的子节点的变化,如被添加或者删除
      subtree: true // 监控目标节点及其所有后代节点, 必须打开
  };

  // 选择要监控的目标节点
  const targetNode = document.getElementById('center_col'); //监控搜索结果内容

  // 对目标节点执行监控
  observer.observe(targetNode, config);


})();