Skip to content

Commit

Permalink
Don't duplicate images in quoted tweet
Browse files Browse the repository at this point in the history
  • Loading branch information
kepano committed Dec 9, 2024
1 parent 7adc496 commit 97d4279
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/utils/extractors/twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,19 @@ export class TwitterExtractor extends BaseExtractor {

const images: string[] = [];

// Skip images that are inside quoted tweets
const quotedTweet = tweet.querySelector('[aria-labelledby*="id__"]')?.querySelector('[data-testid="User-Name"]')?.closest('[aria-labelledby*="id__"]');

for (const selector of imageContainers) {
const elements = tweet.querySelectorAll(selector);

elements.forEach(img => {
// Skip if the image is inside a quoted tweet
if (quotedTweet?.contains(img)) {
return;
}

if (img instanceof HTMLImageElement) {
// Get the highest quality image by removing size parameters
const highQualitySrc = img.src.replace(/&name=\w+$/, '&name=large');
const cleanAlt = img.alt?.replace(/\s+/g, ' ').trim() || '';
images.push(`<img src="${highQualitySrc}" alt="${cleanAlt}" />`);
Expand Down

0 comments on commit 97d4279

Please sign in to comment.