Greasy Fork

Greasy Fork is available in English.

搜索辅助小工具 - 淘宝,京东,百度,必应,谷歌,翻译等

在网页右上角增加快捷搜索功能,功能如下:1.如果选择了文本,那么搜索文本,2.如果在百度,bing,谷歌,淘宝,京东,翻译等搜索框下,则搜索搜索框关键字。3.默认当前页面替换,单弹页面请按ctrl/shift/cmd键组合

当前为 2019-04-03 提交的版本,查看 最新版本

// ==UserScript==
// @name         搜索辅助小工具 - 淘宝,京东,百度,必应,谷歌,翻译等
// @namespace    http://tampermonkey.net/
// @version      2.2
// @description  在网页右上角增加快捷搜索功能,功能如下:1.如果选择了文本,那么搜索文本,2.如果在百度,bing,谷歌,淘宝,京东,翻译等搜索框下,则搜索搜索框关键字。3.默认当前页面替换,单弹页面请按ctrl/shift/cmd键组合
// @require      https://unpkg.com/vue/dist/vue.js
// @require      https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js
// @author       Zszen
// @include     https://*
// @include     http://*
// @grant        none
// ==/UserScript==

//let config_isHidden = localStorage.getItem("config_isHidden")==null? false:localStorage.getItem("config_isHidden");
(function () {
  'use strict'
  setTimeout(function () {
    //console.log('config_isHidden',config_isHidden)
    let div = $('<div @mouseover="mouseover()" @mouseout="mouseout()" id="zszen" class="小工具z" v-bind:style="{height: heightVal, width: widthVal}" style="overflow:hidden; position:fixed; right:2%; top:6%; z-index:9999;width:115px;height:150px;border:2px solid #dfdfdf;background-color:#ffffff"></div>')
    $('body').append(div)
    let title = $('<h4 align="center" style="margin-bottom:0,margin-top:0;line-height:1;padding-left:0px;padding-top:0px;-webkit-margin-before:.3em;-webkit-margin-after:.3em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;" @click="onClickTitle()"><font style="font-size:14px;color:#336699">{{valueForTitle}}</font></h4>')
    div.append(title)
    let items = $('<label v-show="!hidden" style="text-align:center;align:center;padding-left:7px;padding-top:0px;color:#68ac10" v-for="item,index in infos" :key="index"><input type="button" v-on:click="onClick($event,index)" v-bind:value="item.title" style="width:90%"></button></label>')//<font style="font-size:12px;color:#68ac10">{{item.title}}</font>
    div.append(items)
    let intro = $('<label v-show="!hidden" style="width:100%"><font style="margin:auto;text-align:center;padding-left:5px;color:#dddddd;font-size:11px;display:inline-block;">自动搜索关键字/选区</font></label>')
    div.append(intro)
    // @ts-ignore
    let divApp = new Vue({
      el: '#zszen',
      data: {
        title: '小工具z',
        valueForTitle: '小工具z',
        hidden: true,
        widthVal:0,
        heightVal:0,
        infos: [
          {title: '百度搜索', link: "https://www.baidu.com/s?wd="},
          {title: 'Bing搜索', link: 'https://www.bing.com/search?q='},
          {title: 'Google搜索', link: 'https://www.google.com/search?q='},
          {title: '百度翻译', link: 'https://fanyi.baidu.com/#en/zh/'},
          {title: 'Google翻译', link: 'https://translate.google.cn/#view=home&op=translate&sl=en&tl=zh-CN&text='},
          {title: '淘宝搜索', link: 'https://s.taobao.com/search?q='},
          {title: '京东搜索', link: 'https://search.jd.com/Search?keyword='},
          {title:  '简书',  link: 'https://www.jianshu.com/search?q='},
          {title:  '知乎',  link: 'https://www.zhihu.com/search?type=content&q='},
          {title:  '维基百科',  link: 'https://zh.wikipedia.org/wiki/'}
        ]
      },
      computed:{
        isHidden:{
            set:function(val){
                //console.log(val)
                this.hidden = val;
                if(this.hidden){
                    this.valueForTitle= 'TOOLz';
                    this.heightVal = "25px";
                    this.widthVal = '53px';
                }else{
                    this.valueForTitle= this.title;
                    this.heightVal = "270px";
                    this.widthVal = '115px';
                }
            },
            get:function(val){
                return this.hidden;
            }
        }
      },
      methods: {
        mouseover:function(){
            this.isHidden = false;
        },
        mouseout:function(){
            this.isHidden = true;
        },
        onClick: function (evt, val) {
          //window.location.href = this.infos[val].link
            var str = window.location.href;
            var keyword = "";
            if(str.indexOf("www.baidu.com")!=-1){
                console.log("百度:");
                keyword = $('input.s_ipt').val();
            }else if(str.indexOf("fanyi.baidu.com")!=-1){
                keyword = $('textarea.textarea').val();
            }else if(str.indexOf("bing.com")!=-1){
                keyword = $('input.b_searchbox').val();
            }else if(str.indexOf("www.google.com")!=-1){
                keyword = $('input[role]').val();
            }else if(str.indexOf("translate.google.com")!=-1 || str.indexOf("translate.google.cn")!=-1){
                keyword = $('textarea#source').value;
            }else if(str.indexOf("taobao.com")!=-1){
                keyword = $('input.search-combobox-input').val();
            }else if(str.indexOf("jd.com")!=-1){
                keyword = $('input.text#key').val();
            }else if(str.indexOf("jianshu.com")!=-1){
                keyword = $('input.search-input').val();
            }else if(str.indexOf("zhihu.com")!=-1){
                keyword = $('input.Input#Popover5-toggle').val();
            }else if(str.indexOf("wikipedia.org")!=-1){
                var str1 = window.location.href;
                var arr1 = str1.split("/");
                keyword = arr1[arr1.length-1];
            }else{
                console.log("其他");
            }
            if(getSelectedText()!=''){
                keyword = getSelectedText();
            }
            //console.log(evt)
            if(evt.shiftKey || evt.ctrlKey || evt.metaKey){
                window.open(this.infos[val].link+keyword,"_blank");
            }else{
                window.open(this.infos[val].link+keyword,"_self");
            }
        },
        /*onClickTitle:function(){
            //console.log('click', !this.isHidden)
            this.isHidden = !this.isHidden;
        }*/
      }
    });
    //divApp.isHidden = !(config_isHidden==null || config_isHidden==false);
      divApp.isHidden = true;
  }, 1000)
  function getSelectedText() {
    if (window.getSelection) {
      return window.getSelection().toString();
    } else if (document.selection) {
      return document.selection.createRange().text;
    }
    return ''
  }
  // Your code here...
})()