Skip to content

Commit b9e38db

Browse files
committed
Fix home timeline
1 parent 97d4279 commit b9e38db

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/utils/extractors/twitter.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
import { BaseExtractor, ExtractorResult } from './_base';
22

33
export class TwitterExtractor extends BaseExtractor {
4-
private mainTweet: Element | null;
5-
private threadTweets: Element[];
4+
private mainTweet: Element | null = null;
5+
private threadTweets: Element[] = [];
66

77
constructor(document: Document, url: string) {
88
super(document, url);
9-
// Get the main tweet and any thread tweets
10-
this.mainTweet = document.querySelector('article[data-testid="tweet"]');
11-
this.threadTweets = Array.from(document.querySelectorAll('article[data-testid="tweet"]')).slice(1);
9+
10+
// Get all tweets from the timeline
11+
const timeline = document.querySelector('[aria-label="Timeline: Conversation"]');
12+
if (!timeline) {
13+
// Try to find a single tweet if not in timeline view
14+
const singleTweet = document.querySelector('article[data-testid="tweet"]');
15+
if (singleTweet) {
16+
this.mainTweet = singleTweet;
17+
}
18+
return;
19+
}
20+
21+
// Get all tweets before any section with "Discover more" or similar headings
22+
const allTweets = Array.from(timeline.querySelectorAll('article[data-testid="tweet"]'));
23+
const firstSection = timeline.querySelector('section, h2')?.parentElement;
24+
25+
if (firstSection) {
26+
// Filter out tweets that appear after the first section
27+
allTweets.forEach((tweet, index) => {
28+
if (firstSection.compareDocumentPosition(tweet) & Node.DOCUMENT_POSITION_FOLLOWING) {
29+
allTweets.splice(index);
30+
return false;
31+
}
32+
});
33+
}
34+
35+
// Set main tweet and thread tweets
36+
this.mainTweet = allTweets[0] || null;
37+
this.threadTweets = allTweets.slice(1);
1238
}
1339

1440
canExtract(): boolean {
@@ -17,14 +43,15 @@ export class TwitterExtractor extends BaseExtractor {
1743

1844
extract(): ExtractorResult {
1945
const mainContent = this.extractTweet(this.mainTweet);
20-
const threadContent = this.threadTweets.map(tweet => this.extractTweet(tweet)).join('\n\n');
46+
const threadContent = this.threadTweets.map(tweet => this.extractTweet(tweet)).join('\n<hr>\n');
2147

2248
const contentHtml = `
2349
<div class="tweet-thread">
2450
<div class="main-tweet">
2551
${mainContent}
2652
</div>
2753
${threadContent ? `
54+
<hr>
2855
<div class="thread-tweets">
2956
${threadContent}
3057
</div>

0 commit comments

Comments
 (0)