This commit is contained in:
Josh at WLTechBlog
2025-12-09 14:58:00 -07:00
parent 77d1061ae7
commit 87e5e0555d
6 changed files with 159 additions and 41 deletions

View File

@@ -23,7 +23,7 @@ import (
"github.com/go-rod/rod/lib/proto"
)
const Version = "2.2.0-contrast-detection-fix"
const Version = "2.2.1-contrast-detection-fix"
// Daemon is the main server that manages browser connections
type Daemon struct {
@@ -9299,24 +9299,56 @@ func (d *Daemon) checkContrast(tabID string, selector string, timeout int) (*Con
}
// Get the actual text-bearing element for accurate color detection
// If the element has child elements with text, use the first visible child's color
// If the element has child elements with text, use the child's color
let textElement = element;
let textStyle = style;
// Check if element has child elements (not just text nodes)
const childElements = Array.from(element.children).filter(child => {
const childText = child.textContent.trim();
if (!childText) return false;
const childStyle = window.getComputedStyle(child);
return childStyle.display !== 'none' && childStyle.visibility !== 'hidden';
});
// Recursively find the element with the most text content
// This handles cases like <a><span>text</span><br></a>
function findTextBearingElement(el) {
// Get all descendant elements (not just direct children)
const descendants = el.querySelectorAll('*');
let bestElement = el;
let maxTextLength = 0;
// If there are visible child elements with text, use the first one's color
if (childElements.length > 0) {
textElement = childElements[0];
textStyle = window.getComputedStyle(textElement);
// Check the element itself first
const directText = Array.from(el.childNodes)
.filter(node => node.nodeType === Node.TEXT_NODE)
.map(node => node.textContent.trim())
.join('').length;
if (directText > maxTextLength) {
maxTextLength = directText;
bestElement = el;
}
// Check all descendants
for (const desc of descendants) {
const descStyle = window.getComputedStyle(desc);
// Skip invisible elements
if (descStyle.display === 'none' || descStyle.visibility === 'hidden') {
continue;
}
// Get direct text content (not inherited from children)
const descText = Array.from(desc.childNodes)
.filter(node => node.nodeType === Node.TEXT_NODE)
.map(node => node.textContent.trim())
.join('').length;
if (descText > maxTextLength) {
maxTextLength = descText;
bestElement = desc;
}
}
return bestElement;
}
// Find the element with the most text content
textElement = findTextBearingElement(element);
textStyle = window.getComputedStyle(textElement);
const fgColor = textStyle.color;
const fontSize = textStyle.fontSize;
const fontWeight = textStyle.fontWeight;