-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (94 loc) · 2.94 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
const radioButtons = document.querySelectorAll('input[name="Whatsapp1"]');
let numberInput = document.getElementById("phoneInput");
let messageInput = document.getElementById("messageInput");
let inputFile = document.getElementById("inputFile");
let sent = document.getElementById("sent");
let totalNumbers = document.getElementById("total");
let percentage = document.getElementById("percentage");
let messageAlert = document.querySelector(".message");
let method = "api";
let numbers;
let numIndex = 0;
function send() {
numIndex++;
if (inputFile.value) {
CheckForCompletion();
sent.innerHTML = numIndex;
numberInput.value = numbers[numIndex];
let cla = Math.floor((numIndex * 100) / numbers.length);
percentage.innerHTML = cla + "%";
}
let a = document.createElement("a");
let link = `https://${method}.whatsapp.com/send/?phone=${numberInput.value}&text=${messageInput.value}`;
a.href = link;
a.setAttribute("target", "_blank");
a.click();
}
function CheckForCompletion() {
if (numIndex >= numbers.length) {
swal({
title: "Task Completed",
text: "Successfully message sent to all numbers",
icon: "success",
});
messageAlert.classList.remove("show");
numIndex = 0;
numbers.length = 0;
inputFile.value = null;
}
}
radioButtons.forEach((radio) => {
radio.addEventListener("change", () => {
if (radio.checked) {
method = radio.id;
document.body.classList.toggle('web')
}
});
});
inputFile.addEventListener("change", function (event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const content = e.target.result;
const numberArray = extractNumbersFromCSV(content);
numbers = numberArray;
console.log(numberArray[1]);
numberInput.value = numberArray[0];
totalNumbers.innerHTML = numberArray.length;
messageAlert.classList.add("show");
};
reader.readAsText(file);
}
});
function extractNumbersFromCSV(content) {
const lines = content.split(/\r?\n/); // Split into lines, handle both Windows and Unix line endings
if (lines.length === 0) return [];
const header = lines[0].split(/[,;\t]/).map((h) => h.trim().toLowerCase()); // Split header by common delimiters and trim whitespace
const numberIndex = header.indexOf("number");
if (numberIndex === -1) {
swal({
title: "Number not found",
text: "Please ensure that your file has number row",
icon: "error",
});
return [];
}
swal({
title: "Uploaded",
text: "We successfully detect number column",
icon: "success",
});
const numbersSet = new Set(
lines
.slice(1)
.map((line) => {
const cells = line.split(/[,;\t]/).map((cell) => cell.trim());
return parseFloat(cells[numberIndex]);
})
.filter((num) => !isNaN(num))
);
return Array.from(numbersSet);
}
let date = new Date()
date.toLocaleDateString()