tomo2chin2 commited on
Commit
38f7de3
·
verified ·
1 Parent(s): d97560e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -17
app.py CHANGED
@@ -564,43 +564,91 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0
564
  scroll_height = 1000
565
  logger.warning(f"Falling back to dimensions: width={scroll_width}, height={scroll_height}")
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))
 
564
  scroll_height = 1000
565
  logger.warning(f"Falling back to dimensions: width={scroll_width}, height={scroll_height}")
566
 
567
+ # 9) レイアウト安定化の確認 (修正版)
568
  try:
569
  stability_script = """
570
+ return new Promise((resolve, reject) => {
571
+ let lastHeight = 0;
572
+ let lastWidth = 0;
573
  let stableCount = 0;
574
+ const maxChecks = 15; // 最大チェック回数(無限ループ防止)
575
+ let checkCount = 0;
576
+
577
  const checkStability = () => {
578
+ checkCount++;
579
+ if (checkCount > maxChecks) {
580
+ console.warn('Layout stability check reached max attempts.');
581
+ resolve(false); // 安定しなかったと判断
582
+ return;
583
+ }
584
+
585
+ // body要素が存在し、offsetHeight/offsetWidthが利用可能かチェック
586
+ const bodyExists = document.body && typeof document.body.offsetHeight === 'number' && typeof document.body.offsetWidth === 'number';
587
+
588
+ if (!bodyExists) {
589
+ // bodyがまだ利用できない場合、少し待って再試行
590
+ console.warn('Document body not fully available yet for stability check.');
591
+ setTimeout(checkStability, 250); // 待機時間を少し長くする
592
+ return;
593
+ }
594
+
595
  const currentHeight = document.body.offsetHeight;
596
  const currentWidth = document.body.offsetWidth;
597
+
598
+ // サイズが0の場合も不安定とみなす(初期化中など)
599
+ if (currentHeight === 0 || currentWidth === 0) {
600
+ stableCount = 0; // カウントリセット
601
+ lastHeight = 0; // 前回値もリセット
602
+ lastWidth = 0;
603
+ console.warn('Body dimensions are zero, waiting...');
604
+ setTimeout(checkStability, 250); // 再試行
605
+ return;
606
+ }
607
+
608
  if (currentHeight === lastHeight && currentWidth === lastWidth) {
609
  stableCount++;
610
  if (stableCount >= 3) { // 3回連続で同じなら安定と判断
611
+ console.log('Layout deemed stable.');
612
+ resolve(true); // 安定した
613
  return;
614
  }
615
  } else {
616
+ stableCount = 0; // サイズが変わったらリセット
617
  lastHeight = currentHeight;
618
  lastWidth = currentWidth;
619
+ console.log(`Layout changed: ${lastWidth}x${lastHeight}. Resetting stability count.`);
620
  }
621
+
622
+ // 次のチェックをスケジュール
623
+ setTimeout(checkStability, 200);
624
  };
625
+
626
+ // 初回チェックを開始
627
+ // 少し遅延させて初回実行(DOMが完全に準備されるのを待つ)
628
+ setTimeout(checkStability, 100);
629
  });
630
  """
631
+
632
  logger.info("Checking layout stability...")
633
+ # 非同期スクリプトのタイムアウトを設定 (例: 20秒)
634
+ driver.set_script_timeout(20)
635
+ stable = driver.execute_async_script(f"const callback = arguments[arguments.length - 1]; {stability_script}.then(callback);")
636
+
637
+ if stable:
638
+ logger.info("Layout is stable")
639
+ else:
640
+ # 安定しなかった場合も警告を出す
641
+ logger.warning("Layout did not stabilize within the expected time. Proceeding anyway.")
642
+ time.sleep(2) # 念のため少し待つ
643
+
644
+ except TimeoutException:
645
+ # スクリプト実行がタイムアウトした場合
646
+ logger.warning("Layout stability check timed out. Proceeding anyway.")
647
+ time.sleep(2) # 念のため少し待つ
648
  except Exception as e:
649
+ # その他の予期せぬエラー(元のエラーが発生していた箇所)
650
+ logger.error(f"Error during layout stability check: {e}", exc_info=True) # スタックトレース付きでエラー出力
651
+ time.sleep(2) # エラー時も待機して続行
652
 
653
  # 10) Calculate adjusted height with user-specified margin
654
  adjusted_height = int(scroll_height * (1 + extension_percentage / 100.0))