change_style

一些网站的配色方案非常不适合阅读,比如知乎专栏白色背景黑色字体,看一会就非常刺眼,故此写个脚本,方便以后遇到这种网站直接自动修改样式。

目前为 2022-09-29 提交的版本。查看 最新版本

// ==UserScript==
// @name         change_style
// @namespace    https://netoday.cn
// @version      0.1.10
// @description  一些网站的配色方案非常不适合阅读,比如知乎专栏白色背景黑色字体,看一会就非常刺眼,故此写个脚本,方便以后遇到这种网站直接自动修改样式。
// @author       crazy_pig
// @match        https://zhuanlan.zhihu.com/*
// @match        https://www.zhihu.com/*
// @match        https://blog.csdn.net/*
// @match        https://www.5axxw.com/*
// @match        https://www.baidu.com
// @match        https://www.baidu.com/
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license MIT
// ==/UserScript==

// default urls and style to use this script: 0=url,1=bgcolor,2=font color,3=font family, 4=btn names 2 click, 5=elements 2 remove by class, 6=div 2 maximum by classes(1) or by tag(2)
const _default_font_family = "gitbook-content-font,-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif, 微软雅黑";
const _url_array = [
    ["baidu.com", "", "",_default_font_family , "","","", ""],
    ["zhuanlan.zhihu.com", "#181C1F", "#EAF2F7",_default_font_family , "Modal-closeButton","css-1ynzxqw Recommendations-Main","Post-RichTextContainer Post-SideActions", "90%"],
    ["zhihu.com", "#181C1F", "#EAF2F7",_default_font_family , "Modal-closeButton","Question-sideColumn Question-sideColumn--sticky css-1qyytj7 css-1ynzxqw","List-item Question-mainColumn", "90%"],
    ["5axxw.com", "", "",_default_font_family , "","col-xl-auto ad_content_center answer-area bottom-ad","", ""],
    ["blog.csdn.net", "", "", "", "","blog_container_aside blog-footer-bottom more-toolbox-new recommend-box template-box recommend-right recommend-nps-box csdn-side-toolbar","main_father main container nodata", "100%"]
];

const URL_INDEX = 0;
const BGCOLOR_INDEX = 1;
const FNTCOLOR_INDEX = 2;
const FNTFML_INDEX = 3;
const BTN_INDEX = 4;
const DELETE_INDEX = 5;
const MAXIMUM_INDEX = 6;
const RESIZE_INDEX = 7;
const OP_MAXIMUM_BY_CLASSES = 1;
const OP_MAXIMUM_BY_TAG = 2;

(function() {
    'use strict';

    // get url user visited
    var _url = (window.location + "").toLowerCase();

    // if need active script
    var _active_index = -1;
    var i;
    for (i = 0; i < _url_array.length; i++){
        if (_url.indexOf(_url_array[i][URL_INDEX]) > 0){
            _active_index = i;
            break;
        }
    }

    if (_active_index >= 0){
        // set color
        _recursion_set_color(document.body,
                  _url_array[_active_index][BGCOLOR_INDEX],
                  _url_array[_active_index][FNTCOLOR_INDEX],
                  _url_array[_active_index][FNTFML_INDEX]);
        // remove mask div
        setInterval(function (){
            // BAIDU MOBILE
            _baidu_mobile(_url);

            // CSDN
            var csdnContentBox = document.getElementsByClassName("blog-content-box")[0];
            if (null != csdnContentBox && typeof(csdnContentBox) !== "undefined"){
                csdnContentBox.style.background = "#8DA399";
                var links = document.getElementsByTagName("a");
                for (i = 0; i < links.length; i++){
                    links[i].style.color = "#0014ff";
                }
            }
            var zhihuContentBox = document.getElementsByClassName("QuestionHeader-title")[1];
            if (null != zhihuContentBox && typeof(zhihuContentBox) !== "undefined"){
                zhihuContentBox.style.marginTop = "30px";
            }
            var axxwMaskDiv = document.getElementById("gzh-modal-wrap");
            if (null != axxwMaskDiv && typeof(axxwMaskDiv)!=="undefined"){
                axxwMaskDiv.parentNode.remove();
            }

            // click button
            var _element_array = _url_array[_active_index][BTN_INDEX].split(" ");
            var m,i,_btns;
            for(m=0;m<_element_array.length;m++){
                if (""!==_element_array[m].trim()){
                    _element_array[m] = _element_array[m].trim();
                    _btns = document.getElementsByClassName(_element_array[m]);
                    if(typeof(_btns) !== 'undefined'){
                        for(i=0;i<_btns.length;i++){
                            if('BUTTON' === _btns[i].tagName){
                                // click the `close` button to close the mask div
                                _btns[i].click();
                            }
                        }
                    }
                }
            }
            _btns = document.getElementById("passportbox");
            if(null !== _btns && typeof(_btns) !== 'undefined' && _btns.children.length > 0){
                _btns.children[1].click();
            }


            // remove elements by class name
            _element_array = _url_array[_active_index][DELETE_INDEX].split(" ");
            for(m=0;m<_element_array.length;m++){
                if (""!==_element_array[m].trim()){
                    _element_array[m] = _element_array[m].trim();
                    _btns = document.getElementsByClassName(_element_array[m]);
                    if(typeof(_btns) !== 'undefined'){
                        for(i=0;i<_btns.length;i++){
                            _btns[i].remove();
                        }
                    }
                }
            }

            // resize divs
            _resize_div(_url_array[_active_index][MAXIMUM_INDEX].split(" "), OP_MAXIMUM_BY_CLASSES, _url_array[_active_index][RESIZE_INDEX]);
            _resize_div(_url_array[_active_index][MAXIMUM_INDEX].split(" "), OP_MAXIMUM_BY_TAG, _url_array[_active_index][RESIZE_INDEX]);

            // open hidden divs
            var hiddenDivArray = document.getElementsByClassName("hide-preCode-bt");
            if (typeof(hiddenDivArray) !== "undefined" && hiddenDivArray.length > 0){
                for (i = 0; i < hiddenDivArray.length; i++){
                    hiddenDivArray[i].click();
                }
            }
        }, 1000);
    }

})();

function _baidu_mobile(_url){
    if(_url !== "https://www.baidu.com" &&
      _url !== "https://www.baidu.com/"){
        return;
    }
    var i;

    // delete elements by class
    var underSearchboxTips = $(".under-searchbox-tips");
    if (null !== underSearchboxTips &&
        typeof(underSearchboxTips) !== "undefined" &&
        underSearchboxTips.length > 0){

        for(i=0; i<underSearchboxTips.length; i++){
            underSearchboxTips[i].remove();
        }
    }

    // delete elements by id
    var hotsearchWrapper = $("#s-hotsearch-wrapper");
    if (null !== hotsearchWrapper &&
        typeof(hotsearchWrapper) !== "undefined"){
        hotsearchWrapper.remove();
        console.log("delete elements by id:s-hotsearch-wrapper finished");
    }
    var hotsearchData = $("#hotsearch_data");
    if (null !== hotsearchData &&
        typeof(hotsearchData) !== "undefined"){
        hotsearchData.remove();
        console.log("delete elements by id:hotsearch_data finished");
    }

    // process baidu.com for mobile
    // header div
    setInterval(function(){
    var navIndex = -1;
    var headerDiv = document.getElementById("header");
    headerDiv.style.height = $(window).height()+"px";
    headerDiv.style.backgroundColor = "black";
    var headerChildrenArray = headerDiv.childNodes;
    if (null !== headerChildrenArray &&
        typeof(headerChildrenArray) !== "undefined"){
        for(i=0; i<headerChildrenArray.length; i++){
            if (null !== headerChildrenArray[i] &&
                typeof(headerChildrenArray[i]) !== "undefined"){
                if(headerChildrenArray[i].id === "navs"){
                    navIndex = i;
                    continue;
                }
                if(navIndex >= 0){
                    headerChildrenArray[i].style.visibility = "hidden";
                }
            }
        }
    }
    }, 500);
    document.getElementById("index-form").style.borderColor = "rgb(116 116 116)";
    document.getElementById("index-bn").style.backgroundColor = "rgb(116 116 116)";
    $("#center-content-1").hide();
    $("#bottom").hide();
    $("#login-wraps").hide();
    $("#logo").hide();
}

function _resize_div(_div_names, _op, _resize_rate){
    var i,j;
    if(typeof(_div_names) !== 'undefined'){
        for(i=0;i<_div_names.length;i++){
            if(""!==_div_names[i]){
                var _elements;
                if (_op == 1){
                    _elements = document.getElementsByClassName(_div_names[i]);
                }else{
                    _elements = document.getElementsByTagName(_div_names[i]);
                }
                if(typeof(_elements) !== 'undefined'){
                    for(j=0;j<_elements.length;j++){
                        _elements[j].style.width = _resize_rate;
                    }
                }
            }
        }
    }
}

/**
 * set font \ background-color \ font-family
 */
function _recursion_set_color(parent, _bg_color, _fnt_color, _fnt_family){
    if (typeof(parent.children) !== 'undefined'){
        if (parent.children.length > 0){
            var i;
            for(i=0;i<parent.children.length;i++){
                _recursion_set_color(parent.children[i], _bg_color, _fnt_color, _fnt_family);
            }
        }
        if (""!==_bg_color){
            parent.style.backgroundColor = _bg_color;
        }
        if (""!==_fnt_color){
            parent.style.color = _fnt_color;
        }
        if (""!==_fnt_family){
            parent.style.fontFamily = _fnt_family;
        }
    }
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址