Greasy Fork

Greasy Fork is available in English.

万能页内选中搜索【失效联系作者24小时更新】

任何页面选中以后都可以快捷搜索,实现不跳出网页浏览进行搜索,目前支持必应搜索。

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         万能页内选中搜索【失效联系作者24小时更新】
// @namespace    http://tampermonkey.net/
// @version      1.0.2
// @description  任何页面选中以后都可以快捷搜索,实现不跳出网页浏览进行搜索,目前支持必应搜索。
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @author       蜡小新
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=greasyfork.org
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    var content = "";
    $("body").bind('mouseup', function(e) {
        processSelection(e);
    });

    function processSelection(e) {
        var text = getSelectedText();
        if (!!text && text != "" && text.length != 0) {
            showBubble(e, text);
        } else {
            hideBubble();
        }
    }

    function getSelectedText() {
        var text = "";
        if (window.getSelection) {
            text = window.getSelection()
                .toString();
        } else if (document.selection && document.selection.type != "Control") {
            text = document.selection.createRange()
                .text;
        }
        return text;
    }


    function showBubble(e, text) {
        content = text;
        if($('#any-searcher-bubble').length == 0) {
            $("body").append("<div id='any-searcher-bubble' style='color: white;background-color: rgb(41 115 236);width: 110px;height: 31px;padding: 0 4px 0 11px;position:absolute;font-size: 15px;line-height: 31px;border-radius: 3px;cursor: pointer;font-weight:bold;'>点我页内搜索</div>");
            $("#any-searcher-bubble").click(function(e) {
                if($("#any-searcher-iframe-container").length ==0) {
                     $("body").append("<div id='any-searcher-iframe-container' style='width: 500px;height: 300px;position:absolute;border-radius: 2px;border: 1px solid #d3d3d3;'>"+
                                 "<div style='position:absolute;cursor: pointer;top:-22px;left:0px;font-size: 12px;' id='any-searcher-iframe-x'>X</div>"+
                                 "<div style='position:absolute;cursor: pointer;top:-22px;left:20px;font-size: 12px;width:50px;' id='any-searcher-iframe-reset'>Reset</div>"+
                                 "<div style='position:absolute;cursor: pointer;top: -22px;left: 61px;font-size: 12px;width:50px;' id='any-searcher-iframe-left'>Left</div>"+
                                 "<div style='position:absolute;cursor: pointer;top: -22px;left: 91px;font-size: 12px;width:50px;' id='any-searcher-iframe-right'>Right</div>"+
                                 "<iframe id='any-searcher-iframe' style='width:100%;height:100%' src='https://cn.bing.com/search?q="+content+"'></iframe></div>");
                    $("#any-searcher-iframe-x").click(function(e){
                        $("#any-searcher-iframe-container").hide();
                    });
                    $("#any-searcher-iframe-reset").click(function(e){
                        $('#any-searcher-iframe').attr('src', $('#any-searcher-iframe').attr('src'));
                    });
                    $("#any-searcher-iframe-left").click(function(e){
                        $('#any-searcher-iframe-container').hide();
                        $('#any-searcher-iframe-container').css('left',"10px");
                        $('#any-searcher-iframe-container').show();
                    });

                    $("#any-searcher-iframe-right").click(function(e){
                        $('#any-searcher-iframe-container').hide();
                        $('#any-searcher-iframe-container').css('right',"10px");
                        $('#any-searcher-iframe-container').css('left',"");
                        $('#any-searcher-iframe-container').show();
                    });

                    $('#any-searcher-iframe-container').css('left',"10px");
                }
                $('#any-searcher-iframe-container').css('top', e.pageY - 40 + "px");
                $('#any-searcher-iframe-container').show();
                e.stopPropagation();
            });

        }
        $('#any-searcher-bubble').css('top', e.pageY - 40 + "px");
        $('#any-searcher-bubble').css('left', e.pageX + 20 + "px");
        $('#any-searcher-bubble').show();
    }

    function hideBubble() {
        $('#any-searcher-bubble').hide();
    }

    function makeASelf(){
        var elements = $("a");
        for(var i = 0 ; i < elements.length; i++) {
            $(elements[i]).attr("target","_self")
        }
    }
    window.setInterval(makeASelf, 500);
})();