forked from krishpranav/whatsapp-bulk-message-sender
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.js
More file actions
executable file
·261 lines (202 loc) · 8.83 KB
/
scraper.js
File metadata and controls
executable file
·261 lines (202 loc) · 8.83 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
erfan4lx = (function(){
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var SCROLL_INTERVAL = 600,
SCROLL_INCREMENT = 450,
AUTO_SCROLL = true,
NAME_PREFIX = '',
UNKNOWN_CONTACTS_ONLY = false,
MEMBERS_QUEUE = {},
TOTAL_MEMBERS;
var scrollInterval, observer, membersList, header;
var start = function(){
membersList = document.querySelectorAll('span[title=You]')[0]?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode;
header = document.getElementsByTagName('header')[0];
if(!membersList){
document.querySelector("#main > header").firstChild.click();
membersList = document.querySelectorAll('span[title=You]')[0]?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode;
header = document.getElementsByTagName('header')[0];
}
observer = new MutationObserver(function (mutations, observer) {
scrapeData(); // fired when a mutation occurs
});
// the div to watch for mutations
observer.observe(membersList, {
childList: true,
subtree: true
});
TOTAL_MEMBERS = membersList.parentElement.parentElement.querySelector('span').innerText.match(/\d+/)[0]*1;
// click the `n more` button to show all members
document.querySelector("span[data-icon=down]")?.click()
//scroll to top before beginning
header.nextSibling.scrollTop = 100;
scrapeData();
if(AUTO_SCROLL) scrollInterval = setInterval(autoScroll, SCROLL_INTERVAL);
}
var autoScroll = function (){
if(!utils.scrollEndReached(header.nextSibling))
header.nextSibling.scrollTop += SCROLL_INCREMENT;
else
stop();
};
var stop = function(){
window.clearInterval(scrollInterval);
observer.disconnect();
console.log(`%c Extracted [${utils.queueLength()} / ${TOTAL_MEMBERS}] Members. Starting Download..`,`font-size:13px;color:white;background:green;border-radius:10px;`)
downloadAsCSV(['Name','Phone','Status']);
}
var scrapeData = function () {
var contact, status, name;
var memberCard = membersList.querySelectorAll(':scope > div');
for (let i = 0; i < memberCard.length; i++) {
status = memberCard[i].querySelectorAll('span[title]')[1] ? memberCard[i].querySelectorAll('span[title]')[1].title : "";
contact = scrapePhoneNum(memberCard[i]);
name = scrapeName(memberCard[i]);
if (contact.phone!='NIL' && !MEMBERS_QUEUE[contact.phone]) {
if (contact.isUnsaved) {
MEMBERS_QUEUE[contact.phone] = { 'Name': NAME_PREFIX + name,'Status': status };
continue;
} else if (!UNKNOWN_CONTACTS_ONLY) {
MEMBERS_QUEUE[contact.phone] = { 'Name': name, 'Status': status };
}
}else if(MEMBERS_QUEUE[contact.phone]){
MEMBERS_QUEUE[contact.phone].Status = status;
}
if(utils.queueLength() >= TOTAL_MEMBERS) {
stop();
break;
}
}
}
var scrapePhoneNum = function(el){
var phone, isUnsaved = false;
if (el.querySelector('img') && el.querySelector('img').src.match(/u=[0-9]*/)) {
phone = el.querySelector('img').src.match(/u=[0-9]*/)[0].substring(2).replace(/[+\s]/g, '');
} else {
var temp = el.querySelector('span[title]').getAttribute('title').match(/(.?)*[0-9]{3}$/);
if(temp){
phone = temp[0].replace(/\D/g,'');
isUnsaved = true;
}else{
phone = 'NIL';
}
}
return { 'phone': phone, 'isUnsaved': isUnsaved };
}
/**
* Scrapes name from HTML node
* @param {object} el - HTML node
* @returns {string} - returns name..if no name is present phone number is returned
*/
var scrapeName = function (el){
var expectedName;
expectedName = el.firstChild.firstChild.childNodes[1].childNodes[1].childNodes[1].querySelector('span').innerText;
if(expectedName == ""){
return el.querySelector('span[title]').getAttribute('title'); //phone number
}
return expectedName;
}
var downloadAsCSV = function (header) {
var groupName = document.querySelectorAll("#main > header span")[1].title;
var fileName = groupName.replace(/[^\d\w\s]/g,'') ? groupName.replace(/[^\d\w\s]/g,'') : 'WAXP-group-members';
var name = `${fileName}.csv`, data = `${header.join(',')}\n`;
if(utils.queueLength() > 0){
for (key in MEMBERS_QUEUE) {
// Wrapping each variable around double quotes to prevent commas in the string from adding new cols in CSV
// replacing any double quotes within the text to single quotes
if(header.includes('Status'))
data += `"${MEMBERS_QUEUE[key]['Name']}","${key}","${MEMBERS_QUEUE[key]['Status'].replace(/\"/g,"'")}"\n`;
else
data += `"${MEMBERS_QUEUE[key]['Name']}","${key}"\n`;
}
utils.createDownloadLink(data,name);
}else{
alert("Couldn't find any contacts with the given options");
}
}
var quickExport = function(){
var members = document.querySelectorAll("#main > header span")[2].title.replace(/ /g,'').split(',');
var groupName = document.querySelectorAll("#main > header span")[1].title;
var fileName = groupName.replace(/[^\d\w\s]/g,'') ? groupName.replace(/[^\d\w\s]/g,'') : 'WAXP-group-members';
fileName = `${fileName}.csv`;
members.pop(); //removing 'YOU' from array
MEMBERS_QUEUE = {};
for (i = 0; i < members.length; ++i) {
if (members[i].match(/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/)) {
MEMBERS_QUEUE[members[i]] = {
'Name': NAME_PREFIX + members[i]
};
continue;
} else if (!UNKNOWN_CONTACTS_ONLY) {
MEMBERS_QUEUE[members[i]] = {
'Name': members[i]
};
}
}
downloadAsCSV(['Name','Phone']);
}
var utils = (function(){
return {
scrollEndReached: function(el){
if((el.scrollHeight - (el.clientHeight + el.scrollTop)) == 0)
return true;
return false;
},
queueLength: function() {
var size = 0, key;
for (key in MEMBERS_QUEUE) {
if (MEMBERS_QUEUE.hasOwnProperty(key)) size++;
}
return size;
},
createDownloadLink: function (data,fileName) {
var a = document.createElement('a');
a.style.display = "none";
var url = window.URL.createObjectURL(new Blob([data], {
type: "data:attachment/text"
}));
a.setAttribute("href", url);
a.setAttribute("download", fileName);
document.body.append(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();
}
}
})();
return {
start: function(){
MEMBERS_QUEUE = {}; //reset
try {
start();
} catch (error) {
console.log(error, '\nRETRYING in 1 second')
setTimeout(start, 1000);
}
},
stop: function(){
stop()
},
options: {
set NAME_PREFIX(val){ NAME_PREFIX = val },
set SCROLL_INTERVAL(val){ SCROLL_INTERVAL = val },
set SCROLL_INCREMENT(val){ SCROLL_INCREMENT = val },
set AUTO_SCROLL(val){ AUTO_SCROLL = val },
set UNKNOWN_CONTACTS_ONLY(val){ UNKNOWN_CONTACTS_ONLY = val },
get NAME_PREFIX(){ return NAME_PREFIX },
get SCROLL_INTERVAL(){ return SCROLL_INTERVAL },
get SCROLL_INCREMENT(){ return SCROLL_INCREMENT },
get AUTO_SCROLL(){ return AUTO_SCROLL },
get UNKNOWN_CONTACTS_ONLY(){ return UNKNOWN_CONTACTS_ONLY },
},
quickExport: function(){
quickExport();
},
debug: function(){
return {
size: utils.queueLength(),
q: MEMBERS_QUEUE
}
}
}
})();
erfan4lx.quickExport()