Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
394 lines (331 loc) · 13.7 KB
/
Copy pathcontent.js
File metadata and controls
394 lines (331 loc) · 13.7 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Content script for Facebook Groups Monitor Extension
(function() {
'use strict';
let isScrapingPosts = false;
let scrapingAttempts = 0;
const MAX_ATTEMPTS = 3;
console.log('Content script loaded on:', window.location.href);
// Listen for messages from background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('Content script: Received message:', message);
if (message.action === 'scrapePosts' || message.action === 'triggerScraping') {
console.log('Content script: Received scraping trigger');
scrapePosts();
sendResponse({ success: true });
}
return true;
});
// Listen for custom events to trigger scraping
window.addEventListener('triggerPostScraping', () => {
console.log('Content script: Triggered by custom event');
scrapePosts();
});
// Auto-run post scraping when on a Facebook group page
if (window.location.href.includes('facebook.com/groups/')) {
console.log('Content script: Auto-running on Facebook group page');
// Wait for page to load before scraping
setTimeout(() => {
console.log('Content script: Auto-trigger after 5 seconds');
scrapePosts();
}, 5000);
}
// Listen for navigation changes
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
if (url.includes('facebook.com/groups/')) {
console.log('Content script: Navigation detected, triggering scraping');
setTimeout(() => {
scrapePosts();
}, 3000);
}
}
}).observe(document, { subtree: true, childList: true });
function scrapePosts() {
if (isScrapingPosts) {
console.log('Content script: Already scraping posts, skipping...');
return;
}
isScrapingPosts = true;
scrapingAttempts++;
console.log(`Content script: Starting post scraping attempt ${scrapingAttempts}/${MAX_ATTEMPTS}`);
// Wait for page to be ready
if (document.readyState !== 'complete') {
console.log('Content script: Page not ready, waiting...');
setTimeout(() => {
isScrapingPosts = false;
scrapePosts();
}, 2000);
return;
}
try {
// Try multiple extraction methods
let posts = [];
// Method 1: Look for feed posts
const feedPosts = extractFeedPosts();
if (feedPosts.length > 0) {
console.log(`Content script: Found ${feedPosts.length} posts using feed method`);
posts = feedPosts;
}
// Method 2: If no posts found, try broader search
if (posts.length === 0) {
const broadPosts = extractWithBroadSearch();
if (broadPosts.length > 0) {
console.log(`Content script: Found ${broadPosts.length} posts using broad search`);
posts = broadPosts;
}
}
// Method 3: Last resort - try to find any text content
if (posts.length === 0) {
const textPosts = extractTextContent();
if (textPosts.length > 0) {
console.log(`Content script: Found ${textPosts.length} posts using text extraction`);
posts = textPosts;
}
}
// Sort posts by timestamp (newest first)
if (posts.length > 0) {
posts.sort((a, b) => {
const timeA = new Date(a.timestamp).getTime();
const timeB = new Date(b.timestamp).getTime();
return timeB - timeA;
});
console.log(`Content script: Successfully extracted ${posts.length} posts`);
console.log('Content script: Latest post:', posts[0]);
} else {
console.log('Content script: No posts found with any method');
// If no posts found and we haven't reached max attempts, try again
if (scrapingAttempts < MAX_ATTEMPTS) {
console.log('Content script: Retrying in 5 seconds...');
setTimeout(() => {
isScrapingPosts = false;
scrapePosts();
}, 5000);
return;
}
}
// Send posts data to background script
chrome.runtime.sendMessage({
action: 'postsData',
posts: posts
});
} catch (error) {
console.error('Content script: Error during scraping:', error);
// Send empty result
chrome.runtime.sendMessage({
action: 'postsData',
posts: []
});
} finally {
isScrapingPosts = false;
}
}
function extractFeedPosts() {
const posts = [];
// Updated selectors for current Facebook structure
const feedSelectors = [
'[role="main"] [role="article"]',
'[data-pagelet="FeedUnit_0"]',
'[data-pagelet^="FeedUnit"]',
'.userContentWrapper',
'[data-testid="story-subtitle"]',
'div[data-ad-preview="message"]'
];
let postElements = [];
for (const selector of feedSelectors) {
postElements = Array.from(document.querySelectorAll(selector));
if (postElements.length > 0) {
console.log(`Content script: Found ${postElements.length} elements with selector: ${selector}`);
break;
}
}
postElements.forEach((element, index) => {
try {
const post = extractPostFromElement(element, index, 'feed');
if (post && post.content) {
posts.push(post);
}
} catch (error) {
console.log(`Content script: Error extracting post ${index}:`, error);
}
});
return posts;
}
function extractWithBroadSearch() {
const posts = [];
// Look for any divs that might contain posts
const allDivs = document.querySelectorAll('div');
const potentialPosts = [];
allDivs.forEach(div => {
const text = div.textContent?.trim() || '';
// Filter for potential post content
if (text.length > 30 && text.length < 5000) {
// Check if it has links or looks like a post
const hasLinks = div.querySelector('a[href*="facebook.com"]');
const hasTimeIndicator = text.match(/\d+[smhdw]|ago|hour|minute|day/i);
const hasAuthor = div.querySelector('a[role="link"]');
if (hasLinks || hasTimeIndicator || hasAuthor) {
potentialPosts.push(div);
}
}
});
console.log(`Content script: Found ${potentialPosts.length} potential post elements`);
// Take the first 10 potential posts to avoid processing too many
potentialPosts.slice(0, 10).forEach((element, index) => {
try {
const post = extractPostFromElement(element, index, 'broad');
if (post && post.content) {
posts.push(post);
}
} catch (error) {
console.log(`Content script: Error extracting broad post ${index}:`, error);
}
});
return posts;
}
function extractTextContent() {
const posts = [];
// Last resort: just grab some text content from the page
const textElements = document.querySelectorAll('p, div[dir="auto"], span');
const foundTexts = new Set();
Array.from(textElements).forEach((element, index) => {
const text = element.textContent?.trim();
if (text && text.length > 20 && text.length < 2000 && !foundTexts.has(text)) {
foundTexts.add(text);
const post = {
id: `text_${index}_${Date.now()}`,
content: text,
author: 'Unknown User',
timestamp: new Date().toISOString(),
url: window.location.href,
type: 'text_extraction'
};
posts.push(post);
// Limit to 5 text posts
if (posts.length >= 5) {
return;
}
}
});
console.log(`Content script: Extracted ${posts.length} text posts as fallback`);
return posts;
}
function extractPostFromElement(element, index, method) {
const post = {
id: `${method}_post_${index}_${Date.now()}`,
content: '',
author: '',
timestamp: new Date().toISOString(),
url: window.location.href,
type: method,
images: [],
links: []
};
// Extract text content
const textSelectors = [
'[data-testid="post_message"]',
'.userContent',
'[data-ad-preview="message"]',
'div[dir="auto"]',
'p',
'span'
];
for (const selector of textSelectors) {
const textElement = element.querySelector(selector);
if (textElement && textElement.textContent?.trim()) {
post.content = textElement.textContent.trim();
break;
}
}
// If no content found, use element's direct text
if (!post.content) {
const directText = element.textContent?.trim();
if (directText && directText.length > 10) {
// Take first reasonable chunk of text
post.content = directText.substring(0, 500);
}
}
// Extract author name
const authorSelectors = [
'h3 a[role="link"]',
'a[role="link"] strong',
'.actor-link',
'[data-testid="post_author_name"]',
'strong a'
];
for (const selector of authorSelectors) {
const authorElement = element.querySelector(selector);
if (authorElement && authorElement.textContent?.trim()) {
post.author = authorElement.textContent.trim();
break;
}
}
// Extract timestamp
const timeSelectors = [
'a[role="link"][tabindex="0"]',
'.timestampContent',
'abbr[data-utime]',
'time'
];
for (const selector of timeSelectors) {
const timeElement = element.querySelector(selector);
if (timeElement) {
const timeValue = timeElement.getAttribute('data-utime') ||
timeElement.getAttribute('title') ||
timeElement.textContent;
if (timeValue) {
post.timestamp = parseTimestamp(timeValue);
if (timeElement.href) {
post.url = timeElement.href;
}
break;
}
}
}
// Extract images
const images = element.querySelectorAll('img[src]');
images.forEach(img => {
if (img.src && !img.src.includes('emoji') && !img.src.includes('static') && img.src.startsWith('http')) {
post.images.push(img.src);
}
});
return post.content ? post : null;
}
function parseTimestamp(timeText) {
try {
// Unix timestamp
if (/^\d{10}$/.test(timeText)) {
return new Date(parseInt(timeText) * 1000).toISOString();
}
// ISO date string
const date = new Date(timeText);
if (!isNaN(date.getTime())) {
return date.toISOString();
}
// Relative time parsing
const now = new Date();
if (timeText.includes('now') || timeText.includes('Just now')) {
return now.toISOString();
}
const patterns = [
{ regex: /(\d+)\s*s/i, multiplier: 1000 },
{ regex: /(\d+)\s*m/i, multiplier: 60 * 1000 },
{ regex: /(\d+)\s*h/i, multiplier: 60 * 60 * 1000 },
{ regex: /(\d+)\s*d/i, multiplier: 24 * 60 * 60 * 1000 },
{ regex: /(\d+)\s*w/i, multiplier: 7 * 24 * 60 * 60 * 1000 }
];
for (const pattern of patterns) {
const match = timeText.match(pattern.regex);
if (match) {
const value = parseInt(match[1]);
return new Date(now.getTime() - value * pattern.multiplier).toISOString();
}
}
return now.toISOString();
} catch (error) {
return new Date().toISOString();
}
}
})();