-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.ts
216 lines (189 loc) · 6.34 KB
/
main.ts
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
// Utils to export a Javascript double array into a CSV file
function exportToCsv(filename: string, rows: any[][]): void {
var processRow = function (row: any[]) {
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;' });
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);
}
}
declare var members_list:string[][]; // Will store facebook group members
window.members_list = window.members_list || [[
'Profile Id',
'Full Name',
'ProfileLink',
'Bio',
'Image Src',
'Groupe Id',
'Group Joining Text',
'Profile Type'
]]
// Add a Download button to export parsed member into a CSV file
function buildCTABtn(): HTMLElement{
const canvas = document.createElement('div')
const canvasStyles = [
'position: fixed;',
'top: 0;',
'left: 0;',
'z-index: 10;',
'width: 100%;',
'height: 100%;',
'pointer-events: none;'
]
canvas.setAttribute('style', canvasStyles.join(''))
const btn = document.createElement('div')
const btnStyles = [
'position: absolute;',
'bottom: 30px;',
'right: 130px;',
'color: white;',
'min-width: 150px;',
'background: var(--primary-button-background);',
'border-radius: var(--button-corner-radius);',
'padding: 0px 12px;',
'cursor: pointer;',
'font-weight:600;',
'font-size:15px;',
'display: inline-flex;',
'pointer-events: auto;',
'height: 36px;',
'align-items: center;',
'justify-content: center;'
]
btn.setAttribute('style', btnStyles.join(''))
const downloadText = document.createTextNode('Download\u00A0')
const numberSpan = document.createElement("span");
numberSpan.setAttribute('id', 'fb-group-scraper-number-tracker')
numberSpan.textContent = "0";
const memberText = document.createTextNode('\u00A0members')
btn.appendChild(downloadText)
btn.appendChild(numberSpan)
btn.appendChild(memberText)
btn.addEventListener('click', function() {
const timestamp = new Date().toISOString()
exportToCsv(`groupMemberExport-${timestamp}.csv`, window.members_list)
});
canvas.appendChild(btn);
document.body.appendChild(canvas);
return canvas;
}
function processResponse(dataGraphQL: any){
// Only look for Group GraphQL responses
let data: any;
if(dataGraphQL?.data?.group){
// Initial Group members page
data = dataGraphQL.data.group;
} else if(dataGraphQL?.data?.node?.__typename === 'Group'){
// New members load on scroll
data = dataGraphQL.data.node;
} else {
// If no group members, return fast
return;
}
let membersEdges: Array<any>;
// Both are used (new_forum_members seems to be the new way)
if(data?.new_members?.edges){
membersEdges = data.new_members.edges;
}else if(data?.new_forum_members?.edges){
membersEdges = data.new_forum_members.edges;
}else{
return
}
const membersData = membersEdges.map(memberNode=>{
// Member Data
const {
id,
name,
bio_text,
url,
profile_picture,
__isProfile:profileType
} = memberNode.node
// Group Joining Info
const joiningText = memberNode?.join_status_text?.text || memberNode?.membership?.join_status_text?.text;
// Facebook Group Id
const groupId = memberNode.node.group_membership?.associated_group.id
return [
id,
name,
url,
bio_text?.text || '',
profile_picture?.uri || '',
groupId,
joiningText || '',
profileType
]
})
window.members_list.push(...membersData)
// Update member tracker counter
const tracker = document.getElementById('fb-group-scraper-number-tracker')
if(tracker){
tracker.textContent = window.members_list.length.toString()
}
}
function parseResponse(dataRaw: string): void{
let dataGraphQL: Array<any> = [];
try{
dataGraphQL.push(JSON.parse(dataRaw))
}catch(err){
// Sometime Facebook return multiline response
const splittedData = dataRaw.split("\n");
// If not a multiline response
if(splittedData.length<=1){
console.error('Fail to parse API response', err);
return;
}
// Multiline response. Parse each response
for(let i=0; i<splittedData.length;i++){
const newDataRaw = splittedData[i];
try{
dataGraphQL.push(JSON.parse(newDataRaw));
}catch(err2){
console.error('Fail to parse API response', err);
}
}
}
for(let j=0; j<dataGraphQL.length; j++){
processResponse(dataGraphQL[j])
}
}
function main(): void {
buildCTABtn()
// Watch API calls to find GraphQL responses to parse
const matchingUrl = '/api/graphql/';
let send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function() {
this.addEventListener('readystatechange', function() {
if (this.responseURL.includes(matchingUrl) && this.readyState === 4) {
parseResponse(this.responseText);
}
}, false);
send.apply(this, arguments);
};
}
main();