z230
This commit is contained in:
@@ -47,46 +47,62 @@ async def main():
|
||||
context = await browser.new_context(
|
||||
viewport={"width": 1280, "height": 900},
|
||||
)
|
||||
page = await context.new_page()
|
||||
|
||||
# Krok 1: načti hlavní stránku a zjisti URL iframu s hrou
|
||||
page = await context.new_page()
|
||||
print(f"Načítám {URL} ...")
|
||||
await page.goto(URL, wait_until="networkidle", timeout=60_000)
|
||||
|
||||
# Najdi iframe s hrou (solitaire.org vkládá hru do iframe)
|
||||
game_frame = None
|
||||
game_url = None
|
||||
for frame in page.frames:
|
||||
if frame.url != page.url and frame.url.strip() not in ("", "about:blank"):
|
||||
game_frame = frame
|
||||
print(f" Nalezen iframe: {frame.url}")
|
||||
game_url = frame.url
|
||||
print(f" Nalezen iframe: {game_url}")
|
||||
break
|
||||
|
||||
target = game_frame if game_frame else page
|
||||
if not game_url:
|
||||
# Zkus najít iframe src přímo v DOM
|
||||
iframe_src = await page.get_attribute("iframe", "src")
|
||||
if iframe_src:
|
||||
game_url = iframe_src if iframe_src.startswith("http") else f"https://www.solitaire.org{iframe_src}"
|
||||
print(f" Iframe src z DOM: {game_url}")
|
||||
|
||||
# Zkus kliknout na Print tlačítko (různé možné selektory)
|
||||
print_selectors = [
|
||||
"text=Print",
|
||||
"button:has-text('Print')",
|
||||
"[title*='print' i]",
|
||||
"[aria-label*='print' i]",
|
||||
".print-button",
|
||||
"#print",
|
||||
]
|
||||
clicked = False
|
||||
for sel in print_selectors:
|
||||
try:
|
||||
await target.click(sel, timeout=3_000)
|
||||
clicked = True
|
||||
print(f" Kliknuto na Print ({sel})")
|
||||
await page.wait_for_timeout(1_500)
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
await page.close()
|
||||
|
||||
if not clicked:
|
||||
print(" Tlačítko Print nenalezeno — ukládám celou stránku jako PDF.")
|
||||
# Krok 2: otevři URL hry
|
||||
game_page = await context.new_page()
|
||||
target_url = game_url if game_url else URL
|
||||
print(f"Načítám hru přímo: {target_url} ...")
|
||||
await game_page.goto(target_url, wait_until="networkidle", timeout=60_000)
|
||||
|
||||
# Uložit jako PDF
|
||||
await page.pdf(
|
||||
# Game.printDaily() vytvoří iframe #printFrame a zapíše do něj puzzle HTML
|
||||
await game_page.evaluate("Game.printDaily()")
|
||||
await game_page.wait_for_timeout(1_000)
|
||||
|
||||
# Přečti HTML přímo z iframu
|
||||
iframe_html = await game_page.evaluate("""() => {
|
||||
const iframe = document.getElementById('printFrame');
|
||||
if (!iframe) return null;
|
||||
try {
|
||||
return iframe.contentDocument.documentElement.outerHTML;
|
||||
} catch(e) { return null; }
|
||||
}""")
|
||||
|
||||
if not iframe_html:
|
||||
raise RuntimeError("Nepodařilo se získat HTML z #printFrame")
|
||||
|
||||
print(f" iframe HTML zachycen ({len(iframe_html)} znaků), ukládám PDF...")
|
||||
|
||||
# Přidej base URL aby se načetly obrázky (img/black.png, img/title.jpg)
|
||||
base_url = "https://www.solitaire.org/daily-str8ts/"
|
||||
iframe_html = iframe_html.replace("<head>", f'<head><base href="{base_url}">', 1)
|
||||
|
||||
# Načti HTML do nové stránky a ulož jako PDF
|
||||
pdf_page = await context.new_page()
|
||||
await pdf_page.set_content(iframe_html, wait_until="networkidle")
|
||||
await pdf_page.wait_for_timeout(500)
|
||||
|
||||
await pdf_page.pdf(
|
||||
path=str(output_path),
|
||||
format="A4",
|
||||
print_background=True,
|
||||
|
||||
Reference in New Issue
Block a user