Sergidev commited on
Commit
f47cd32
·
verified ·
1 Parent(s): 53013c4

Create script.js

Browse files
Files changed (1) hide show
  1. static/script.js +92 -0
static/script.js ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener('DOMContentLoaded', () => {
2
+ const dropArea = document.getElementById('drop-area');
3
+ const fileInput = document.getElementById('fileInput');
4
+ const options = document.getElementById('options');
5
+ const multiplicationFactor = document.getElementById('multiplicationFactor');
6
+ const factorValue = document.getElementById('factorValue');
7
+ const processButton = document.getElementById('processButton');
8
+ const result = document.getElementById('result');
9
+ const audioPlayer = document.getElementById('audioPlayer');
10
+ const downloadLink = document.getElementById('downloadLink');
11
+
12
+ let isDecrypting = false;
13
+
14
+ ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
15
+ dropArea.addEventListener(eventName, preventDefaults, false);
16
+ });
17
+
18
+ function preventDefaults(e) {
19
+ e.preventDefault();
20
+ e.stopPropagation();
21
+ }
22
+
23
+ ['dragenter', 'dragover'].forEach(eventName => {
24
+ dropArea.addEventListener(eventName, highlight, false);
25
+ });
26
+
27
+ ['dragleave', 'drop'].forEach(eventName => {
28
+ dropArea.addEventListener(eventName, unhighlight, false);
29
+ });
30
+
31
+ function highlight() {
32
+ dropArea.classList.add('highlight');
33
+ }
34
+
35
+ function unhighlight() {
36
+ dropArea.classList.remove('highlight');
37
+ }
38
+
39
+ dropArea.addEventListener('drop', handleDrop, false);
40
+
41
+ function handleDrop(e) {
42
+ const dt = e.dataTransfer;
43
+ const files = dt.files;
44
+ handleFiles(files);
45
+ }
46
+
47
+ fileInput.addEventListener('change', function() {
48
+ handleFiles(this.files);
49
+ });
50
+
51
+ function handleFiles(files) {
52
+ if (files.length > 0) {
53
+ const file = files[0];
54
+ isDecrypting = file.type === 'audio/mpeg';
55
+ options.style.display = 'block';
56
+ processButton.textContent = isDecrypting ? 'Deconvert' : 'Convert';
57
+ }
58
+ }
59
+
60
+ multiplicationFactor.addEventListener('input', function() {
61
+ factorValue.textContent = this.value;
62
+ });
63
+
64
+ processButton.addEventListener('click', function() {
65
+ const file = fileInput.files[0];
66
+ if (!file) {
67
+ alert('Please select a file first.');
68
+ return;
69
+ }
70
+
71
+ const formData = new FormData();
72
+ formData.append('file', file);
73
+ formData.append('factor', multiplicationFactor.value);
74
+
75
+ fetch('/process', {
76
+ method: 'POST',
77
+ body: formData
78
+ })
79
+ .then(response => response.blob())
80
+ .then(blob => {
81
+ const url = URL.createObjectURL(blob);
82
+ result.style.display = 'block';
83
+ audioPlayer.src = url;
84
+ downloadLink.href = url;
85
+ downloadLink.download = isDecrypting ? 'deconverted_file.wav' : 'converted_file.mp3';
86
+ })
87
+ .catch(error => {
88
+ console.error('Error:', error);
89
+ alert('An error occurred while processing the file.');
90
+ });
91
+ });
92
+ });