Spaces:
Running
Running
Ryan Christian D. Deniega commited on
Commit ·
7b2808c
1
Parent(s): 0ee6199
fix(extension): exclude comment articles from Facebook post detection
Browse files- extension/content.js +24 -5
extension/content.js
CHANGED
|
@@ -32,11 +32,14 @@
|
|
| 32 |
|
| 33 |
const PLATFORM_CFG = {
|
| 34 |
facebook: {
|
|
|
|
|
|
|
|
|
|
| 35 |
post: [
|
| 36 |
'[data-pagelet^="FeedUnit"]',
|
| 37 |
'[data-pagelet^="GroupsFeedUnit"]',
|
|
|
|
| 38 |
'[role="article"]',
|
| 39 |
-
'[data-testid="post_message"]',
|
| 40 |
],
|
| 41 |
text: ['[data-ad-comet-preview="message"]', '[data-testid="post_message"]', '[dir="auto"]'],
|
| 42 |
image: 'img[src*="fbcdn"]',
|
|
@@ -370,8 +373,18 @@
|
|
| 370 |
|
| 371 |
function findPosts(root) {
|
| 372 |
for (const sel of POST_SELECTORS) {
|
| 373 |
-
const found = root.querySelectorAll(sel)
|
| 374 |
-
if (found.length)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
}
|
| 376 |
return []
|
| 377 |
}
|
|
@@ -380,9 +393,15 @@
|
|
| 380 |
for (const mutation of mutations) {
|
| 381 |
for (const node of mutation.addedNodes) {
|
| 382 |
if (node.nodeType !== 1) continue // element nodes only
|
| 383 |
-
// Check if the node itself matches
|
| 384 |
for (const sel of POST_SELECTORS) {
|
| 385 |
-
if (node.matches?.(sel)) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
}
|
| 387 |
// Check descendants
|
| 388 |
const posts = findPosts(node)
|
|
|
|
| 32 |
|
| 33 |
const PLATFORM_CFG = {
|
| 34 |
facebook: {
|
| 35 |
+
// data-pagelet attrs are structural and stable — they only exist on feed posts,
|
| 36 |
+
// NOT on comments. [role="article"] matches both posts AND comment items so
|
| 37 |
+
// we keep it only as a last resort with extra filtering in findPosts().
|
| 38 |
post: [
|
| 39 |
'[data-pagelet^="FeedUnit"]',
|
| 40 |
'[data-pagelet^="GroupsFeedUnit"]',
|
| 41 |
+
'[data-pagelet^="ProfileTimeline"]',
|
| 42 |
'[role="article"]',
|
|
|
|
| 43 |
],
|
| 44 |
text: ['[data-ad-comet-preview="message"]', '[data-testid="post_message"]', '[dir="auto"]'],
|
| 45 |
image: 'img[src*="fbcdn"]',
|
|
|
|
| 373 |
|
| 374 |
function findPosts(root) {
|
| 375 |
for (const sel of POST_SELECTORS) {
|
| 376 |
+
const found = Array.from(root.querySelectorAll(sel))
|
| 377 |
+
if (!found.length) continue
|
| 378 |
+
|
| 379 |
+
// For [role="article"] on Facebook we must filter out comment items.
|
| 380 |
+
// A comment article is always nested inside a parent [role="article"].
|
| 381 |
+
if (PLATFORM === 'facebook' && sel === '[role="article"]') {
|
| 382 |
+
const topLevel = found.filter(el => !el.parentElement?.closest('[role="article"]'))
|
| 383 |
+
if (topLevel.length) return topLevel
|
| 384 |
+
continue
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
return found
|
| 388 |
}
|
| 389 |
return []
|
| 390 |
}
|
|
|
|
| 393 |
for (const mutation of mutations) {
|
| 394 |
for (const node of mutation.addedNodes) {
|
| 395 |
if (node.nodeType !== 1) continue // element nodes only
|
| 396 |
+
// Check if the node itself matches a post selector
|
| 397 |
for (const sel of POST_SELECTORS) {
|
| 398 |
+
if (node.matches?.(sel)) {
|
| 399 |
+
// Skip comment articles on Facebook
|
| 400 |
+
if (PLATFORM === 'facebook' && sel === '[role="article"]'
|
| 401 |
+
&& node.parentElement?.closest('[role="article"]')) break
|
| 402 |
+
scheduleProcess(node)
|
| 403 |
+
break
|
| 404 |
+
}
|
| 405 |
}
|
| 406 |
// Check descendants
|
| 407 |
const posts = findPosts(node)
|