How/where write a javascript hook/plugin?

I’m trying to customize my instance (or maybe publish a plugin)
about support of magnet:? uri scheme and webtorrent p2p sharing.

For example, if anyone want to try, in my instance this test-toots:

Open Inspector -> JS Console -> paste

document.body.innerHTML = document.body.innerHTML.replace(/magnet:\?([^<\s\b]+)/g, "<iframe allowfullscreen allowtransparency='true' style='width:100%;height:20em;' src='https://search-id.org/?magnet:?$1'></iframe>");

It match magnet:? urls and replace with an iframe.

How i can automate this JS injection in my instance? It’s even possible to write a plugin?

Thx for any feedback.

mastodon does not currently have any sort of infrastructure for plugins. the code you’ve pasted here only works if the magnet links exist on the page when you’re running the code—it won’t work if you run it before any statuses are loaded and it won’t work if you scroll down or click into a thread or otherwise load statuses after the code is run.