Skip to content

Instantly share code, notes, and snippets.

@mkody
Last active December 11, 2022 20:21
Show Gist options
  • Save mkody/993fb729c195caa90ff346cbba6153d7 to your computer and use it in GitHub Desktop.
Save mkody/993fb729c195caa90ff346cbba6153d7 to your computer and use it in GitHub Desktop.
F2 to go to my mastodon instance
// ==UserScript==
// @name F2 to go to my mastodon instance
// @include /https:\/\/(.*)\/@(\w+)(\/[0-9]+)?/
// @include /https:\/\/(.*)\/users\/(\w+)/
// @include /https:\/\/(.*)\/notice\/([a-zA-Z]+)/
// @include /https:\/\/(.*)\/notes\/([0-9a-z]+)/
// @grant GM_xmlhttpRequest
// ==/UserScript==
// Based on https://github.com/rugk/mastodon-simplified-federation/issues/77#issuecomment-1328265632
// and extended to support more platforms and load statuses by using the search API from your instance.
// Should work to redirect from Mastodon, Pleroma/Akkoma, Misskey/Foundkey.
// PUT YOUR INSTANCE BELOW
const INSTANCE = 'joinmastodon.org'
// ADD A USER TOKEN BELOW
// Create it on https://${INSTANCE}/settings/applications and only set the "read:search" permission
const USER_TOKEN = ''
if (INSTANCE && USER_TOKEN) {
addEventListener('keydown', (e) => {
if (e.key === 'F2' && (
document.body.firstElementChild.id === 'mastodon' || // Mastodon
document.querySelector('body > #misskey_app') !== null || // Misskey/Foundkey
location.href.includes('/notice/') || location.href.includes('/users/') // Pleroma/Akkoma
)) {
e.preventDefault()
// Search for the status or account and redirect if found
GM_xmlhttpRequest({
url: `https://${INSTANCE}/api/v2/search?resolve=true&q=${location.href}`,
method: 'get',
headers: { Authorization: 'Bearer ' + USER_TOKEN },
responseType: 'json',
onload: (r) => {
if (r.response.statuses.length > 0) {
const s = r.response.statuses[0]
location.href = `https://${INSTANCE}/@${s.account.acct}/${s.id}`
} else if (r.response.accounts.length > 0) {
const a = r.response.accounts[0]
location.href = `https://${INSTANCE}/@${a.acct}`
} else {
alert('Status or account not found')
}
}
})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment