Greasy Fork

Greasy Fork is available in English.

网站助手

1、简化百度页面,去除广告;2、修改部分网站超链打开窗口为新窗口方式;3、Github搜索列表自动翻译,readme划词翻译

当前为 2018-12-14 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         网站助手
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  1、简化百度页面,去除广告;2、修改部分网站超链打开窗口为新窗口方式;3、Github搜索列表自动翻译,readme划词翻译
// @author       Yisin
// @require      https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js
// @match        *://www.baidu.com/*
// @match        *://www.google.com.hk/*
// @match        *://bbs.125.la/*
// @match        *://github.com/*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// ==/UserScript==

(function () {
    'use strict';

    $(window).on('load', function () {
        var start = function () {
            // 百度
            new BaiduHelper().listener();
            
            // Guthub
            new GitHubHelper().listener();

            // 超链接
            new HrefHelp().ready();
        };
        start();
    });

    /**
     * 百度助手
     */
    function BaiduHelper() {
        this.pageIndex = 1;
        this.first = '';
    }
    BaiduHelper.prototype = {
        listener: function(){
            var that = this;
            setInterval(function(){
                var href = document.location.href;
                if (href == 'https://www.baidu.com/') {
                    that.homeClearAd();
                }
                var t = $('.result.c-container .t:eq(0)').text();
                if(that.first != t){
                    that.first = t;
                    
                    setTimeout(function(){
                        that.pageIndex = 1;
                        that.clearAd();         
                    }, 200);
                }
            }, 200);
        },
        // 去广告
        homeClearAd: function () {
            $('#s_wrap').remove();
            document.body.style.overflow = 'hidden';
        },
        clearAd: function () {
            var href = document.location.href;
            if (href.substring(0, 24) == 'https://www.baidu.com/s?') {
                document.body.style.overflow = 'auto';
                $('#content_right,#rs,#page,#foot').remove();
                var cleard = function () {
                    $('#content_left > div').each(function () {
                        var that = $(this);
                        if (!that.hasClass('result') || that.find('.m').text() == '广告') {
                            that.remove();
                        }
                    });
                }
                setInterval(cleard, 200);
                cleard();
                this.autoLoadSearch();
                this.pageFormat();
            }
        },
        // 页面美化
        pageFormat: function () {
            var cw = document.body.clientWidth;
            cw = parseInt(cw / 3) - 22;
            var styleText = ".result.c-container{border:1px solid #dedede;margin: 5px;padding: 3px;height: 100px;float: left;overflow-y: auto;width: " + cw + "px;}";
            GM_addStyle('#container{width:100%;} #content_left{width: calc(100% - 10px);padding-left:0; padding-top:0;padding:5px;} ' + styleText);
        },
        // 自动加载搜索下一页
        autoLoadSearch: function () {
            var that = this;
            var kw = $('#kw').val();
            var num = that.pageIndex * 10;
            var url = formatByJson("https://www.baidu.com/s?wd={0}&pn={1}", {
                wd: kw, pn: num
            });       
            loadHtml(url, function (res) {
                try {
                    var $html = $(res);
                    if ($html) {                           
                        var items = $html.find('#content_left > div.result');
                        if (items && items.length) {
                            $('#content_left').append(items);
                            that.pageIndex++;
                            if (that.pageIndex < 20) {
                                that.autoLoadSearch();
                            }
                        }
                    }
                } catch (e1) {
                }
            });
        }
    };

    /**
     * GitHub助手
     */
    function GitHubHelper() {
        this.href = '';
    }
    GitHubHelper.prototype = {
        searchPage: function () {
            var href = document.location.href;
            if (href.substring(0, 26) == 'https://github.com/search?') {
                var $inpo = document.querySelector('input.header-search-input');
                if ($inpo && !$inpo.value) {
                    $inpo.value = 'search:start';
                }
                var $p = document.querySelectorAll('.repo-list .repo-list-item p.d-inline-block');
                if ($p && $p.length) {
                    var i1 = 0;
                    var startFY = function (i2) {
                        var text = $p[i2].innerText;
                        fanyi(text, function (res) {
                            if (res) {
                                i1++;
                                $p[i2].innerHTML = $p[i2].innerHTML + '<br><div style="color:red">' + res + '</div>';
                                if (i1 < $p.length) {
                                    startFY(i1);
                                }
                            }
                        });
                    }
                    startFY(i1);
                }
            }
        },
        projectPage: function () {
            var href = document.location.href;
            if (href.substring(0, 19) == 'https://github.com/') {
                var redme = document.querySelector('article.entry-content');
                redme.addEventListener('mouseup', function () {
                    var t1 = selectText();
                    if (t1 && !/^[\s]+$/g.test(t1)) {
                        fanyi(t1, function (res) {
                            if (res) {
                                var ___res = redme.querySelector('.____res');
                                if (!___res || ___res.className != '____res') {
                                    ___res = document.createElement("div");
                                    ___res.className = '____res';
                                    ___res.style.color = 'red';
                                    ___res.style.position = 'fixed';
                                    ___res.style.right = '10px';
                                    ___res.style.top = '245px';
                                    ___res.style.width = '400px';
                                    ___res.style.height = '600px';
                                    ___res.style.overflow = 'auto';
                                    redme.appendChild(___res);
                                }
                                ___res.innerText = res;
                            }
                        });
                    }
                });

                var span1 = document.querySelector('.repository-content span');
                var t2 = span1.innerText;
                if (t2 && t2.length > 10) {
                    var b2 = document.createElement("button");
                    b2.innerText = '翻译';
                    b2.addEventListener('click', function () {
                        fanyi(t2, function (res2) {
                            if (res2) {
                                span1.removeChild(b2);
                                span1.innerHTML = span1.innerHTML + '<br>' + res2;
                            }
                        });
                    });
                    span1.appendChild(b2)
                }
            }
        },
        listener: function(){
            var that = this;
            setInterval(function(){
                var href = document.location.href;
                if(that.href != href){
                    that.href = href;
                    that.searchPage();
                    that.projectPage();
                }
            }, 200);
        }
    }

    /**
     * 超链接部分
     */
    function HrefHelp() { }
    HrefHelp.prototype = {
        ready: function () {
            var eles = document.querySelectorAll('a');
            var host = document.location.host;
            if (eles && eles.length) {
                for (var i = 0; i < eles.length; i++) {
                    var a = eles[i];
                    if (a) {
                        if (host == "www.google.com.hk") {
                            a.target = "_blank";
                        } else if (host == "bbs.125.la") {
                            var f = "" + a.onclick;
                            if (f.includes('atarget(this)')) {
                                a.target = "_blank";
                                a.onclick = null;
                            }
                        } else if (host == "github.com" && a.className == 'v-align-middle') {
                            a.target = "_blank";
                        }
                    }
                }
            }
        }
    };

    function loadHtml(url, callback) {
        $.ajax({
            url: url,
            async: true,
            timeOut: 5000,
            type: "GET",
            dataType: "html"
        }).done(function (res) {
            callback(res);
        });
    }

    /**
     * 获取选择的文本
     */
    function selectText() {
        if (document.Selection) {
            //ie浏览器
            return document.selection.createRange().text;
        } else {
            //标准浏览器
            return window.getSelection().toString();
        }
    }

    /**
     * 翻译
     * @param {*} text 
     * @param {*} callback 
     */
    function fanyi(text, callback) {
        var call = "YoudaoFanyier.Instance.updateTranslate";
        var time = new Date().valueOf();
        var param = "type=data&only=on&doctype=jsonp&version=1.1&relatedUrl=http%3A%2F%2Ffanyi.youdao.com%2Fopenapi%3Fpath%3Dweb-mode%26mode%3Dfanyier&keyfrom=test&key=null&callback=" + call + "&q=" + encodeURIComponent(text) + "&ts=" + time;
        GM_xmlhttpRequest({
            method: 'GET',
            url: "http://fanyi.youdao.com/openapi.do?" + param,
            headers: { "Accept": "*/*", "connection": "Keep-Alive", "Content-Type": "charset=utf-8", "Referer": "http://fanyi.youdao.com/openapi?path=web-mode&mode=fanyier" },
            contentType: "application/json",
            dataType: 'json',
            onload: function (response) {
                if (response.statusText == 'OK') {
                    try {
                        var res = response.responseText;
                        if (/^YoudaoFanyier.Instance.updateTranslate/g.test(res)) {
                            res = res.substring(call.length + 1, res.length - 2);
                        }
                        res = JSON.parse(res);
                        if (callback && res.translation && res.translation.length) {
                            callback(res.translation[0]);
                        }
                    } catch (e) {
                        callback(response.statusText);
                    }
                } else {
                    callback(response.statusText);
                }
            }
        });
    }

    /**
     * 字符串格式化,json格式
     * @param {*} str 
     * @param {*} json 
     */
    function formatByJson(str, json) {
        if (json) {
            for (var key in json) {
                var exc = new RegExp('\{' + key + '\}', "g");
                str = str.replace(exc, json[key]);
            }
        }
        return str;
    }

    /**
     * 判断变量是否为对象
     * @param {*} obj 
     */
    function _isObject(obj) {
        return Object.prototype.toString.call(obj) === '[object Object]';
    }
})();