Compare commits

..

2 Commits

Author SHA1 Message Date
Nes370 fe0a543bf4 Update Bluesky.ts
Added a filter to only visible elements to reduce clutter from posts in feed.
2026-10-13 23:46:36 -07:00
Nes370 1575a7a2f1 Update Bluesky.ts
An attempt to order posts by their position in the page.
2026-10-13 23:14:14 -07:00
1 changed files with 38 additions and 6 deletions

View File

@ -26,21 +26,25 @@ export default class Bluesky extends ScrapeEngineBase {
scrapeDocument(document: Document): ScrapeResult { scrapeDocument(document: Document): ScrapeResult {
const result = new ScrapeResult(this.name); const result = new ScrapeResult(this.name);
type Candidate = { el: Element; post: ScrapedPost };
const candidates: Candidate[] = [];
// Images
const imgEls = Array.from<HTMLImageElement>(document.querySelectorAll("div > img")); const imgEls = Array.from<HTMLImageElement>(document.querySelectorAll("div > img"));
let els = imgEls.filter((x) => x.src.indexOf("feed_fullsize") != -1); let els = imgEls.filter((x) => x.src.indexOf("feed_fullsize") != -1);
// Only add the 'normal' feed elements if no post is in fullscreen. // Only add the 'normal' feed elements if no post is in fullscreen.
if (els.length == 0) { if (els.length == 0) {
this.log("Post is not fullscreen."); this.log("Post is not fullscreen.");
els = imgEls.filter((x) => x.src.indexOf("feed") != -1); els = imgEls.filter((x) => x.src.indexOf("feed") != -1);
} }
for (const el of els) { for (const el of els) {
if(!isVisible(el)) continue;
const post = new ScrapedPost(); const post = new ScrapedPost();
post.pageUrl = document.location.href; post.pageUrl = document.location.href;
post.contentUrl = getFullSizeImg(el.src); post.contentUrl = getFullSizeImg(el.src);
result.tryAddPost(post); candidates.push({ el, post });
} }
// Videos // Videos
@ -48,6 +52,8 @@ export default class Bluesky extends ScrapeEngineBase {
document.querySelectorAll('video[poster^="https://video.bsky.app/watch"]') document.querySelectorAll('video[poster^="https://video.bsky.app/watch"]')
); );
for (const v of videoEls) { for (const v of videoEls) {
if(!isVisible(v)) continue;
const poster = v.getAttribute("poster"); const poster = v.getAttribute("poster");
if (!poster) continue; if (!poster) continue;
@ -56,14 +62,22 @@ export default class Bluesky extends ScrapeEngineBase {
this.log(`Could not parse poster URL: ${poster}`); this.log(`Could not parse poster URL: ${poster}`);
continue; continue;
} }
const post = new ScrapedPost(); const post = new ScrapedPost();
post.pageUrl = document.location.href; post.pageUrl = document.location.href;
post.contentUrl = blobUrl; post.contentUrl = blobUrl;
post.contentType = "video"; 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; return result;
} }
@ -92,3 +106,21 @@ export default class Bluesky extends ScrapeEngineBase {
} }
} }
} }
/**
* A lightweight visibility check. We use this rather than IntersectionObserver
* because scrapeDocument is synchronous and must return immediately.
*/
function isVisible(el: Element): boolean {
const rect = el.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) return false;
if (rect.bottom <= 0 || rect.top >= window.innerHeight) return false;
if (rect.right <= 0 || rect.left >= window.innerWidth) return false;
const style = window.getComputedStyle(el);
if (style.display === "none") return false;
if (style.visibility === "hidden") return false;
if (style.opacity === "0") return false;
return true;
}