File size: 7,491 Bytes
7a7f916
251dcfb
f35c037
 
 
 
 
d416496
 
f35c037
 
 
 
b983a60
d416496
f35c037
5fd155f
9b252fc
 
5fd155f
9b252fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fd155f
 
 
 
 
9b252fc
5fd155f
b983a60
5fd155f
b983a60
 
d416496
 
 
5fd155f
d416496
 
b983a60
5fd155f
d416496
 
 
 
 
 
 
251dcfb
f35c037
 
 
5fd155f
d416496
 
f35c037
 
 
251dcfb
f35c037
 
 
5fd155f
d416496
 
f35c037
 
 
251dcfb
d416496
 
 
 
f35c037
9b252fc
 
 
 
 
 
5fd155f
9b252fc
 
 
 
5fd155f
9b252fc
5fd155f
9b252fc
 
 
5fd155f
 
 
9b252fc
5fd155f
9b252fc
 
 
5fd155f
9b252fc
 
 
 
 
 
 
5fd155f
9b252fc
 
 
 
5fd155f
9b252fc
5fd155f
9b252fc
 
 
5fd155f
 
 
9b252fc
5fd155f
9b252fc
 
 
5fd155f
9b252fc
 
 
 
 
 
f39e50f
 
 
 
 
 
 
 
 
 
3e6a6a1
 
f39e50f
 
3e6a6a1
f39e50f
f6beeee
f39e50f
3e6a6a1
f39e50f
3e6a6a1
f39e50f
f6beeee
9b252fc
5fd155f
9b252fc
 
 
 
 
3e6a6a1
9b252fc
 
 
 
 
 
 
 
 
f6beeee
 
 
10a517e
f6beeee
 
9b252fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* static/script.js */
document.addEventListener('DOMContentLoaded', function() {
    console.log("DOMContentLoaded event fired - script.js loaded");

    const urlSubmitButton = document.getElementById('url-submit');
    const imageSubmitButton = document.getElementById('image-submit');
    const tabButtons = document.querySelectorAll('.ssth_tab_button');
    const statusMessages = document.getElementById('status-messages');
    const progressBar = document.getElementById('progress-bar');

    console.log("urlSubmitButton:", urlSubmitButton);
    console.log("imageSubmitButton:", imageSubmitButton);
    console.log("tabButtons:", tabButtons);
    console.log("statusMessages:", statusMessages);
    console.log("progressBar:", progressBar);

    let socket; // Declare socket outside the if block

    if (typeof io !== 'undefined') {
        socket = io(); // Connect to Socket.IO server

        socket.on('connect', function() {
            console.log('Connected to WebSocket server!');
        });

        socket.on('progress', function(data) {
            console.log('Progress update received:', data);
            updateProgressBar(data.percent);
            displayStatusMessage(data.message);
        });

        socket.on('disconnect', function() {
            console.log('Disconnected from WebSocket server!');
        });
    } else {
        console.warn("Socket.IO library not found. Progress updates will not be displayed.");
    }

    if (!urlSubmitButton) console.error("URL Submit button not found! Check ID in HTML.");
    if (!imageSubmitButton) console.error("Image Submit button not found! Check ID in HTML.");
    if (!tabButtons || tabButtons.length === 0) console.error("Tab buttons not found! Check class in HTML.");
    if (!statusMessages) console.error("Status messages area not found! Check ID in HTML.");
    if (!progressBar) console.error("Progress bar not found! Check ID in HTML");

    // Helper functions
    function displayStatusMessage(message) {
        if (statusMessages) statusMessages.textContent = message;
    }

    function updateProgressBar(percentage) {
        if (progressBar) {
            progressBar.style.width = percentage + '%';
            progressBar.textContent = percentage + '%';
        }
    }

    // Tab switching
    tabButtons.forEach(button => {
        button.addEventListener('click', function(event) {
            const tabId = this.dataset.tab;
            console.log(`Tab button '${tabId}' clicked`);
            switchTab(tabId);
        });
    });

    if (urlSubmitButton) {
        urlSubmitButton.addEventListener('click', function(event) {
            event.preventDefault();
            console.log("URL Submit button clicked");
            resetProgress();
            displayStatusMessage("Taking screenshot of webpage...");
            processURL();
        });
    }

    if (imageSubmitButton) {
        imageSubmitButton.addEventListener('click', function(event) {
            event.preventDefault();
            console.log("Image Submit button clicked");
            resetProgress();
            displayStatusMessage("Processing uploaded image...");
            processImage();
        });
    }

    function switchTab(tabId) {
        console.log(`switchTab('${tabId}') called`);
        document.querySelectorAll('.ssth_tab_button').forEach(btn => btn.classList.remove('active'));
        document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));

        document.querySelector(`.ssth_tab_button[data-tab="${tabId}"]`).classList.add('active');
        document.getElementById(tabId).classList.add('active');
        resetProgress();
    }

    async function processURL() {
        console.log("processURL() called");
        const form = document.getElementById('url-form');
        const formData = new FormData(form);

        try {
            const response = await fetch('/process_url', { method: 'POST', body: formData });
            displayStatusMessage("Response received, processing with Gemini...");
            updateProgressBar(50);

            const result = await response.json();
            if (response.ok) {
                console.log("Response OK:", result);
                updateProgressBar(100);
                displayResults(result.files, result.preview, result.combined_html);
            } else {
                console.error("Response NOT OK:", result);
                displayError(result.error);
            }
        } catch (err) {
            console.error("Fetch error:", err);
            displayError(err.message);
        } finally {
            displayStatusMessage("");
        }
    }

    async function processImage() {
        console.log("processImage() called");
        const form = document.getElementById('image-form');
        const formData = new FormData(form);

        try {
            const response = await fetch('/process_image', { method: 'POST', body: formData });
            displayStatusMessage("Response received, processing with Gemini...");
            updateProgressBar(50);

            const result = await response.json();
            if (response.ok) {
                console.log("Response OK:", result);
                updateProgressBar(100);
                displayResults(result.files, result.preview, result.combined_html);
            } else {
                console.error("Response NOT OK:", result);
                displayError(result.error);
            }
        } catch (err) {
            console.error("Fetch error:", err);
            displayError(err.message);
        } finally {
            displayStatusMessage("");
        }
    }

    function displayResults(files, preview, combinedHtml) {
        console.log("displayResults() called");
        updateProgressBar(0);
        const filesDiv = document.getElementById('files');
        filesDiv.innerHTML = '';

        Object.entries(files).forEach(([filename, file]) => {
            const fileDiv = document.createElement('div');
            fileDiv.className = 'ssth_file_window';
            fileDiv.innerHTML = `
                <h3 class="ssth_file_title">${filename}</h3>
                <pre class="ssth_code_container"><code id="${filename}-code"></code></pre>
            `;
            filesDiv.appendChild(fileDiv);
            document.getElementById(`${filename}-code`).textContent = file.content;
        });

        const previewIframe = document.getElementById('preview-iframe');
        if (previewIframe) previewIframe.srcdoc = combinedHtml;

        document.getElementById('combined-code').textContent = combinedHtml ? escapeHtml(combinedHtml) : 'No combined HTML generated.';
    }

    function displayError(error) {
        console.log("displayError() called:", error);
        displayStatusMessage(`Error: ${error}`);
        updateProgressBar(0);
        const filesDiv = document.getElementById('files');
        filesDiv.innerHTML = `<p class="ssth_error_message">Error: ${escapeHtml(error)}</p>`;
        document.getElementById('preview-iframe').srcdoc = '';
        document.getElementById('combined-code').textContent = '';
    }

    function resetProgress() {
        updateProgressBar(0);
        displayStatusMessage("");
    }

    function escapeHtml(unsafe) {
        return unsafe
            .replace(/&/g, "&")
            .replace(/</g, "<")
            .replace(/>/g, ">")
            .replace(/"/g, '"')
            .replace(/'/g, "'");
    }
});