change_style

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

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

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         change_style
// @namespace    https://netoday.cn
// @version      0.1.9
// @description  一些网站的配色方案非常不适合阅读,比如知乎专栏白色背景黑色字体,看一会就非常刺眼,故此写个脚本,方便以后遇到这种网站直接自动修改样式。
// @author       crazy_pig
// @match        https://zhuanlan.zhihu.com/*
// @match        https://www.zhihu.com/*
// @match        https://blog.csdn.net/*
// @match        https://www.5axxw.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 = [
    ["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 (){
            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 _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;
        }
    }
}