diff --git a/src/engines/Bluesky.ts b/src/engines/Bluesky.ts index df1937d..54b2ec5 100644 --- a/src/engines/Bluesky.ts +++ b/src/engines/Bluesky.ts @@ -26,21 +26,23 @@ export default class Bluesky extends ScrapeEngineBase { scrapeDocument(document: Document): ScrapeResult { const result = new ScrapeResult(this.name); + + type Candidate = { el: Element; post: ScrapedPost }; + const candidates: Candidate[] = []; + + // Images const imgEls = Array.from(document.querySelectorAll("div > img")); - let els = imgEls.filter((x) => x.src.indexOf("feed_fullsize") != -1); - // Only add the 'normal' feed elements if no post is in fullscreen. if (els.length == 0) { this.log("Post is not fullscreen."); els = imgEls.filter((x) => x.src.indexOf("feed") != -1); } - for (const el of els) { const post = new ScrapedPost(); post.pageUrl = document.location.href; post.contentUrl = getFullSizeImg(el.src); - result.tryAddPost(post); + candidates.push({ el, post }); } // Videos @@ -56,13 +58,21 @@ export default class Bluesky extends ScrapeEngineBase { this.log(`Could not parse poster URL: ${poster}`); continue; } - const post = new ScrapedPost(); post.pageUrl = document.location.href; post.contentUrl = blobUrl; post.contentType = "video"; - result.tryAddPost(post); + candidates.push({ el: v, post }); } + + candidates.sort((a, b) => { + const pos = a.el.compareDocumentPosition(b.el); + if (pos & Node.DOCUMENT_POSITION_FOLLOWING) return -1; + if (pos & Node.DOCUMENT_POSITION_PRECEDING) return 1; + return 0; + }); + + for(const c of candidates) result.tryAddPost(c.post); return result; }