Baidu Search Beautify

顶部工具栏毛玻璃效果、底部分页栏悬浮、搜索结果添加边框、侧边栏开关、搜索结果多列(设置中可关闭)、隐藏工具栏、隐藏分页并自动加载下一页

Versión del día 04/09/2022. Echa un vistazo a la versión más reciente.

// ==UserScript==
// @name         Baidu Search Beautify
// @name:zh-CN   百度搜索美化
// @namespace    baidu_beautify
// @version      3.2
// @description  顶部工具栏毛玻璃效果、底部分页栏悬浮、搜索结果添加边框、侧边栏开关、搜索结果多列(设置中可关闭)、隐藏工具栏、隐藏分页并自动加载下一页
// @description:zh-CN  顶部工具栏毛玻璃效果、底部分页栏悬浮、搜索结果添加边框、侧边栏开关、搜索结果多列(设置中可关闭)、隐藏工具栏、隐藏分页并自动加载下一页
// @author       Xavier Wong
// @run-at       document-start
// @match        https://www.baidu.com/
// @match        https://www.baidu.com/s*
// @grant        GM_addStyle
// @grant    GM_getValue
// @grant    GM_setValue
// ==/UserScript==

// 是否展示工具栏
var isToolShowed = GM_getValue('isToolShowed', true);
// 是否单列
var isSingleCol = GM_getValue('isSingleCol', false);
// 是否展示侧栏
var isSideShowed = GM_getValue('isSideShowed', false);
// 是否展示分页
var isPageShowed = GM_getValue('isPageShowed', true);

function customStyle(){

    GM_addStyle(
        // 顶栏样式
        '#s_tab{border-bottom: #e0e0e0 1px solid;background-color: rgba(248,248,248,0.4) !important; overflow: hidden !important}' +
        '.s_tab_inner{background-color: rgba(248,248,248,0.4) !important}' +
        '#head {background-color: rgba(248,248,248,0.4)}' +
        '.s_form {backdrop-filter: blur(10px);}' +
        '#form > span.bg.s_ipt_wr.quickdelete-wrap {background-color: white}' +
        //
        '#container.sam_newgrid {width:calc(100% - 140px);margin-left:140px;margin-bottom:45px}' +
        '#content_left{width:auto !important;margin-left:0px !important;padding-left:0px !important;flex-wrap:wrap ;float: none !important}' +
        // 隐藏相关搜索&其他人在搜
        '#rs_new, [tpl=recommend_list], [tpl=app\\/search-tool], [tpl=app\\/footer]{display: none}' +
        // 搜索结果样式
        '#content_left>div{margin-right:44px !important;margin-bottom:20px !important;width:560px !important;border-radius:12px !important;padding:16px !important;-webkit-box-shadow: 0 2px 5px 0 rgba(0,0,0,.1) !important;}' +
        '#content_left>table{margin-right:44px !important;margin-bottom:20px !important;border-radius:12px !important;-webkit-box-shadow: 0 2px 5px 0 rgba(0,0,0,.1) !important;}' +
        '#content_left>table>tbody>tr>td{padding:16px !important}' +
        '#content_left>table>tbody>tr>td>div{width:560px !important;-webkit-box-shadow:unset !important;border: none !important;padding: 0 !important}' +
        '.c-border{-webkit-box-shadow:unset !important;margin-bottom: 0 !important;border: none !important}' +
        // 侧栏样式
        "#content_right{display:block;padding-right:100px !important;float: right !important;}" +
        '#sideControl{padding:0 15px;position: absolute;width: auto;height: 35px;background-color: rgb(78, 110, 242);right: 0px;top: 30px;border-radius: 8px 0px 0px 8px;cursor: pointer;color: white;line-height: 35px;text-align: center}' +
        // 分页栏样式
        '#page{position:fixed;bottom:0;width:100%;}' +
        '#page > div{padding: 5px 15px !important; margin: 5px 0 5px 140px !important;}'
    );

GM_addStyle(`
@-webkit-keyframes fadeInRight {
	0% {
		opacity: 0;
		-webkit-transform: translateX(100%);
	}
	100% {
		opacity: 1;
		-webkit-transform: translateX(0);
	}
}
@-webkit-keyframes fadeOutRight {
	0% {
		opacity: 0;
		-webkit-transform: translateX(0);
        visibility: visible;
	}
	100% {
		opacity: 1;
		-webkit-transform: translateX(100%);
        visibility: visible;
	}
}

@keyframes fadeInRight {
	0% {
		opacity: 0;
		transform: translateX(100%);
	}
	100% {
		opacity: 1;
		transform: translateX(0);
	}
}

@keyframes fadeOutRight {
	0% {
		opacity: 0;
		transform: translateX(0);
        visibility: visible;
	}
	100% {
		opacity: 1;
		transform: translateX(100%);
        visibility: visible;
	}
}

@keyframes labelSpringOut {
	0% {
		opacity: 1;
        width: 30px;
	}
    50% {
		opacity: 1;
        width: 100px;
    }
	100% {
		opacity: 1;
        width: 80px;
	}
}
`);
}

const hideLabel = '>',
      showLabel = '<',
      hideFullLabel = '隐藏侧栏&nbsp;&nbsp;>',
      showFullLabel = '展示侧栏&nbsp;&nbsp;<';
function sideControl(){
    if(!isSingleCol){
        return false;
    }
    let toggle = document.createElement('div');
    toggle.id = 'sideControl';
    toggle.innerHTML = isSideShowed?hideLabel:showLabel;
    toggle.onmouseenter = function(){
        toggle.innerHTML = isSideShowed?hideFullLabel:showFullLabel;
    }
    toggle.onmouseout = function(){
        toggle.innerHTML = isSideShowed?hideLabel:showLabel;
    }
    toggle.onclick = function(){
        toggle.innerHTML = isSideShowed?hideFullLabel:showFullLabel;
        if(isSideShowed){
            GM_addStyle("#content_right{-webkit-animation:'fadeOutRight' 1s 1;visibility: hidden}");
        }else{
            GM_addStyle("#content_right{-webkit-animation:'fadeInRight' 1s 1;visibility: visible}");
        }
        isSideShowed = !isSideShowed;
        GM_setValue('isSideShowed', isSideShowed);
    }

    if(!document.getElementById('sideControl')){
        document.getElementById('container').appendChild(toggle);
    }
}

function sideInitiate(settingItem1, settingItem2, settingItem3){
    if(isSingleCol){
        settingItem1.innerHTML = '<span class="set">多列排列</span>';
        GM_addStyle('#content_left{display: grid; !important}');
        if(isSideShowed){
            GM_addStyle("#content_right{display:block;visibility: visible}");
        }else{
            GM_addStyle('#content_right{display:block;visibility: hidden}');
        }
    }else{
        settingItem1.innerHTML = '<span class="set">单列排列</span>';
        GM_addStyle('#content_left{display: flex; !important}' +
                    '#content_right{display:none}');
    }
    if(isToolShowed){
        settingItem2.innerHTML = '<span class="set">隐藏分类</span>';
        GM_addStyle('#s_tab{height: 38px !important;');
    }else{
        settingItem2.innerHTML = '<span class="set">显示分类</span>';
        GM_addStyle('#s_tab{height: 11px !important;');
    }
    if(isPageShowed){
        settingItem3.innerHTML = '<span class="set">隐藏分页</span>';
        GM_addStyle('#page{visibility: visible');
    }else{
        settingItem3.innerHTML = '<span class="set">显示分页</span>';
        GM_addStyle('#page{visibility: hidden');
    }
    settingItem1.onclick = function(){
        isSingleCol = !isSingleCol;
        GM_setValue('isSingleCol', isSingleCol)
    }
    settingItem2.onclick = function(){
        isToolShowed = !isToolShowed;
        GM_setValue('isToolShowed', isToolShowed)
    }
    settingItem3.onclick = function(){
        isPageShowed = !isPageShowed;
        GM_setValue('isPageShowed', isPageShowed)
    }
}

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

var currentUrl = document.location.href;
var currentPage = getUrlParam("pn");
function loadNextPage(){
    console.log(currentUrl)
    console.log(currentPage)
    var xhr = new XMLHttpRequest();
    var nextPageUrl;
    var nextPage;
    if(currentPage == null){
        currentPage = 0;
        nextPage = 10;
        if(document.querySelectorAll("#page .n").length===2){
            nextPageUrl = document.querySelectorAll("#page .n")[1].href;
        }else{
            nextPageUrl = document.querySelectorAll("#page .n")[0].href;
        }
    }else{
        nextPage = currentPage + 10;
        nextPageUrl = currentUrl.replace('&pn='+currentPage, '&pn='+nextPage)
    }
    xhr.open("GET", nextPageUrl);
    xhr.send();
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4 && xhr.status === 200){
            var parser = new DOMParser();
            var doc = parser.parseFromString(xhr.responseText, "text/html");
            var results = doc.querySelectorAll('.result');
            for (var i = 0; i < results.length; i++) {
                document.querySelector('#content_left').appendChild(results[i])
            }
        }
    }
    currentUrl = nextPageUrl;
    currentPage= nextPage;
}

function scrollToTopF(){
    window.scrollTo({
        left: 0,
        top: 0,
        behavior: 'smooth'
    });
}

(function() {
    'use strict';
    if(window.location.href !== 'https://www.baidu.com/'){
        customStyle();
    }else{
        GM_addStyle('#head_wrapper{width:unset !important}' +
                    'body{min-width:unset !important}');
    }
    var settingItem1 = document.createElement("a");
    settingItem1.href='';
    var settingItem2 = document.createElement("a");
    settingItem2.href='';
    var settingItem3 = document.createElement("a");
    settingItem3.href='';
    var scrollToTop = document.createElement("div");
    scrollToTop.id = 'scrollToTop';
    scrollToTop.innerHTML = '<div style="z-index: 999;position: fixed;left: 50px;bottom: 50px;border-radius: 10px; background: #dcdcdc8a;cursor: pointer;padding: 0 10px;font-size: 30px">🔝</div>';
    scrollToTop.onclick = scrollToTopF;
    var splitLine = document.createElement('span');
    splitLine.style.margin = '8px 16px';
    splitLine.style.background = '#d7d9e0';
    splitLine.style.height = '1px';
    splitLine.style.display = 'block';

    sideInitiate(settingItem1, settingItem2, settingItem3);
    window.onload = function(){
        // 添加观察者,监控样式变化
        var observer = new MutationObserver(function() {
            GM_addStyle('#scrollToTop{visibility: hidden');
            document.getElementsByTagName('body')[0].appendChild(scrollToTop);
            // console.log("changed")
            currentUrl = document.location.href;
            currentPage = getUrlParam("pn");
            customStyle();
            sideControl();
            sideInitiate(settingItem1, settingItem2, settingItem3);
            let pf = document.getElementsByClassName('pf')[0];
            pf.onmouseover = function(){
                let settings = document.getElementsByClassName('bdpfmenu')[0];
                settings.appendChild(splitLine);
                settings.appendChild(settingItem1);
                settings.appendChild(settingItem2);
                settings.appendChild(settingItem3);
                pf.onmouseover = null;
            }
            let ec = document.querySelectorAll('.EC_result');
            for(let idx=0;idx<ec.length;idx++){
                ec[idx].parentElement.parentElement.parentElement.style.setProperty('display', 'none');
            }
        })
        observer.observe(document.querySelector('#wrapper'), {
            attributes: true
        });
        if(window.location.href == 'https://www.baidu.com/'){
            return false;
        }
        sideControl();
        let pf = document.getElementsByClassName('pf')[0];
        pf.onmouseover = function(){
            let settings = document.getElementsByClassName('bdpfmenu')[0];
            settings.appendChild(splitLine);
            settings.appendChild(settingItem1);
            settings.appendChild(settingItem2);
            settings.appendChild(settingItem3);
            pf.onmouseover = null;
        }
        let ec = document.querySelectorAll('.EC_result');
        for(let idx=0;idx<ec.length;idx++){
            ec[idx].parentElement.parentElement.parentElement.style.setProperty('display', 'none');
        }
    }
    window.onchange = function(){
        let ec = document.querySelectorAll('.EC_result');
        for(let idx=0;idx<ec.length;idx++){
            ec[idx].parentElement.parentElement.parentElement.style.setProperty('display', 'none');
        }
    }
    window.onscroll = function(){
        if(!isPageShowed) {
            let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
            let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
            // console.log("st:"+scrollTop+";ch:"+clientHeight+";sh:"+scrollHeight)
            if(scrollTop <= 200) {
                GM_addStyle('#scrollToTop{visibility: hidden');
            }
            if(scrollTop > 200) {
                GM_addStyle('#scrollToTop{visibility: visible');
            }
            if((scrollHeight > clientHeight) && (scrollTop + clientHeight >= scrollHeight)) {
                loadNextPage();
            }
        }
    }
})();