DeliciousGarfield / BaiduPlus

// ==UserScript==
// @license MIT
// @name         BaiduPlus
// @version      0.1
// @description  使用百度搜索时同时展示谷歌的搜索结果
// @author       deliciousgarfield
// @match  *://www.baidu.com/*
// @match  *://www.google.com.hk/*
// @grant        GM_openInTab
// @grant        GM_getTabs
// @grant        GM_getTab
// @grant        GM_saveTab
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_addValueChangeListener
// @grant        GM_log
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @run-at document-start
// ==/UserScript==

(function () {
  'use strict';

  $(document).ready(function () {
    $('#su').click(function () {
      var kw = $('#kw').val();
      GM_setValue('kw', kw);
    });

    GM_addValueChangeListener('kw', function (name, old_value, new_value, remote) {
      if (!remote) {
        GM_getTabs(function (tabs) {
          var google_opened = false;
          for (var i in tabs) {
            if (tabs[i].hostname == 'www.google.com.hk') {
              google_opened = true;
              break;
            }
          }

          if (!google_opened) {
            GM_openInTab("https://www.google.com.hk/search?q=" + new_value);
          }
        });
      }
      else {
        location.href = "https://www.google.com.hk/search?q=" + new_value;
      }
    });

    GM_getTab(function (tab) {
      tab.hostname = location.hostname;
      GM_saveTab(tab);
    });
  });
})();