change_style

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

目前為 2022-09-21 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

            // 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]);
        }, 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;
        }
    }
}