Created
May 9, 2025 03:40
-
-
Save Tarrgon/c84f267a4f97314090b4870760b8fb8f to your computer and use it in GitHub Desktop.
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
| // ==UserScript== | |
| // @name Bsky Permalink | |
| // @namespace https://tampermonkey.net/ | |
| // @version 2025-05-09 | |
| // @description Permalinks bsky profiles | |
| // @author Tarrgon | |
| // @match https://bsky.app/profile/* | |
| // @icon https://www.google.com/s2/favicons?sz=64&domain=bsky.app | |
| // @grant GM_setValue | |
| // @grant GM_getValue | |
| // ==/UserScript== | |
| let cache | |
| function addToCache(handle, did) { | |
| let exists = cache.find(e => e.did == did) | |
| if (exists) return | |
| if (cache.length == 500) { | |
| cache.shift() | |
| } | |
| cache.push({ handle, did }) | |
| GM.setValue("cache", JSON.stringify(cache)) | |
| } | |
| function getFromCache(handle) { | |
| return cache.find(e => e.handle == handle)?.did | |
| } | |
| async function getDid(handle) { | |
| if (handle.startsWith("did:plc:")) return handle | |
| let cached = getFromCache(handle) | |
| if (cached) return cached | |
| let res = await fetch(`https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor=${handle}`) | |
| let data = await res.json() | |
| if (data) { | |
| addToCache(handle, data.did) | |
| return data.did | |
| } | |
| return null | |
| } | |
| async function updateUrl() { | |
| let prevUrl = window.location.href | |
| let handle = prevUrl.split("/")[4] | |
| let did = await getDid(handle) | |
| if (did) { | |
| let url = prevUrl.split("/") | |
| url[4] = did | |
| window.history.replaceState({}, "", url.join("/")) | |
| } | |
| } | |
| function observeUrlChange() { | |
| let oldHref = document.location.href | |
| const body = document.querySelector("body") | |
| const observer = new MutationObserver(async () => { | |
| if (oldHref !== document.location.href) { | |
| oldHref = document.location.href | |
| await updateUrl() | |
| } | |
| }) | |
| observer.observe(body, { childList: true, subtree: true }) | |
| } | |
| (async function () { | |
| 'use strict' | |
| cache = JSON.parse((await GM.getValue("cache")) || "[]") | |
| await updateUrl() | |
| observeUrlChange() | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment