Quick search & Go to Top -- Google

Add 'quick search' input and 'go to top' button for Google

当前为 2015-09-06 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Quick search & Go to Top -- Google
// @namespace   feifeihang.info
// @description Add 'quick search' input and 'go to top' button for Google
// @include     https://www.google.*
// @include     http://www.google.*
// @version     5
// @grant       none
// ==/UserScript==
(function (window, document, undefined) {
  var container = document.createElement('div');
  container.style = 'display: none; position: fixed; bottom: 10px;' +
              'right: 10px; text-align: right;' +
              'width: 260px; height: 40px; background: #343C45; z-index: 999999999;' +
              'opacity: 0.8;';

  
  // create quick query input.
  var input = document.createElement('input');
  input.tyle = 'text';
  input.value = document.querySelector('#lst-ib').value;
  input.setAttribute('placeholder', 'Search...');
  input.style = 'border: none; border-left: solid #EA4335 5px; flex: 1; display: inline-block; outline: none; height: 40px;' +
    'font-size: 15px; padding: 0; padding-left: 10px; padding-right: 10px; background: #D9D9D9;';
  container.appendChild(input);
  
  // bind keypress-enter event.
  input.addEventListener('keypress', function (evt) {
    if (evt.keyCode === 13) {
      var value = input.value.trim() || '';
      if (value !== '') {
        document.querySelector('#lst-ib').value = value;
        document.querySelector('.lsb').click();
      }
    }
  }, false);
  
  // create the goto-top button.
  var btn = document.createElement('div');
  btn.id = 'goto-top-btn';
  btn.innerHTML = 'TOP';
  btn.onclick = gotoTop;
  
  // set button CSS style.
  btn.style = 'display: inline-block; color: #fff; line-height: 42px; text-align: center;' +
              'width: 40px; height: 40px; background: #4285F4;' +
              'cursor: pointer; font-weight: bolder;';
  
  // append the go-to-top to search form to successfully attach to the UI.
  container.appendChild(btn);
  
  var form = document.querySelector('#searchform');
  form.appendChild(container);
  
  window.onload = function () {
    var doc = document.documentElement;
    var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
    if (top !== 0) {
      container.style.display = 'flex';
    }
  }
  
  // bind button hiden/show event.
  window.onscroll = function() {
    var doc = document.documentElement;
    var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
    if (top === 0) {
      container.style.display = 'none';
    } else {
      container.style.display = 'flex';
    }
  }
  
  function gotoTop() {
    goto(Math.floor(window.pageYOffset / 5));
  }
  
  function goto(step) {
    setTimeout(function () {
      window.scrollTo(0, window.pageYOffset - step);
      if (window.pageYOffset <= 0) return;
      goto(step);
    }, 100);
 
  }
  
})(window, document, undefined);