// ==UserScript== // @name AC-baidu的搜索引擎样式再优化,对视觉中心优化-------请额外下载AC-baidu,此功能在AC-baidu基础上进行样式优化. // @namespace http://tampermonkey.net/ // @version 0.5 // @description 百度第一页查询csdn橙色,知乎蓝色,豆瓣绿色,百度灰色 // @author 白水 // @match https://www.baidu.com/s?* // @match https://www.google.*/search?* // @grant none // @require https://code.jquery.com/jquery-3.5.1.js // @downloadURL none // ==/UserScript== //--------------常用封装-------------------- //写入JQuery 3.5.1 //(function(url) { document.body.appendChild(document.createElement('script')).src = url; })("https://code.jquery.com/jquery-3.5.1.js"); //var $;//不能写入 //--------------定义-------------- var host = window.location.host, //host href = window.location.href; //href /** get 指定JS querySelector的对象,返回一个对象 * @param {*} querySelector JS 选择器 */ function getByQuerySelector(querySelector) { if (!document.querySelector(querySelector)) return false; return document.querySelector(querySelector); } /** get 指定id属性值的对象,返回一个对象 * @param {*} Id */ function getById(Id) { if (!document.getElementsById(Id)) return false; return document.getElementsById(Id); } /**get 指定TagName的对象 ,返回一个数组 * @param {*} TagName 指定tag的对象 */ function getByTagNameArr(TagName) { if (!document.getElementsByTagName(TagName)) return false; return document.getElementsByTagName(TagName); } /** get 指定Name属性的对象,返回一个数组 * @param {*} Name 指定Name的对象 */ function getByNameArr(Name) { if (!document.getElementsByName(Name)) return false; return document.getElementsByName(Name); } /** get 指定ClassName的对象,返回一个数组//仅仅缩写 * @param {*} ClassName 类名称 */ function getByClassNameArr(ClassName) { if (!document.getElementsByClassName(ClassName)) return false; //几乎百分百不会触发 return document.getElementsByClassName(ClassName); } /** get 筛选指定ClassName,利用getByClassNameArr数组返回一个数组 * @param {*} ClassName 类名称 */ function getClassUniqueArr(ClassName) { var oElements = getByClassNameArr(ClassName), boxArr = new Array(); for (var i = 0, len = oElements.length; i < len; i++) { if (oElements[i].className == ClassName) { boxArr.push(oElements[i]); } } return boxArr; } function QRemove(querySelector) { if (!getByQuerySelector(querySelector)) return console.log(getByQuerySelector(querySelector) + "不存在"); return getByQuerySelector(querySelector).remove(); } function CRemove(ClassName) { if (getByClassNameArr(ClassName).length == 0) return console.log(getByClassNameArr(ClassName) + "不存在"); if (getByClassNameArr(ClassName).length == 1) return getByClassNameArr(ClassName)[0].remove(); //唯一 return console.log(getByClassNameArr(ClassName) + "不唯一"); } var getByArr = getByClassNameArr('edit-icon j-edit-link'); //编辑按钮 /** * * @param {*} getByArr getBy系列Arr */ function ArrRomve(getByArr) { if (getByArr.length == 0) return console.log(getByArr + "不存在"); //倒序遍历删除编辑按钮 for (var i = getByArr.length - 1; i >= 0; i--) { getByArr[i].remove(); } } /* Element.prototype.remove = function() { // 像那些属性节点,注释节点,文本节点等等根本不可能做父节点,所以可以说parentNode返回的一般都是父元素节点====一直没有判断这个节点是否存在 //if (!this) return false; //if (!this.parentNode) return false; this.parentNode.removeChild(this); //父元素节点里删除调用者 }; */ /* HTMLCollection.prototype.remove = function() { // 像那些属性节点,注释节点,文本节点等等根本不可能做父节点,所以可以说parentNode返回的一般都是父元素节点====一直没有判断这个节点是否存在 //if (this) return false; //HTMLCollection [] 必定存在 if (this.length == 0) return false; //不存在 //if (!this.parentNode) return false; this.parentNode.removeChild(this); //父元素节点里删除调用者 }; */ //--------------Function-------------- //--------------常用封装-------------------- (function() { 'use strict'; //运行逻辑 host.includes (t/f) func(){ (remove,ColorChange)} //搜索主页_主程序 function SearchIndex() { //百度搜索 function baidu() { getByQuerySelector("#container").style.padding = "110px"; //头部距离改为110px getByQuerySelector("#container > div.head_nums_cont_outer.OP_LOG.new_head_nums_cont_outer > div > div.nums.new_nums > span").style.color = "black"; //搜索数量改为黑色 QRemove("#s_tab"); //头部删除 QRemove("#foot"); //脚注去除 QRemove("#rs"); //相关搜索去除???去除之可能会出现不会自动翻页的情况 //保留百科/翻译/贴吧 function baiduArrRomve() { var getByArr = getByClassNameArr("result-op"); //推广全部删除 if (getByArr.length == 0) return console.log(getByArr + "不存在"); //倒序遍历删除编辑按钮 for (var i = getByArr.length - 1; i >= 0; i--) { if (getByArr[i].children[0].innerText.includes("百度百科")) continue; if (getByArr[i].children[0].innerText.includes("百度翻译")) continue; if (getByArr[i].children[0].innerText.includes("百度贴吧")) continue; getByArr[i].remove(); } } baiduArrRomve(); } //谷歌搜索 function google() { } //改变颜色 function ColorChange(childrenClassName, parentClassName) { var SearchUrlArr = [ { name: "CSDN|csdn", color: "rgb(255,140,0,0.5)" }, { name: "知乎|zhihu", color: "rgb(65,105,225,0.5)" }, { name: "博客园|cnblogs", color: "rgb(240,0,0,0.5)" }, { name: "豆瓣|douban", color: "rgb(46,139,87,0.5)" }, { name: "百度|baidu", color: "rgb(165,165,165,0.5)" }, { name: "腾讯|qq", color: "rgb(165,165,165,0.5)" }, { name: "搜狐|sohu", color: "rgb(165,165,165,0.5)" }, ]; //定义子元素集合 var c = getByClassNameArr(childrenClassName), p; for (var i = 0, ilen = c.length; i < ilen; i++) { //查找指定父元素 if (c[i].parentNode.parentNode.className === parentClassName) { p = c[i].parentNode.parentNode; //console.log("2"); } else if (c[i].parentNode.parentNode.parentNode.className === parentClassName) { p = c[i].parentNode.parentNode.parentNode; //console.log("3"); } else if (c[i].parentNode.parentNode.parentNode.parentNode.className === parentClassName) { p = c[i].parentNode.parentNode.parentNode.parentNode; //console.log("4"); } else { console.log("找不到父元素"); } //p.style.background = "rgb(255,255,255,0.5)" for (var j = 0, jlen = SearchUrlArr.length; j < jlen; j++) { if (RegExp(SearchUrlArr[j].name).test(c[i].text)) { p.style.background = SearchUrlArr[j].color; //console.log("j--1"); break; } if (j == jlen - 1) { //console.log("j--2"); p.style.background = "rgb(255,255,255,0.5)"; break; } } //console.log("j--3"); /* if (j == jlen) { //恒等于最后一次输出即为 j=jlen; //p.style.background = "rgb(255,255,255,0.5)"; } */ } } if (host.includes("baidu")) { baidu(); ColorChange("c-showurl c-color-gray", "result c-container new-pmd"); } else if (host.includes("google")) { google(); ColorChange(document.getElementsByClassName("g"), document.getElementsByClassName("g")); } else { console.log("不存在"); } } //主程序 延迟 //window.onload = function() { setTimeout(SearchIndex(), 0); }; //jQuery预载画面 $(document).ready(SearchIndex()); })();