-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
108 lines (88 loc) · 3.54 KB
/
script.js
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
const phoneInputsContainer = document.getElementById('phoneInputs');
const addPhoneButton = document.getElementById('addPhoneButton');
const sendSMSLink = document.getElementById('sendSMS');
const messageInput = document.getElementById('message');
const statusArea = document.getElementById('status');
function logStatus(message) {
statusArea.textContent = message;
}
let phoneInputCount = 0;
// Load phone numbers from local storage on page load
window.addEventListener('load', () => {
const storedPhoneNumbers = JSON.parse(localStorage.getItem('phoneNumbers')) || [];
phoneInputCount = storedPhoneNumbers.length;
storedPhoneNumbers.forEach((phoneNumber, index) => {
addPhoneInput(phoneNumber, index + 1);
});
});
addPhoneButton.addEventListener('click', () => {
phoneInputCount++;
addPhoneInput('', phoneInputCount);
});
// addPhoneButton.click()
sendSMSLink.addEventListener('click', () => {
let phoneNumbers = [];
for (let i = 1; i <= phoneInputCount; i++) {
const phoneInput = document.getElementById(`phone${i}`);
if (phoneInput.value) {
phoneNumbers.push(phoneInput.value);
}
}
const message = messageInput.value;
// example:
// <a href="sms://open?addresses=+12223334444,+12223334445?&body=Message%20Line%201%E2%80%A8Message%20Line%202">Send SMS to multi #s</a>
// vs
// sms:/open?addresses={phone number 1},{phone number 2},...
// https://stackoverflow.com/a/71807194
// Construct the SMS link
const smsLink = `sms://open?addresses=${phoneNumbers.map(n => "+1" + n).join(',')}?&body=${encodeURIComponent(message)}`;
logStatus(`SMS link: ${smsLink}`);
sendSMSLink.href = smsLink;
});
// Function to add a phone input field
function addPhoneInput(phoneNumber, index) {
const newPhoneInput = document.createElement('div');
newPhoneInput.classList.add('phone-input');
newPhoneInput.innerHTML = `
<label for="phone${index}">
Phone Number ${index}:
<button class="delete-phone" data-index="${index}">X</button>
</label>
<input type="tel" id="phone${index}" name="phone${index}" required value="${phoneNumber}">
`;
phoneInputsContainer.appendChild(newPhoneInput);
logStatus(`Phone number added. Total: ${phoneInputCount}`);
// Add event listener for delete button
const deleteButton = newPhoneInput.querySelector('.delete-phone');
deleteButton.addEventListener('click', handleDeletePhone);
}
// Function to handle phone number deletion
function handleDeletePhone(event) {
const indexToDelete = parseInt(event.target.dataset.index, 10);
const phoneInput = document.getElementById(`phone${indexToDelete}`);
phoneInput.parentElement.remove(); // Remove the entire phone input div
// Update phoneInputCount
phoneInputCount--;
// Update local storage
const phoneNumbers = [];
for (let i = 1; i <= phoneInputCount; i++) {
const phoneInput = document.getElementById(`phone${i}`);
if (phoneInput.value) {
phoneNumbers.push(phoneInput.value);
}
}
localStorage.setItem('phoneNumbers', JSON.stringify(phoneNumbers));
logStatus(`Phone number ${indexToDelete} deleted.`);
}
// Save phone numbers to local storage whenever an input changes
phoneInputsContainer.addEventListener('input', () => {
const phoneNumbers = [];
for (let i = 1; i <= phoneInputCount; i++) {
const phoneInput = document.getElementById(`phone${i}`);
if (phoneInput.value) {
phoneNumbers.push(phoneInput.value);
}
}
localStorage.setItem('phoneNumbers', JSON.stringify(phoneNumbers));
logStatus(`Saved phone numbers to local storage: ${JSON.stringify(phoneNumbers)}`);
});