High Contrast Theme for E-Ink Displays and page down

Apply a high contrast black and white theme to all websites for better readability on e-ink displays

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         High Contrast Theme for E-Ink Displays and  page down
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Apply a high contrast black and white theme to all websites for better readability on e-ink displays
// @author       Your Name
// @match        *://*/*
// @grant        none
// @license  MIT
// @run-at       document-body
// ==/UserScript==

(function () {
    'use strict';

    // 设置高对比度的黑白主题
    var css = document.createElement("style");
    css.type = "text/css";
    css.innerHTML = `
            * {
                color: #000000 !important;
            }
            a, a * {
                color: #000000 !important;
            }
            input,select,option,button,textarea {
                color: #000000 !important;
            }
            input: focus,select: focus,option: focus,button: focus,textarea: focus,input: hover,select: hover,option: hover,button: hover,textarea: hover {
                color: #000000 !important;
            }
            input[type=button],input[type=submit],input[type=reset],input[type=image] {
                color: #000000 !important;
            }
    `;
    document.head.appendChild(css);


    // ========================== 翻页 ===================================
    const body = document.body;
    const positionCSS = "bottom:0;right:0";
    const sizeCSS = "width:15vmin;height:30vmin";
    const scrollRatio = 0.9;

    const Container = document.createElement("div");
    Container.style.cssText = `
        ${sizeCSS};
        ${positionCSS};
        position:fixed;
        display:flex;
        flex-direction:column;
        border-style:dashed;
        z-index:2147483647;
        border-width:2px;
    `;

    // 创建一个数组来存储具有overflow-y: auto的元素
    let elementsWithOverflowYAuto = [window];
    let el = window;
    // 设置一个标志变量来记录是否滚动
    let scrolled = false;
    let index = -1;

    init();

    function init() {
        // 创建一个数组来存储具有overflow-y: auto的元素
        elementsWithOverflowYAuto = [window];
        el = window;
        // 设置一个标志变量来记录是否滚动
        scrolled = false;
        index = -1;


        // 确认滚动元素
        setTimeout(() => {
            check();
        }, 80);

    }

    function check() {
        if (scrolled) {
            return;
        }
        index++;
        if (index == 1) {
            tryFindOverflowY();
        }
        if (index >= elementsWithOverflowYAuto.length) {
            el = window;
            return;
        }
        el = elementsWithOverflowYAuto[index];
        // 监听滚动事件
        el.addEventListener('scroll', function () {
            scrolled = true;
        });

        // 执行滚动操作
        el.scrollBy(0, 1);
        setTimeout(() => {
            check();
        }, 30);

    }


    function tryFindOverflowY() {
        // 获取所有元素.
        var allElements = document.querySelectorAll('*');
        let arr = [];
        // 遍历所有元素,检查它们的样式
        allElements.forEach(function (element) {
            // 获取元素的计算样式
            var style = getComputedStyle(element);

            // 检查overflow-y属性是否为auto
            if (style.overflowY === 'auto') {
                // 如果是,添加到结果数组中
                arr.push(element);
            }
        });
        arr.sort((a, b) => {
            return (b.clientHeight + b.clientWidth) - (a.clientHeight + a.clientWidth);
        })
        elementsWithOverflowYAuto = elementsWithOverflowYAuto.concat(arr);
    }

    const UpButton = document.createElement("div");
    UpButton.style.cssText = "flex:1";
    UpButton.addEventListener('click', () => {
        scroll(-scrollRatio * window.innerHeight);
    });

    const DownButton = document.createElement("div");
    DownButton.style.cssText = "flex:1;border-top-style:dashed;border-width:2px;";
    DownButton.addEventListener('click', () => {
        scroll(scrollRatio * window.innerHeight);
    });

    function scroll(y) {
        if (document.contains(el)) {
            el.scrollBy(0, y);
            return;
        }
        init();
        setTimeout(() => {
            if (document.contains(el)) {
                el.scrollBy(0, y);
            }
        }, 150);
    }

    Container.appendChild(UpButton);
    Container.appendChild(DownButton);
    body.appendChild(Container);

})();