I know there have been a few of these posted recently, but I modified a few of the ones I found to add one additional feature: If the page you are on is already the remote version, but for the wrong local instance, to still update that to your preferred local instance.

Much easier to explain what I mean with examples so here’s some:
https://lemmy.world/c/technology -> https://kbin.social/m/[email protected]
https://lemmy.world/c/[email protected] -> https://kbin.social/m/[email protected]

The first they all do, the second is the feature I added.

Here’s the code, just manually create a bookmark and enter this as the URL, with whatever you want as the name. Then go to a page for a community you want to see in your local instance and click the bookmark, and it should take you to that local version of that page.

It’s set up to take you to kbin.social versions of page, but you can change that by editing the home_instance_domain and the home_instance_char.

javascript:(function(){
var home_instance_domain = 'kbin.social';
var home_instance_char = '/m/';
var prefix = 'https://';

var url = new URL(document.URL);
var base_url = url.hostname;
var path_name = url.pathname;
path_name = path_name.replace(/\/\w\//, home_instance_char);
if (path_name.includes(home_instance_domain)) {
  path_name = path_name.replace(/@.+/, '');
  new_url = prefix + home_instance_domain + path_name;
} else if (path_name.includes('@')) {
  new_url = prefix + home_instance_domain + path_name;
} else {
  new_url = prefix + home_instance_domain + path_name + "@" + base_url;
}
console.log(new_url);
window.location.href=new_url;
})();

Edited to also handle being on the federated version of you home instance, like this:
https://lemmy.world/c/[email protected] -> https://kbin.social/m/privacy