Greasy Fork

来自缓存

Greasy Fork is available in English.

CHH站内搜索

CHH更好的站内搜索

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        CHH站内搜索
// @namespace   Violentmonkey Scripts
// @match       https://www.chiphell.com/*
// @grant       none
// @version     1.2
// @author      liansishen
// @description CHH更好的站内搜索
// @license           MPL-2.0
// ==/UserScript==

(function () {
	'use strict';
  var strMemo,strValue,localSearch;
  //获取前一次选择的搜索方式
  localSearch = localStorage.getItem('txtSearch');
  
  //站内搜索部分
  var form =  document.querySelector('#scbar > div:nth-child(2) > form');
  
  //添加下拉框
  var objSel = document.createElement("select"); 
  objSel.id = "selSearch";
	objSel.options.add(new Option("谷歌","google"));
  objSel.options.add(new Option("百度","baidu"));
  objSel.options.add(new Option("必应","bing"));
  objSel.onchange = function() {
    strMemo = " " + objSel.value + "站内搜索";
    if (txtSearch.value == '' | txtSearch.value == " bing站内搜索" | txtSearch.value==" google站内搜索" |  txtSearch.value== " baidu站内搜索") {txtSearch.value=strMemo}
  }
  //form.prepend(objSel)
  
  //搜索输入框显示内容修改
  var txtSearch = document.getElementsByName('q')[0];
  form.insertBefore(objSel,txtSearch);
  strMemo = " " + localSearch + "站内搜索";
  switch (localSearch) {
    case 'google':
      objSel.options[0].selected = true;
      break;
    case 'baidu':
      objSel.options[1].selected = true;
      break;
    case 'bing':
      objSel.options[2].selected = true;
      break;
    default:
      objSel.options[0].selected = true;
      break;
  }
  txtSearch.onfocus = function() {
    if(txtSearch.value==strMemo) {
      txtSearch.value='';
    }
  }
  txtSearch.onblur = function() {
    if (txtSearch.value=='') {
      txtSearch.value=strMemo;
    }
  }
  txtSearch.value=strMemo;
  
  //替换原搜索按钮的事件
  var button = document.getElementsByName('sa')[0]
  button.onclick = function (){
    strValue = txtSearch.value;
    if (strValue == '' | strValue ==" google站内搜索" |  strValue == " baidu站内搜索"|txtSearch.value == " bing站内搜索") {return false;}
    localStorage.setItem('txtSearch',objSel.value)
    switch (objSel.value) {
        case 'google':
          window.open('https://www.google.com/search?hl=zh-CN&client=opera&q=site:' + encodeURIComponent(location.hostname) +' ' + encodeURIComponent(strValue),'','');
          break;
        case 'baidu':
          window.open('https://www.baidu.com/s?wd= site:' + encodeURIComponent(location.hostname) + ' ' + encodeURIComponent(strValue),'','');
          break;
        case 'bing':
          window.open('https://cn.bing.com/search?q=site:' + encodeURIComponent(location.hostname) + ' ' + encodeURIComponent(strValue),'','');
          break;
        default:
          window.open('https://www.google.com/search?hl=zh-CN&client=opera&q=site:' + encodeURIComponent(location.hostname) +' ' + encodeURIComponent(strValue),'','');
          break;
    }
		
    return false;
	};
 
  
})();