tomo2chin2 commited on
Commit
6b12398
·
verified ·
1 Parent(s): f0fa273

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -90
app.py CHANGED
@@ -425,37 +425,9 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
425
 
426
  # 5) Font Awesomeと外部リソースの読み込み完了を確認
427
  try:
428
- # Font Awesomeと外部リソースの読み込み完了を確認するスクリプト
429
- resource_check_script = """
430
- return new Promise((resolve) => {
431
- // Font Awesomeの読み込み確認
432
- const checkFontAwesome = () => {
433
- const icons = document.querySelectorAll('.fa, .fas, .far, .fab, [class*="fa-"]');
434
- if (icons.length > 0) {
435
- console.log('Font Awesome icons found:', icons.length);
436
- // すべてのフォントの読み込み完了を待つ
437
- document.fonts.ready.then(() => {
438
- console.log('All fonts loaded');
439
- setTimeout(resolve, 1000); // 安定化のための追加待機
440
- });
441
- } else {
442
- // アイコンがない、またはすでに読み込み完了
443
- document.fonts.ready.then(() => setTimeout(resolve, 500));
444
- }
445
- };
446
-
447
- // DOMContentLoadedまたはloadイベント後にチェック
448
- if (document.readyState === 'complete') {
449
- checkFontAwesome();
450
- } else {
451
- window.addEventListener('load', checkFontAwesome);
452
- }
453
- });
454
- """
455
-
456
  logger.info("Waiting for Font Awesome and other resources to load...")
457
  driver.set_script_timeout(15) # 15秒のタイムアウト
458
- driver.execute_async_script(f"const callback = arguments[arguments.length - 1]; {resource_check_script}.then(callback);")
459
  logger.info("Resources loaded successfully")
460
  except Exception as e:
461
  logger.warning(f"Resource loading check failed: {e}. Using fallback wait.")
@@ -463,42 +435,14 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
463
 
464
  # 6) スクロールを制御してコンテンツ全体が描画されるようにする
465
  try:
466
- scroll_script = """
467
- return new Promise(resolve => {
468
- const height = Math.max(
469
- document.documentElement.scrollHeight,
470
- document.body.scrollHeight
471
- );
472
- const viewportHeight = window.innerHeight;
473
-
474
- // ページを少しずつスクロールして全体を描画させる
475
- const scrollStep = Math.floor(viewportHeight * 0.8);
476
- let currentPos = 0;
477
-
478
- const scrollDown = () => {
479
- if (currentPos < height) {
480
- window.scrollTo(0, currentPos);
481
- currentPos += scrollStep;
482
- setTimeout(scrollDown, 100);
483
- } else {
484
- // 最後にトップに戻す
485
- window.scrollTo(0, 0);
486
- setTimeout(resolve, 300);
487
- }
488
- };
489
-
490
- scrollDown();
491
- });
492
- """
493
-
494
  logger.info("Ensuring all content is rendered...")
495
- driver.execute_async_script(f"const callback = arguments[arguments.length - 1]; {scroll_script}.then(callback);")
496
  except Exception as e:
497
  logger.warning(f"Content rendering check failed: {e}")
498
  # スクロールを元の位置に戻す
499
  driver.execute_script("window.scrollTo(0, 0);")
500
  time.sleep(1)
501
-
502
  # 7) Hide scrollbars via CSS
503
  try:
504
  driver.execute_script(
@@ -566,42 +510,13 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
566
 
567
  # 9) レイアウト安定化の確認
568
  try:
569
- stability_script = """
570
- return new Promise(resolve => {
571
- let lastHeight = document.body.offsetHeight;
572
- let lastWidth = document.body.offsetWidth;
573
- let stableCount = 0;
574
-
575
- const checkStability = () => {
576
- const currentHeight = document.body.offsetHeight;
577
- const currentWidth = document.body.offsetWidth;
578
-
579
- if (currentHeight === lastHeight && currentWidth === lastWidth) {
580
- stableCount++;
581
- if (stableCount >= 3) { // 3回連続で同じなら安定と判断
582
- resolve(true);
583
- return;
584
- }
585
- } else {
586
- stableCount = 0;
587
- lastHeight = currentHeight;
588
- lastWidth = currentWidth;
589
- }
590
-
591
- setTimeout(checkStability, 200); // 200ms間隔でチェック
592
- };
593
-
594
- checkStability();
595
- });
596
- """
597
-
598
  logger.info("Checking layout stability...")
599
- driver.execute_async_script(f"const callback = arguments[arguments.length - 1]; {stability_script}.then(callback);")
600
  logger.info("Layout is stable")
601
  except Exception as e:
602
  logger.warning(f"Layout stability check failed: {e}")
603
  time.sleep(2) # 追加待機
604
-
605
  # 10) Calculate adjusted height with user-specified margin
606
  adjusted_height = int(scroll_height * (1 + extension_percentage / 100.0))
607
  # Ensure adjusted height is not excessively large or small
 
425
 
426
  # 5) Font Awesomeと外部リソースの読み込み完了を確認
427
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
  logger.info("Waiting for Font Awesome and other resources to load...")
429
  driver.set_script_timeout(15) # 15秒のタイムアウト
430
+ driver.execute_async_script(resource_check_script)
431
  logger.info("Resources loaded successfully")
432
  except Exception as e:
433
  logger.warning(f"Resource loading check failed: {e}. Using fallback wait.")
 
435
 
436
  # 6) スクロールを制御してコンテンツ全体が描画されるようにする
437
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
438
  logger.info("Ensuring all content is rendered...")
439
+ driver.execute_async_script(scroll_script)
440
  except Exception as e:
441
  logger.warning(f"Content rendering check failed: {e}")
442
  # スクロールを元の位置に戻す
443
  driver.execute_script("window.scrollTo(0, 0);")
444
  time.sleep(1)
445
+
446
  # 7) Hide scrollbars via CSS
447
  try:
448
  driver.execute_script(
 
510
 
511
  # 9) レイアウト安定化の確認
512
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513
  logger.info("Checking layout stability...")
514
+ driver.execute_async_script(stability_script)
515
  logger.info("Layout is stable")
516
  except Exception as e:
517
  logger.warning(f"Layout stability check failed: {e}")
518
  time.sleep(2) # 追加待機
519
+
520
  # 10) Calculate adjusted height with user-specified margin
521
  adjusted_height = int(scroll_height * (1 + extension_percentage / 100.0))
522
  # Ensure adjusted height is not excessively large or small