File size: 6,125 Bytes
83e35a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
197
198
199
200
201
202
203
204
205
206
const carousel = document.querySelector(".carousel");
const carouselItems = carousel ? carousel.querySelectorAll(".carousel-item") : [];
const submissionResult = document.getElementById("submissionResult");
const linkInput = document.getElementById("link-input");
const filePicker = document.getElementById("fileInput");
const videoPreview = document.getElementById("video-preview");
const iFramePreview = document.getElementById("iframe-preview");
// Background video removed; keep null to avoid errors
const bgVideo = null;

var selectedFile = null;
var selectedLink = "";
var linkInputVisible = false;
let currentItem = 0;

// 1. Background image carousel
function changeImage() {
  if (!carouselItems || carouselItems.length === 0) return;
  carouselItems.forEach((item, index) => {
    if (index === currentItem) {
      item.classList.add("active");
    } else {
      item.classList.remove("active");
    }
  });
  currentItem = (currentItem + 1) % carouselItems.length;
}
setInterval(changeImage, 3000);

// 2. Box with title, description and (file uploader/link & submit button)
const box = document.querySelector(".box");
setTimeout(() => {
  box.classList.add("visible");
}, 2000);

// 3. File uploader
function openFilePicker() {
  hideLinkInput();
  filePicker.click();
}

filePicker.addEventListener("change", function () {
  selectedFile = this.files[0];
  document.getElementById("fileName").textContent =
    "Selected File: " + selectedFile.name;
  hideLinkInput();
  showVideoPreview(URL.createObjectURL(selectedFile));
});

// 4. Link
function toggleLinkInput() {
  hideVideoPreview(); 
  var linkInputContainer = document.getElementById("linkInputContainer");
  linkInputVisible = !linkInputVisible;
  if (linkInputVisible) {
    linkInputContainer.style.display = "block";
  } else {
    hideLinkInput();
  }
}

function hideLinkInput() {
  document.getElementById("linkInputContainer").style.display = "none";
  linkInput.value = "";
  selectedLink = "";
  hideIFramePreview();
}


function convertToEmbed(url) {
  // Regular expression to capture the video ID from the YouTube URL
  const videoIdPattern = /(?:v=|\/)([0-9A-Za-z_-]{11}).*/;
  const match = url.match(videoIdPattern);
  
  if (!match) {
      return null;  // Return null if no video ID is found
  }
  
  const videoId = match[1];
  const embedUrl = `https://www.youtube.com/embed/${videoId}`;
  return embedUrl;
}

linkInput.addEventListener("input", function () {
  selectedLink = this.value;
  selectedFile = null;
  document.getElementById("fileName").textContent = "";
  showIFramePreview(convertToEmbed(selectedLink));
});

// 5. Submit button
// Add smart comic options to the UI
function addSmartComicOptions() {
    const box = document.getElementById('box');
    const submitButton = box.querySelector('.submit-button');
    
    // Create options container
    const optionsDiv = document.createElement('div');
    optionsDiv.id = 'smart-options';
    optionsDiv.style.cssText = 'margin: 20px 0; padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px;';
    optionsDiv.innerHTML = `
        <h3 style="margin: 0 0 10px 0; font-size: 18px;">✨ Enhanced Comic Options</h3>
        <label style="display: block; margin: 5px 0; text-align: left; cursor: pointer;">
            <input type="checkbox" id="smart-mode" checked style="margin-right: 8px;">
            <span style="font-weight: bold;">Smart Frame Selection</span>
            <br>
            <span style="font-size: 12px; color: #aaa; margin-left: 25px;">
                Automatically selects the most engaging frames with perfect expressions
            </span>
        </label>
    `;
    
    // Insert before submit button
    box.insertBefore(optionsDiv, submitButton);
}

// Call on page load
document.addEventListener('DOMContentLoaded', addSmartComicOptions);

function submitForm() {
  // Get smart comic options
  const smartMode = document.getElementById('smart-mode').checked;
  
  // If file is selected
  if (selectedFile !== null && selectedLink === "") {
    submissionResult.textContent = "Your comic is being created";
    var formdata = new FormData();
    formdata.append("file", selectedFile);
    formdata.append("smart_mode", smartMode);

    var requestOptions = {
      method: "POST",
      body: formdata,
      redirect: "follow",
    };

    fetch("/uploader", requestOptions)
      .then((response) => response.text())
      .then((result) => {
        console.log(result);
        submissionResult.textContent = result;
      })
      .catch((error) => {
        console.log("error", error);
        alert(error);
      });
  }

  // If link is entered
  else if (selectedLink !== "" && selectedFile === null) {
    submissionResult.textContent = "Your comic is being created";

    var formdata = new FormData();
    formdata.append("link", linkInput.value);
    formdata.append("smart_mode", smartMode);

    var requestOptions = {
      method: "POST",
      body: formdata,
      redirect: "follow",
    };

    fetch("/handle_link", requestOptions)
      .then((response) => response.text())
      .then((result) => {
        console.log(result);
        submissionResult.textContent = result;
      })
      .catch((error) => {
        console.log("error", error);
        submissionResult.textContent = "An error has occurred";
      });
  } else {
    document.getElementById("submissionResult").textContent =
      "Please select either a file or enter a link.";
  }
}

// 6. Video preview
function showVideoPreview(url) {
  hideIFramePreview();
  videoPreview.src = url;
  videoPreview.style.display = "block";
  videoPreview.play();
  // no-op: background video removed
}

function hideVideoPreview() {
  videoPreview.src = "";
  videoPreview.style.display = "none";
  // no-op: background video removed
}

function showIFramePreview(url) {
  hideVideoPreview();
  iFramePreview.src = url;
  iFramePreview.style.display = "block";
  // no-op: background video removed
}

function hideIFramePreview() {
  iFramePreview.src = "";
  iFramePreview.style.display = "none";
  // no-op: background video removed
}