Spaces:
Running
Running
Download script.js from sync2ai/phantom-caller-magic: direct link, hf CLI and curl.
- Browser
- Download file 4.86 kB
-
https://huggingface.co/spaces/sync2ai/phantom-caller-magic/resolve/main/script.js
- Command line
-
hf download hf://spaces/sync2ai/phantom-caller-magic/script.js
-
curl -L -o script.js https://huggingface.co/spaces/sync2ai/phantom-caller-magic/resolve/main/script.js
4.86 kB
| document.addEventListener('DOMContentLoaded', () => { | |
| const callForm = document.getElementById('callForm'); | |
| const callHistory = document.getElementById('callHistory'); | |
| let callTimeout; | |
| // Form validation | |
| function validatePhoneNumber(number) { | |
| return /^\+?[\d\s\-\(\)]{7, | |
| } | |
| // Initialize with some sample calls if empty | |
| if (callHistory.children.length === 1) { | |
| setTimeout(() => { | |
| simulateCall(Date.now()-5000, '+1 (555) 123-4567', '+1 (555) 987-6543'); | |
| simulateCall(Date.now()-10000, '+44 7911 123456', '+49 176 9876543'); | |
| }, 1000); | |
| } | |
| $/.test(number); | |
| } | |
| callForm.addEventListener('submit', (e) => { | |
| e.preventDefault(); | |
| const callerId = document.getElementById('callerId').value.trim(); | |
| const recipientNumber = document.getElementById('recipientNumber').value.trim(); | |
| // Clear any previous timeouts | |
| if (callTimeout) clearTimeout(callTimeout); | |
| if (!callerId || !recipientNumber) { | |
| showAlert('Please enter both caller ID and recipient number', 'error'); | |
| return; | |
| } | |
| if (!validatePhoneNumber(callerId) || !validatePhoneNumber(recipientNumber)) { | |
| showAlert('Please enter valid phone numbers', 'error'); | |
| return; | |
| } | |
| // Simulate call initiation | |
| const callId = Date.now(); // Unique ID for each call | |
| simulateCall(callId, callerId, recipientNumber); | |
| }); | |
| // Add service worker for PWA functionality | |
| if ('serviceWorker' in navigator) { | |
| window.addEventListener('load', () => { | |
| navigator.serviceWorker.register('/sw.js').then(registration => { | |
| console.log('ServiceWorker registration successful'); | |
| }).catch(err => { | |
| console.log('ServiceWorker registration failed: ', err); | |
| }); | |
| }); | |
| } | |
| function showAlert(message, type = 'success') { | |
| const alert = document.createElement('div'); | |
| alert.className = `fixed top-4 right-4 px-4 py-3 rounded-lg shadow-lg z-50 ${ | |
| type === 'error' ? 'bg-red-500' : 'bg-green-500' | |
| } text-white`; | |
| alert.textContent = message; | |
| document.body.appendChild(alert); | |
| setTimeout(() => { | |
| alert.remove(); | |
| }, 3000); | |
| } | |
| function simulateCall(callId, callerId, recipientNumber) { | |
| // Add to call history with unique ID | |
| const callItem = document.createElement('div'); | |
| callItem.dataset.callId = callId; | |
| callItem.className = 'bg-gray-800 p-4 rounded-lg flex justify-between items-center'; | |
| callItem.innerHTML = ` | |
| <div> | |
| <span class="text-purple-400 font-medium">${callerId}</span> | |
| <i data-feather="arrow-right" class="inline mx-2 w-4 h-4"></i> | |
| <span class="text-pink-400 font-medium">${recipientNumber}</span> | |
| </div> | |
| <div class="flex items-center space-x-3"> | |
| <span class="text-green-400 text-sm">Calling...</span> | |
| <div class="flex space-x-2"> | |
| <button class="end-call-btn p-1 rounded-full bg-red-500 hover:bg-red-600 transition"> | |
| <i data-feather="phone-off" class="w-4 h-4"></i> | |
| </button> | |
| <button class="delete-call-btn p-1 rounded-full bg-gray-600 hover:bg-gray-700 transition"> | |
| <i data-feather="trash-2" class="w-4 h-4"></i> | |
| </button> | |
| </div> | |
| </div> | |
| `; | |
| callHistory.prepend(callItem); | |
| feather.replace(); | |
| // Add event listeners to buttons | |
| callItem.querySelector('.end-call-btn').addEventListener('click', () => { | |
| endCall(callId, callItem); | |
| }); | |
| callItem.querySelector('.delete-call-btn').addEventListener('click', () => { | |
| callItem.remove(); | |
| showAlert('Call removed from history', 'info'); | |
| }); | |
| // Simulate call completion after random time (3-10 seconds) | |
| const callDuration = 3000 + Math.random() * 7000; | |
| callTimeout = setTimeout(() => { | |
| if (callItem.parentNode) { // Only if still in DOM | |
| callItem.querySelector('.text-green-400').textContent = 'Completed'; | |
| callItem.querySelector('.end-call-btn').remove(); | |
| feather.replace(); | |
| showAlert(`Call to ${recipientNumber} completed`); | |
| } | |
| }, callDuration); | |
| } | |
| function endCall(callId, callItem) { | |
| clearTimeout(callTimeout); | |
| callItem.querySelector('.text-green-400').textContent = 'Ended'; | |
| callItem.querySelector('.end-call-btn').remove(); | |
| feather.replace(); | |
| showAlert('Call ended manually', 'info'); | |
| } | |
| }); |