Update static/script.js
Browse files- static/script.js +125 -102
static/script.js
CHANGED
|
@@ -1,102 +1,125 @@
|
|
| 1 |
-
document.addEventListener("DOMContentLoaded",
|
| 2 |
-
const fileInput = document.getElementById("fileInput");
|
| 3 |
-
const scanBtn = document.getElementById("scan-btn");
|
| 4 |
-
const resultDiv = document.getElementById("result");
|
| 5 |
-
const consentCheckbox = document.getElementById("user-consent");
|
| 6 |
-
const preview = document.getElementById("preview");
|
| 7 |
-
const
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
updateButton();
|
| 28 |
-
return;
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
+
const fileInput = document.getElementById("fileInput");
|
| 3 |
+
const scanBtn = document.getElementById("scan-btn");
|
| 4 |
+
const resultDiv = document.getElementById("result");
|
| 5 |
+
const consentCheckbox = document.getElementById("user-consent");
|
| 6 |
+
const preview = document.getElementById("preview");
|
| 7 |
+
const previewBox = document.getElementById("previewBox"); // ✅ NEW
|
| 8 |
+
const removeBtn = document.getElementById("removeBtn"); // ✅ NEW
|
| 9 |
+
const splash = document.getElementById("splash-screen");
|
| 10 |
+
const yearEl = document.getElementById("year");
|
| 11 |
+
|
| 12 |
+
// Initially hide preview
|
| 13 |
+
if (previewBox) previewBox.style.display = "none";
|
| 14 |
+
|
| 15 |
+
function updateButton() {
|
| 16 |
+
scanBtn.disabled = !(consentCheckbox.checked && fileInput.files.length > 0);
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
consentCheckbox.addEventListener("change", updateButton);
|
| 20 |
+
|
| 21 |
+
fileInput.addEventListener("change", function () {
|
| 22 |
+
const file = this.files[0];
|
| 23 |
+
|
| 24 |
+
if (!file) {
|
| 25 |
+
preview.style.display = "none";
|
| 26 |
+
if (previewBox) previewBox.style.display = "none";
|
| 27 |
+
updateButton();
|
| 28 |
+
return;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
if (file.size > 4 * 1024 * 1024) {
|
| 32 |
+
alert("File too big! Max 4MB allowed");
|
| 33 |
+
fileInput.value = "";
|
| 34 |
+
preview.style.display = "none";
|
| 35 |
+
if (previewBox) previewBox.style.display = "none";
|
| 36 |
+
updateButton();
|
| 37 |
+
return;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
preview.src = URL.createObjectURL(file);
|
| 41 |
+
preview.style.display = "block";
|
| 42 |
+
if (previewBox) previewBox.style.display = "block";
|
| 43 |
+
|
| 44 |
+
updateButton();
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
// ❌ REMOVE IMAGE BUTTON (X)
|
| 48 |
+
if (removeBtn) {
|
| 49 |
+
removeBtn.addEventListener("click", () => {
|
| 50 |
+
preview.src = "";
|
| 51 |
+
preview.style.display = "none";
|
| 52 |
+
fileInput.value = "";
|
| 53 |
+
if (previewBox) previewBox.style.display = "none";
|
| 54 |
+
updateButton();
|
| 55 |
+
});
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
scanBtn.addEventListener("click", async (e) => {
|
| 59 |
+
e.preventDefault();
|
| 60 |
+
|
| 61 |
+
if (!fileInput.files[0]) {
|
| 62 |
+
alert("Select image first");
|
| 63 |
+
return;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
resultDiv.innerHTML = "Analyzing...";
|
| 67 |
+
|
| 68 |
+
const formData = new FormData();
|
| 69 |
+
formData.append("image", fileInput.files[0]);
|
| 70 |
+
|
| 71 |
+
try {
|
| 72 |
+
const res = await fetch("/analyze", {
|
| 73 |
+
method: "POST",
|
| 74 |
+
body: formData
|
| 75 |
+
});
|
| 76 |
+
|
| 77 |
+
const data = await res.json();
|
| 78 |
+
|
| 79 |
+
if (data.error) {
|
| 80 |
+
resultDiv.innerHTML = "Error: " + data.error;
|
| 81 |
+
return;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
const analysis = data.analysis || {};
|
| 85 |
+
const metadata = data.metadata || {};
|
| 86 |
+
const width = metadata.width ?? "N/A";
|
| 87 |
+
const height = metadata.height ?? "N/A";
|
| 88 |
+
const size = metadata.size_mb ?? "N/A";
|
| 89 |
+
const status = analysis.is_real ? "Likely Real" : "Likely AI Generated";
|
| 90 |
+
|
| 91 |
+
resultDiv.innerHTML = `
|
| 92 |
+
<div style="text-align: center; margin-top: 15px;">
|
| 93 |
+
<p><b>Result:</b> ${status}</p>
|
| 94 |
+
<p><b>Confidence:</b> ${analysis.confidence ?? 0}%</p>
|
| 95 |
+
<p><b>Reason:</b> ${analysis.reason ?? "N/A"}</p>
|
| 96 |
+
<p><b>Detected Text:</b> ${data.text || "None"}</p>
|
| 97 |
+
<hr>
|
| 98 |
+
<p><b>Width:</b> ${width} px</p>
|
| 99 |
+
<p><b>Height:</b> ${height} px</p>
|
| 100 |
+
<p><b>Size:</b> ${size} MB</p>
|
| 101 |
+
</div>
|
| 102 |
+
`;
|
| 103 |
+
} catch (error) {
|
| 104 |
+
console.error("Analyze error:", error);
|
| 105 |
+
resultDiv.innerHTML = "Server connection error!";
|
| 106 |
+
}
|
| 107 |
+
});
|
| 108 |
+
|
| 109 |
+
window.addEventListener('load', () => {
|
| 110 |
+
if (splash) {
|
| 111 |
+
splash.style.display = "flex";
|
| 112 |
+
setTimeout(() => {
|
| 113 |
+
splash.style.transition = "opacity 0.8s ease";
|
| 114 |
+
splash.style.opacity = "0";
|
| 115 |
+
setTimeout(() => {
|
| 116 |
+
splash.style.display = "none";
|
| 117 |
+
}, 800);
|
| 118 |
+
}, 2000);
|
| 119 |
+
}
|
| 120 |
+
});
|
| 121 |
+
|
| 122 |
+
if (yearEl) {
|
| 123 |
+
yearEl.innerText = new Date().getFullYear();
|
| 124 |
+
}
|
| 125 |
+
});
|