Copy <script>ย
(function () {
function initAudioRecorder() {
// Your audio logic here
console.log("DOM ready โ recorder initialized");
debugger;
const form = document.querySelector(".super-form");
if (!form) return;
var hideFileUpload = document.querySelector('.f4d-audio-recording');
if(hideFileUpload) hideFileUpload.style.display = 'none';
const recordBtn = document.createElement("button");
recordBtn.type = "button";
recordBtn.textContent = "๐๏ธ Start Recording";
recordBtn.style.margin = "10px 0";
const deleteBtn = document.createElement("button");
deleteBtn.type = "button";
deleteBtn.textContent = "โ Delete Recording";
deleteBtn.style.display = "none";
//const audioPreview = document.createElement("audio");
//audioPreview.controls = true;
//audioPreview.style.display = "none";
// Find the Super Forms file input by name
const fileInput = form.querySelector('input[name="audio_recording"]').parentNode.querySelector('input[name="files[]"]');
let mediaRecorder;
let audioChunks = [];
recordBtn.addEventListener("click", async () => {
if (mediaRecorder && mediaRecorder.state === "recording") {
mediaRecorder.stop();
recordBtn.textContent = "๐๏ธ Start Recording";
} else {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
audioChunks = [];
mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) audioChunks.push(e.data);
};
mediaRecorder.onstop = () => {
debugger;
const audioBlob = new Blob(audioChunks, { type: "audio/webm" });
const audioFile = new File([audioBlob], "recording.webm", { type: "audio/webm" });
const audioURL = URL.createObjectURL(audioBlob);
// STEP 1: Get form ID
debugger;
var fileInput = form.querySelector('input[name="audio_recording"]').parentNode.querySelector('input[name="files[]"]');
const formEl = fileInput.closest(".super-form");
const formID = formEl.querySelector('input[name="hidden_form_id"]').value;
// STEP 2: Create Super Forms file entry
/*const fileData = {
name: audioFile.name,
size: audioFile.size,
type: audioFile.type,
file: audioFile,
src: audioURL
};*/
if (typeof SUPER.files === "undefined") SUPER.files = {};
if (typeof SUPER.files[formID] === "undefined") SUPER.files[formID] = {};
//SUPER.files[formID]["audio_recording"] = [fileData]; // name = your field name
SUPER.files[formID]["audio_recording"] = [audioFile];
// STEP 3: Update the preview (already done correctly)
const wrapper = fileInput.closest('.super-fileupload');
const fileListContainer = wrapper.parentNode.querySelector('.super-fileupload-files');
fileListContainer.innerHTML = "";
const fileDiv = document.createElement("div");
fileDiv.className = "super-uploaded";
fileDiv.setAttribute("data-name", "recording.webm");
fileDiv.setAttribute("title", "recording.webm");
fileDiv.setAttribute("data-type", "audio/webm");
const spanImg = document.createElement("span");
spanImg.className = "super-fileupload-image super-file-type-audio-webm";
spanImg.style.width = "180px";
spanImg.style.maxWidth = "180px";
const audioTag = document.createElement("audio");
audioTag.controls = true;
audioTag.src = audioURL;
spanImg.appendChild(audioTag);
const info = document.createElement("span");
info.className = "super-fileupload-info";
const fileName = document.createElement("span");
fileName.className = "super-fileupload-name";
fileName.textContent = "recording.webm";
const fileDelete = document.createElement("span");
fileDelete.className = "super-fileupload-delete";
fileDelete.style.cursor = "pointer";
fileDelete.title = "Delete";
fileDelete.addEventListener("click", () => {
fileListContainer.innerHTML = "";
fileInput.value = "";
deleteBtn.style.display = "none";
recordBtn.textContent = "๐๏ธ Start Recording";
// Clear from SUPER.files
if (SUPER.files?.[formID]?.["audio_recording"]) {
delete SUPER.files[formID]["audio_recording"];
}
});
info.appendChild(fileName);
info.appendChild(fileDelete);
fileDiv.appendChild(spanImg);
fileDiv.appendChild(info);
fileListContainer.appendChild(fileDiv);
};
mediaRecorder.start();
recordBtn.textContent = "๐ Stop Recording";
} catch (err) {
alert("Microphone access denied.");
}
}
});
deleteBtn.addEventListener("click", () => {
audioChunks = [];
//audioPreview.src = "";
//audioPreview.style.display = "none";
fileInput.value = ""; // Clear file input
deleteBtn.style.display = "none";
recordBtn.textContent = "๐๏ธ Start Recording";
});
// Insert into form (above the file field)
fileInput.parentElement.insertBefore(recordBtn, fileInput);
fileInput.parentElement.insertBefore(deleteBtn, fileInput);
//fileInput.parentElement.insertBefore(audioPreview, fileInput);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initAudioRecorder);
} else {
// DOM already ready
initAudioRecorder();
}
})();
</script>