Spaces:
Sleeping
Sleeping
File size: 7,098 Bytes
ee6e247 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | #!/usr/bin/env python3
"""
Analyze visual aspects of a web page using Playwright.
Usage:
python analyze_visual.py https://example.com
"""
import argparse
import ipaddress
import json
import socket
import sys
from urllib.parse import ParseResult, urlparse
try:
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeout
except ImportError:
print("Error: playwright required. Install with: pip install playwright && playwright install chromium")
sys.exit(1)
def normalize_url(url: str) -> tuple[str, ParseResult]:
"""Normalize URL and return (url, parsed_url)."""
parsed = urlparse(url)
if not parsed.scheme:
url = f"https://{url}"
parsed = urlparse(url)
if parsed.scheme not in ("http", "https"):
raise ValueError(f"Invalid URL scheme: {parsed.scheme}")
if not parsed.hostname:
raise ValueError("Invalid URL: missing hostname")
return url, parsed
def analyze_visual(url: str, timeout: int = 30000) -> dict:
"""
Analyze visual aspects of a web page.
Args:
url: URL to analyze
timeout: Page load timeout in milliseconds
Returns:
Dictionary with visual analysis results
"""
result = {
"url": url,
"above_fold": {
"h1_visible": False,
"cta_visible": False,
"hero_image": None,
},
"mobile": {
"viewport_meta": False,
"horizontal_scroll": False,
"touch_targets_ok": True,
},
"layout": {
"overlapping_elements": [],
"text_overflow": [],
},
"fonts": {
"base_size": None,
"readable": True,
},
"error": None,
}
try:
url, parsed = normalize_url(url)
result["url"] = url
except ValueError as e:
result["error"] = str(e)
return result
# SSRF prevention: block private/internal IPs
try:
resolved_ip = socket.gethostbyname(parsed.hostname)
ip = ipaddress.ip_address(resolved_ip)
if ip.is_private or ip.is_loopback or ip.is_reserved:
result["error"] = f"Blocked: URL resolves to private/internal IP ({resolved_ip})"
return result
except socket.gaierror:
pass
try:
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
# Desktop analysis
desktop = browser.new_context(viewport={"width": 1920, "height": 1080})
page = desktop.new_page()
page.goto(url, wait_until="networkidle", timeout=timeout)
# Check H1 visibility above fold
h1 = page.query_selector("h1")
if h1:
box = h1.bounding_box()
if box and box["y"] < 1080:
result["above_fold"]["h1_visible"] = True
# Check for CTA buttons above fold
cta_selectors = [
"a[href*='signup']",
"a[href*='contact']",
"a[href*='demo']",
"button:has-text('Get Started')",
"button:has-text('Sign Up')",
"button:has-text('Contact')",
".cta",
"[class*='cta']",
]
for selector in cta_selectors:
try:
cta = page.query_selector(selector)
if cta:
box = cta.bounding_box()
if box and box["y"] < 1080:
result["above_fold"]["cta_visible"] = True
break
except Exception:
pass
# Check hero image
hero_selectors = [
".hero img",
"[class*='hero'] img",
"header img",
"main img:first-of-type",
]
for selector in hero_selectors:
try:
hero = page.query_selector(selector)
if hero:
src = hero.get_attribute("src")
if src:
result["above_fold"]["hero_image"] = src
break
except Exception:
pass
desktop.close()
# Mobile analysis
mobile = browser.new_context(viewport={"width": 375, "height": 812})
page = mobile.new_page()
page.goto(url, wait_until="networkidle", timeout=timeout)
# Check viewport meta
viewport_meta = page.query_selector('meta[name="viewport"]')
result["mobile"]["viewport_meta"] = viewport_meta is not None
# Check for horizontal scroll
scroll_width = page.evaluate("document.documentElement.scrollWidth")
viewport_width = page.evaluate("window.innerWidth")
result["mobile"]["horizontal_scroll"] = scroll_width > viewport_width
# Check font size
base_font_size = page.evaluate("""
() => {
const body = document.body;
const style = window.getComputedStyle(body);
return parseFloat(style.fontSize);
}
""")
result["fonts"]["base_size"] = base_font_size
result["fonts"]["readable"] = base_font_size >= 16
mobile.close()
browser.close()
except PlaywrightTimeout:
result["error"] = f"Page load timed out after {timeout}ms"
except Exception as e:
result["error"] = str(e)
return result
def main():
parser = argparse.ArgumentParser(description="Analyze visual aspects of a web page")
parser.add_argument("url", help="URL to analyze")
parser.add_argument("--timeout", "-t", type=int, default=30000, help="Timeout in ms")
parser.add_argument("--json", "-j", action="store_true", help="Output as JSON")
args = parser.parse_args()
result = analyze_visual(args.url, timeout=args.timeout)
if args.json:
print(json.dumps(result, indent=2))
else:
print("Visual Analysis Results")
print("=" * 40)
print("\nAbove the Fold:")
print(f" H1 Visible: {'β' if result['above_fold']['h1_visible'] else 'β'}")
print(f" CTA Visible: {'β' if result['above_fold']['cta_visible'] else 'β'}")
print(f" Hero Image: {result['above_fold']['hero_image'] or 'None found'}")
print("\nMobile Responsiveness:")
print(f" Viewport Meta: {'β' if result['mobile']['viewport_meta'] else 'β'}")
print(f" Horizontal Scroll: {'β (problem)' if result['mobile']['horizontal_scroll'] else 'β'}")
print("\nTypography:")
print(f" Base Font Size: {result['fonts']['base_size']}px")
print(f" Readable (β₯16px): {'β' if result['fonts']['readable'] else 'β'}")
if result["error"]:
print(f"\nError: {result['error']}")
if __name__ == "__main__":
main()
|