Skip to content

Instantly share code, notes, and snippets.

@silasrm
Last active December 28, 2022 18:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silasrm/2f5e87926d08ce21e25c1aed5b9ffae9 to your computer and use it in GitHub Desktop.
Save silasrm/2f5e87926d08ce21e25c1aed5b9ffae9 to your computer and use it in GitHub Desktop.
Run this code in developer tools from Whatsapp Web. Get contacts phone number using image profile URL
// @gist https://gist.github.com/silasrm/2f5e87926d08ce21e25c1aed5b9ffae9
// Get contacts phone number using image profile URL
var script = document.createElement('script');script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js";document.getElementsByTagName('head')[0].appendChild(script);
var csvData = [];
var uniqueArray = function(arrArg) {
return arrArg.filter(function(elem, pos,arr) {
return arr.indexOf(elem) == pos;
});
};
function get() {
document.querySelectorAll('._1WliW img').forEach(e => {
if (e.src.search(/^http/) !== -1) {
var a = e.src.split('u=');
if (a !== 'undefined' ) {
var b = a[1].split('%40');
if (b !== 'undefined') {
if (b[0].search(/\-/) !== -1) {
// console.log(b[0].split('-')[0]);
csvData.push(b[0].split('-')[0]);
} else {
// console.log(b[0]);
csvData.push(b[0]);
}
}
}
}
});
document.querySelectorAll('._1wjpf').forEach(e => {
let number = e.innerHTML.replace(/\D/g, '');
if (number.length >= 8 && number.length <= 12) {
csvData.push(number);
}
});
}
function createCsv(data) {
var csvContent = '';
data = uniqueArray(data);
data.forEach(function(item, index) {
csvContent += index < data.length ? item + '\n' : item;
});
var download = function(content, fileName, mimeType) {
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';
if (navigator.msSaveBlob) { // IE10
navigator.msSaveBlob(new Blob([content], {
type: mimeType
}), fileName);
} else if (URL && 'download' in a) { //html5 A[download]
a.href = URL.createObjectURL(new Blob([content], {
type: mimeType
}));
a.setAttribute('download', fileName);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
}
}
download(csvContent, 'dowload.csv', 'text/csv;encoding:utf-8');
}
function extractZapContacts(targetElement) {
var scrollHeight = $(targetElement).get(0).scrollHeight;
var clientHeight = $(targetElement).get(0).clientHeight;
var steps = parseInt(scrollHeight / clientHeight);
steps = steps + 1;
var count = 1;
console.log('getting ' + count + ' of ' + steps);
for (var i = 1; i <= steps; i++) {
get();
$(targetElement).delay(1200).animate(
{ scrollTop: clientHeight * i },
{
complete: function () {
if (count < steps) {
count++;
console.log('getting ' + count + ' of ' + steps);
}
get();
}
}
);
if (i === steps) {
targetElement.animate(
{ scrollTop: 0 },
{
complete: function () {
// console.log(csvData);
console.log('downloading....');
createCsv(csvData);
}
}
);
}
}
};
extractZapContacts($('._1NrpZ'));
function convertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
function exportCSVFile(headers, items, fileTitle) {
if (headers) {
items.unshift(headers);
}
// Convert Object to JSON
var jsonObject = JSON.stringify(items);
var csv = this.convertToCSV(jsonObject);
var exportedFilenmae = fileTitle + '.csv' || 'export.csv';
var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, exportedFilenmae);
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", exportedFilenmae);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}
let paneSide = document.getElementById("pane-side");
let internalName = "";
for (p in paneSide) {
if (p.indexOf("reactInternal") >= 0) {
internalName = p; break;
}
}
let allChats = paneSide[internalName];
allChats = allChats.memoizedProps.children[0][0].props.chats;
let chatList = []
for(let index in allChats){
let chat = allChats[index];
if(chat.isUser){
chatList.push({
//name: chat.name || "",
number: chat.id.user
});
}
}
exportCSVFile({number: "number"}, chatList, 'wpp_chat_list');
@dqureshiumar
Copy link

This does not work now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment