colorblind text helper

change all red text color to preferred one

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         colorblind text helper
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  change all red text color to preferred one
// @author       Something begins
// @license      none
// @include       https://*
// @grant        none
// ==/UserScript==

const shadeCoef = 80;
const toColor = "purple";
// Function to check if an element is a text element
function isTextElement(element) {
    return element.nodeType === Node.TEXT_NODE;
}

// Function to check if a text element has a red color
function isRedText(element) {
    const parentElement = element.parentElement;
    if (!parentElement) {
        return false;
    }

    const computedStyle = window.getComputedStyle(parentElement);
    const color = computedStyle.color;

    if (color.startsWith('rgb(')) {
        const rgb = color.match(/\d+/g);
        const red = parseInt(rgb[0]);
        const green = parseInt(rgb[1]);
        const blue = parseInt(rgb[2]);

        if (red > green + shadeCoef && red > blue + shadeCoef) {
            return true;
        }
    } else if (color.startsWith('#')) {
        const hex = color.substr(1);
        const red = parseInt(hex.substr(0, 2), 16);
        const green = parseInt(hex.substr(2, 2), 16);
        const blue = parseInt(hex.substr(4, 2), 16);

        if (red > green + shadeCoef && red > blue + shadeCoef) {
            return true;
        }
    }

    return false;
}

// Get all text nodes on the page
const walker = document.createTreeWalker(
    document.body,
    NodeFilter.SHOW_TEXT,
    { acceptNode: node => NodeFilter.FILTER_ACCEPT }
);

const textNodes = [];
while (walker.nextNode()) {
    textNodes.push(walker.currentNode);
}

// Filter text nodes that are red
const redTextNodes = textNodes.filter(isRedText);

// Log red text nodes
redTextNodes.forEach(textNode => {
    console.log('Red text:', textNode);
    textNode.parentElement.style.color = toColor;
});