diff --git a/scripts/bsky-nesting-hovers.user.js b/scripts/bsky-nesting-hovers.user.js new file mode 100644 index 0000000..a0028dd --- /dev/null +++ b/scripts/bsky-nesting-hovers.user.js @@ -0,0 +1,111 @@ +// ==UserScript== +// @name Bluesky: keep nested profile hover cards open +// @namespace https://mcint.io/ +// @version 0.1.0 +// @description Keeps a profile hover card open while the pointer is in a card opened from inside it (bluesky-social/social-app#8559). +// @match https://bsky.app/* +// @run-at document-start +// @grant none +// ==/UserScript== + +// Why this is needed: social-app's hover cards render through a context-based +// (src/components/Portal.tsx), so a card opened from a mention inside +// another card is a *sibling* of that card in both the DOM and the React tree. +// Moving onto the child therefore fires onPointerLeave on the parent, which +// hides after 150 ms + 200 ms fade and, on unmount, takes the child with it. +// +// What this does: tracks which card spawned which, and while the pointer is in +// a descendant card, replays pointerover on each ancestor card (React turns a +// pointerover with no relatedTarget into onPointerEnter, i.e. 'hovered-card'). +// When the pointer leaves the chain, it replays pointerout so they hide as usual. + +;(function install(win) { + 'use strict' + const doc = win.document + const parentOf = new Map() // card root element -> parent card root | null + let current = null // card root under the pointer, if any + let faking = false + + // Card root = floating-ui wrapper
whose + // only child is
+ // (ProfileHoverCard/index.web.tsx). Update here if the markup changes. + function isCardRoot(el) { + if (el.nodeType !== 1 || !el.style || el.childElementCount !== 1) return false + if (el.style.position !== 'absolute' && el.style.position !== 'fixed') return false + const inner = el.firstElementChild + if (!inner.style || inner.style.willChange !== 'transform') return false + return /\bfade(In|Out)\b/.test( + inner.style.animationName || inner.style.animation || inner.getAttribute('style') || '', + ) + } + + function cardOf(node) { + for (let n = node; n && n !== doc; n = n.parentNode) { + if (parentOf.has(n)) return n + } + return null + } + + function ancestors(card) { + const out = [] + for (let p = card ? parentOf.get(card) : null; p; p = parentOf.get(p)) out.push(p) + return out + } + + function fake(type, el) { + if (!el.isConnected) return + faking = true + try { + el.dispatchEvent( + new win.PointerEvent(type, { + bubbles: true, + composed: true, + pointerType: 'mouse', + relatedTarget: null, + }), + ) + } finally { + faking = false + } + } + + function setCurrent(next) { + if (next === current) return + const keep = new Set(ancestors(next)) + for (const a of ancestors(current)) { + if (a !== next && !keep.has(a)) fake('pointerout', a) // -> 'unhovered-card' + } + for (const a of keep) fake('pointerover', a) // -> 'hovered-card' + current = next + } + + win.addEventListener( + 'pointerover', + e => { + if (!faking) setCurrent(cardOf(e.target)) + }, + true, + ) + win.addEventListener( + 'pointerout', + e => { + if (!faking && e.relatedTarget == null) setCurrent(null) // left the window + }, + true, + ) + + new win.MutationObserver(records => { + for (const r of records) { + for (const n of r.addedNodes) { + // A card that appears while the pointer is inside another card was + // opened from a mention in that card. + if (!parentOf.has(n) && isCardRoot(n)) parentOf.set(n, current) + } + for (const n of r.removedNodes) { + if (!parentOf.has(n)) continue + if (current === n) setCurrent(null) + parentOf.delete(n) + } + } + }).observe(doc.documentElement, {childList: true, subtree: true}) +})(typeof unsafeWindow !== 'undefined' ? unsafeWindow : window) diff --git a/tampermonkey/registry.json b/tampermonkey/registry.json index e123b8b..a480db0 100644 --- a/tampermonkey/registry.json +++ b/tampermonkey/registry.json @@ -23,5 +23,19 @@ "grant": [], "integrity": null, "desc": "Navigation popups (maintained fork) — MediaWiki hover previews. Matches Wikimedia family by domain + any */wiki/* path (coarse; content-detection planned)." + }, + { + "id": "bsky-nesting-hovers", + "name": "Bluesky: keep nested profile hover cards open", + "repo": "mcint/userscripts", + "path": "scripts/bsky-nesting-hovers.user.js", + "ref": "main", + "match": [ + "https://bsky.app/*" + ], + "runAt": "document-start", + "grant": [], + "integrity": null, + "desc": "Keeps a profile hover card open while the pointer is in a card opened from inside it (bluesky-social/social-app#8559). Depends on social-app's hover-card markup; see isCardRoot in the script if it stops working." } ] diff --git a/tampermonkey/test/bsky-nesting-hovers-match.test.js b/tampermonkey/test/bsky-nesting-hovers-match.test.js new file mode 100644 index 0000000..7d21346 --- /dev/null +++ b/tampermonkey/test/bsky-nesting-hovers-match.test.js @@ -0,0 +1,26 @@ +const test = require('node:test'); +const assert = require('node:assert'); +const fs = require('node:fs'); +const path = require('node:path'); +const L = require('../loader.user.js'); + +const root = path.join(__dirname, '..', '..'); + +function entry() { + const text = fs.readFileSync(path.join(__dirname, '..', 'registry.json'), 'utf8'); + return L.parseRegistry(text).find((e) => e.id === 'bsky-nesting-hovers'); +} + +test('bsky-nesting-hovers is registered and its path exists in the repo', () => { + const e = entry(); + assert.ok(e, 'registry entry missing'); + assert.ok(fs.existsSync(path.join(root, e.path)), `no file at ${e.path}`); +}); + +test('bsky-nesting-hovers matches bsky.app only', () => { + const m = entry().match; + assert.ok(L.matchUrl(m, 'https://bsky.app/')); + assert.ok(L.matchUrl(m, 'https://bsky.app/profile/example.bsky.social')); + assert.strictEqual(L.matchUrl(m, 'https://example.com/'), false); + assert.strictEqual(L.matchUrl(m, 'https://notbsky.app/'), false); +});