Skip to content

Commit f143e6e

Browse files
committed
Add robust check
Signed-off-by: Shawn Hsu <ambersun1019.shawn@gmail.com>
1 parent 9f77720 commit f143e6e

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

src/core/analyzer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def analyze(self) -> int:
5959
page_result, page_fail_count = checker.check_page(page_url)
6060
fail_count += page_fail_count
6161
site_results.extend(page_result)
62+
except Exception as e:
63+
logger.error(
64+
f"Error analyzing page", extra={"page_url": page_url, "error": e}
65+
)
6266

6367
finally:
6468
context.close()

src/core/image_checker.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def check_page(self, page_url: str) -> (list, bool):
2727
)
2828
raise Exception(f"Failed to load page: {page_url}")
2929

30+
page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
31+
page.wait_for_timeout(1000)
32+
3033
images = page.locator("img").all()
3134
for index, img in enumerate(images):
3235
width, height = self._check_image(img, page_url)
@@ -61,6 +64,7 @@ def check_page(self, page_url: str) -> (list, bool):
6164

6265
def _check_image(self, img_element: Locator, page_url: str) -> (int, int):
6366
try:
67+
img_element.scroll_into_view_if_needed()
6468
img_url = img_element.get_attribute("src", timeout=0)
6569

6670
box = img_element.bounding_box()
@@ -76,48 +80,67 @@ def _check_image(self, img_element: Locator, page_url: str) -> (int, int):
7680
return
7781

7882
dimensions = img_element.evaluate("""
79-
(img) => {
80-
return {
81-
width: img.width,
82-
height: img.height,
83-
naturalWidth: img.naturalWidth,
84-
naturalHeight: img.naturalHeight,
83+
async (img) => {
84+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
85+
86+
const checkDimensions = (el) => {
87+
const isSvg = el.currentSrc.toLowerCase().endsWith('.svg') || el.src.startsWith('data:image/svg+xml');
88+
const hasPixels = el.naturalWidth > 0 || (isSvg && el.getBoundingClientRect().width > 0);
89+
90+
return {
91+
naturalWidth: el.naturalWidth,
92+
naturalHeight: el.naturalHeight,
93+
hasPixels: hasPixels,
94+
complete: el.complete,
95+
src: el.currentSrc || el.src
96+
};
8597
};
98+
99+
let res = checkDimensions(img);
100+
if (!img.complete) {
101+
await new Promise(r => { img.onload = r; img.onerror = r; setTimeout(r, 5000); });
102+
}
103+
104+
res = checkDimensions(img);
105+
if (!res.hasPixels && img.complete) {
106+
await delay(300);
107+
res = checkDimensions(img);
108+
}
109+
110+
return res;
86111
}
87112
""")
88113

89-
width = dimensions.get("width")
90-
height = dimensions.get("height")
91-
92114
n_width = dimensions.get("naturalWidth")
93115
n_height = dimensions.get("naturalHeight")
116+
complete = dimensions.get("complete")
94117

95-
if (width == 0 or height == 0) or (n_width == 0 or n_height == 0):
118+
if not complete:
96119
logger.error(
97-
f"Image failed to load (size 0)",
120+
f"Image failed to load (complete False)",
98121
extra={
99122
"page_url": page_url,
100123
"img_url": img_url,
101-
"reason": "Image failed to load (size 0)",
124+
"reason": "Image failed to load (complete False)",
102125
},
103126
)
104-
raise Exception("Image failed to load (size 0)")
127+
raise Exception("Image failed to load (complete False)")
105128

106-
if width is None or height is None:
129+
if n_width == 0 and n_height == 0:
107130
logger.error(
108-
f"Image failed to load (size None)",
131+
f"Image failed to load (size 0)",
109132
extra={
110133
"page_url": page_url,
111134
"img_url": img_url,
112-
"reason": "Image failed to load (size None)",
135+
"naturalWidth": n_width,
136+
"naturalHeight": n_height,
137+
"complete": complete,
138+
"reason": "Image failed to load (size 0)",
113139
},
114140
)
115-
raise Exception("Image failed to load (size None)")
116-
117-
w = width or n_width
118-
h = height or n_height
141+
raise Exception("Image failed to load (size 0)")
119142

120-
return w, h
143+
return n_width, n_height
121144

122145
except Exception as e:
123146
raise e

0 commit comments

Comments
 (0)