Skip to content

Instantly share code, notes, and snippets.

@bersling
Last active April 21, 2023 07:57
Show Gist options
  • Select an option

  • Save bersling/71ec269ce126a00fbd544fb29d515f7d to your computer and use it in GitHub Desktop.

Select an option

Save bersling/71ec269ce126a00fbd544fb29d515f7d to your computer and use it in GitHub Desktop.
Mastodon Browser Search
// There were mentions that the default mastodon client does not let you search in your posts
// e.g. https://infosec.exchange/@codinghorror/110232149814997656
// This is a hacky function to fetch all posts and search through them. Use and adapt at your own risk.
// Usage:
// 1. Navigate to your mastodon server
// 2. Open browser console
// 3. Copy paste the function
// 4. Execute the function for example like this: searchPosts("techhub.social", "bersling", "something")
async function searchPosts(server, accountNameWithoutAt, searchTerm) {
const accountLookupUrl = `https://${server}/api/v1/accounts/lookup?acct=${accountNameWithoutAt}`;
console.log(`Fetching accoung info from ${accountLookupUrl}`);
const accountResponse = await fetch(accountLookupUrl);
const accountId = (await accountResponse.json()).id;
const maxLimit = "40"; // looks like you can't go higher. without limit it's 20.
const queryUrl = `https://${server}/api/v1/accounts/${accountId}/statuses?exclude_replies=true&limit=${maxLimit}`;
const maxRequests = 10; // limit * maxRequests ~ numPosts. Don't DDOS servers please :)
let allPosts = [];
async function queryPosts(counter = 1, maxId = "") {
const fullUrl = queryUrl + (maxId ? `&max_id=${maxId}` : "");
console.log(`Sending GET to ${fullUrl}`);
const response = await fetch(fullUrl);
const posts = await response.json();
allPosts = [...allPosts, ...posts];
if (posts.length > 0 && counter < maxRequests) {
await queryPosts(counter + 1, posts[posts.length - 1].id);
}
}
await queryPosts();
console.log(`We found ${allPosts.length} posts.`);
const matches = allPosts.filter((post) =>
post.content.toLowerCase().includes(searchTerm.toLowerCase())
);
console.log(`Your search term matched with the following posts:`);
console.log(matches.map((match) => match));
console.log(`Here are direct links to those posts:`);
matches.forEach((match) => {
console.log(`https://${server}/@${accountNameWithoutAt}/${match.id}`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment