@post-embed/exporter
v0.5.0
Published
Read X post data from the page's own GraphQL responses
Readme
@post-embed/exporter
Turns X post data into the XPost snapshot that @post-embed/elements renders: either from the GraphQL responses the x.com page already receives, or from a syndication API response.
@post-embed/exporter/x/syndication
Convert a response of X's syndication API (the data behind X's own embeds, also what react-tweet's fetchTweet returns) to XPost:
import { fromSyndication } from '@post-embed/exporter/x/syndication'
const response = await fetch(`https://react-tweet.vercel.app/api/tweet/${id}`)
const { data } = await response.json()
const result = fromSyndication(data)
if (result.issues) {
// Validation failed; each issue contains its message and optional path.
console.error(result.issues)
} else {
const post = result.value // XPost
}fromSyndication accepts any value and returns a Standard Schema result: { value: XPost } on success or { issues } on validation failure, including tombstones and empty objects. Handle HTTP not-found responses before conversion. This replaces the earlier XPost | undefined return type. fromSyndicationTweet still converts an already validated Tweet directly.
@post-embed/exporter/x
Run in the page's own JavaScript context (a MAIN world content script or a userscript) before the page's scripts start:
import { observeXTweets } from '@post-embed/exporter/x'
const observer = observeXTweets()
observer.subscribe((entry) => console.log(entry.post.id, entry.protected))
const entry = await observer.waitFor('20', { timeoutMs: 5000 })observeXTweets wraps fetch and XMLHttpRequest.prototype.open, copies responses of the known tweet operations, finds every tweet object in them, converts each one to XPost with toXPost, and keeps the newest 300 by id. entry.protected is true when the author limits who can see their posts. It only reads: no request is sent, no request or response is changed.
@post-embed/exporter/x/bridge
Passes posts from the MAIN world to an extension's isolated content script:
In the MAIN world:
import { exposeXTweets } from '@post-embed/exporter/x/bridge'
exposeXTweets(observer)In the ISOLATED world:
import { requestXTweet } from '@post-embed/exporter/x/bridge'
const entry = await requestXTweet('20')createXTweetClient keeps one connection open for repeated get calls and broadcast entries. The bridge is a birpc channel over window.postMessage, filtered to same-window, same-origin messages; answers are re-validated with XPostSchema on the isolated side.
