// ==UserScript== // @name 网站助手 // @namespace http://tampermonkey.net/ // @version 1.3 // @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 // @downloadURL none // ==/UserScript== (function () { 'use strict'; $(window).on('load', function () { var start = function () { var host = document.location.host; // 百度 if(host == 'www.baidu.com'){ new BaiduHelper().listener(); } else if(host == 'github.com'){ // 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,#foot').remove();//#page, 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('#page{display:none;}#container{width:100%;} #content_left{width: calc(100% - 10px);padding-left:0; padding-top:0;padding:5px;} ' + styleText); }, // 自动加载搜索下一页 autoLoadSearch: function () { var that = this; var loadstatus = false; var max = $('#page a').length - 1; var startload = function () { loadstatus = true; 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) { loadstatus = false; 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($('body').height() < $(window).height() && that.pageIndex < max && that.pageIndex < 4){ startload(); } } } } catch (e1) { } }); } startload(); $(window).unbind('scroll').scroll(function (e, i) { if ($(window).scrollTop() + $(window).height() >= $(document).height() - 10) { if (!loadstatus) { startload(); } } }); } }; var githubcss = ` .container-lg { max-width: 10120px; } .paginate-container { float: left; width: 100%; } .float-left { width: 210px; } .codesearch-results { width: calc(100% - 210px); } .repo-list-item { width: 530px; height: 250px; float: left; padding: 5px; overflow-y: auto; overflow-x: hidden; } .repo-list-item > div:nth-child(1) { width: calc(100% - 130px) !important; } .repo-list-item > div:nth-child(1) p { width: 100% !important; } .repo-list-item .d-flex { width: 130px !important; display: block !important; float: left; } .repo-list-item .d-flex > div { float: left !important; width: 230px !important; height: 30px !important; text-align: left !important; } .paginate-container { display: none !important; } `; /** * GitHub助手 */ function GitHubHelper() { this.first = ''; this.pageIndex = 2; this.itemw = 530; } GitHubHelper.prototype = { searchPage: function () { var href = document.location.href; if (href.substring(0, 26) == 'https://github.com/search?') { var $inpo = $('input.header-search-input'); if ($inpo && !$inpo.val()) { $inpo.val('search:start'); } this.searchFanyi(); } }, searchFanyi: function () { var that = this; var $p = $('.repo-list .repo-list-item p.d-inline-block'); if ($p && $p.length) { var i1 = 0; var startFY = function (i2) { if($p[i2].fy){ i1++; startFY(i1); return; } var text = $p[i2].innerText; fanyi(text, function (res) { if (res) { i1++; $p[i2].innerHTML = $p[i2].innerHTML + '
' + res + '
'; $p[i2].fy = true; if (i1 < $p.length) { startFY(i1); } } }); } startFY(i1); } $('.repo-list .repo-list-item').css('width', that.itemw); }, autoLoad: function () { $('.pagination .previous_page, .pagination .next_page').remove(); var that = this; var alist = $('.pagination a'); var max = alist.length + 1; var href = document.location.href; var p = href.match(/&p=[0-9]+/g)[0]; var loadstatus = false; var startload = function () { if (that.pageIndex > max) { return; } loadstatus = true; var url = href.replace(p, '&p=' + that.pageIndex); loadHtml(url, function (res) { loadstatus = false; try { var $html = $(res); if ($html) { var items = $html.find('.repo-list .repo-list-item'); if (items && items.length) { $('.repo-list').append(items); that.pageIndex++; that.searchFanyi(); } } } catch (e1) { } }); }; $(window).unbind('scroll').scroll(function (e, i) { if ($(window).scrollTop() + $(window).height() >= $(document).height() - 60) { if (!loadstatus) { startload(); } } }); var ch = document.body.clientWidth - 226; if(ch){ var ih = (ch - 32) / 3; if(ih < 500){ ih = (ch - 32) / 2; if(ih < 500){ ih = (ch - 32); } } that.itemw = ih; } }, projectPage: function () { var href = document.location.href; if (href.substring(0, 19) == 'https://github.com/') { var redme = $('article.entry-content'); redme.on('mouseup', function () { var t1 = selectText(); if (t1 && !/^[\s]+$/g.test(t1)) { fanyi(t1, function (res) { if (res) { var ___res = redme.find('.____res'); if (!___res || ___res.className != '____res') { ___res = $("div"); ___res.addClass('____res'); ___res.css({ color: 'red', position: 'fixed', right: '10px', top: '245px', width: '400px', height: '600px', overflow: 'auto' }); redme.append(___res); } ___res.text(res); } }); } }); var span1 = $('.repository-content span'); var t2 = span1.text(); if (t2 && t2.length > 10) { var b2 = document.createElement("button"); b2.innerText = '翻译'; b2.addEventListener('click', function () { fanyi(t2, function (res2) { if (res2) { span1[0].removeChild(b2); span1.append('
' + res2); } }); }); span1[0].appendChild(b2) } } }, listener: function () { var that = this; setInterval(function () { var t = $('.repo-list .repo-list-item a:eq(0)').text(); if (that.first != t) { that.first = t; that.searchPage(); that.projectPage(); } }, 200); that.autoLoad(); GM_addStyle(githubcss); } } /** * 超链接部分 */ 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]'; } })();