Last active
April 21, 2023 07:57
-
-
Save bersling/71ec269ce126a00fbd544fb29d515f7d to your computer and use it in GitHub Desktop.
Mastodon Browser Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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