Free javascript Hosting


playground-module.js

Uploaded on Feb 05 2022 15:47 by floriandiud

function exportToCsv(filename, rows) {
var processRow = function (row) {
var finalVal = '';
for (var j = 0; j < row.length; j++) {
var innerValue = ((row[j] === null) || (typeof(row[j]) === "undefined")) ? '' : row[j].toString();
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
};
var result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0)
result = '"' + result + '"';
if (j > 0)
finalVal += ',';
finalVal += result;
}
return finalVal + '\n';
};

var csvFile = '';
for (var i = 0; i < rows.length; i++) {
csvFile += processRow(rows[i]);
}

var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });

// @ts-ignore
if ( typeof(window.navigator.msSaveBlob) === "function") { // IE 10+
// @ts-ignore
window.navigator.msSaveBlob(blob, filename);
} 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", filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}


const GROUP_SELECTOR = '.rq0escxv.l9j0dhe7.du4w35lb.j83agx80.cbu4d94t.pfnyh3mw.d2edcug0.ofv0k9yr.cwj9ozl2';
const MEMBER_SELECTOR = '.ue3kfks5.pw54ja7n.uo3d90p7.l82x9zwi.a8c37x1j:not(.oajrlxb2)'

function ScrapeMembers(){
const newMemberDiv = document.querySelector(GROUP_SELECTOR)
const memberDivs = newMemberDiv.querySelectorAll(MEMBER_SELECTOR)

const results = [
['Name', "ProfileLink", "Baseline", "GroupeLink", "Image"]
]

memberDivs.forEach((memberNode, i) => {
console.log(`Processing ${i}`);
let image
let name;
let baseLine;
let groupMemberLink;
let profileLink;

// Links
const links = memberNode.querySelectorAll('a')
links.forEach((link, j) => {
if(link.ariaLabel){
name = link.ariaLabel;
groupMemberLink = link.href
}
});
if(groupMemberLink){
// https://www.facebook.com/groups/1428363520516269/user/100071794474160/
const cleanText = groupMemberLink.replace(/https:\/\/www.facebook.com\/groups\/[0-9]+\/user\//g, '');
const userId = cleanText.replace(/\//g, '');
profileLink = `https://facebook.com/profile/${userId}`
}

// Image
const imageNode = memberNode.querySelector('image')
if(imageNode){
image = imageNode.href.baseVal
}

// Base line
const baselineNode = memberNode.querySelector('.pipptul6')
if(baselineNode){
baseLine = baselineNode.innerText
}

if(image || name || groupMemberLink || profileLink){
results.push([
name,
profileLink,
baseLine,
groupMemberLink,
image
])
}
});

return results
}


export function GenerateMemberCSV(){
// console.log(results)
const timestamp = new Date().toISOString()
const results = ScrapeMembers()
exportToCsv(`groupMemberExport-${timestamp}.csv`, results)
}

Back to list