<?php
// Extract JS from quiz.php and validate syntax with Node
$content = file_get_contents('quiz.php');

// Find <script> and </script>
if (preg_match('#<script>(.*?)</script>#s', $content, $m)) {
    $js = $m[1];
    // Replace PHP tags
    $js = preg_replace('#<\?= e\(csrfToken\(\)\) \?>#', "'dummycsrftoken'", $js);
    $js = preg_replace('#<\?= \$studyDeck \? json_encode\(\$studyDeck\) : \'null\' \?>#', 'null', $js);
    $js = preg_replace('#<\?= json_encode\(\$studyQuestions\) \?>#', '[]', $js);
    file_put_contents('quiz_script.js', $js);
    echo "Extracted JS written to quiz_script.js\n";
} else {
    echo "No script block found\n";
}