Skip to content

Instantly share code, notes, and snippets.

@fitnr
Last active December 17, 2018 09:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitnr/c19ca210206b2e220092 to your computer and use it in GitHub Desktop.
Save fitnr/c19ca210206b2e220092 to your computer and use it in GitHub Desktop.
Bookmarklet for switching between two versions of a site, say dev and production, or localhost and dev

How to

Change the left_domain_name and right_domain_name variables to reflect your project. If your sites have exactly matching URL schemas (where localhost/my/path is always equivalent to production.com/my/path), you can ignore that big comment block. If not, check it out and rewrite the two functions to fit your setup.

Compress the javascript, say with UglifyJS.

Urlencode the result, say with this tool.

Add javascript: to the front and save it as a bookmark. Depending on your browser, the easiest way to do this might be to make a new bookmark for a random page and then edit both the address and name.

(function() {
var uri,
location = document.location,
path = location.pathname,
// change these to the two domains you want to switch between
left_domain_name = "localhost",
right_domain_name = "www.production.com";
// This would be really easy if our two servers had exactly matching url schemas,
// but the world isn't always fair
// If you have diverging schemas, rewrite these functions to convert back and forth
// The comment text shows a simple example - the urls on production.com
// must be prefixed with /html
// These two functions should be inverses of each other, i.e.
// right2left(left2right(path)) == path
function left2right(path) {
return path;
// Add the /html
// return path + "/html"
}
function right2left(path) {
return path
// We would have to remove that "/html"
// return path.substring(5);
}
if (location.host === left_domain_name) {
uri = right_domain_name + left2right(path);
} else if (location.host === right_domain_name) {
uri = left_domain_name + right2left(path);
}
location.href = "http://" + uri + location.hash + location.search;
})();
@chriszarate
Copy link

Nice! Use my bookmarkleter for one-step bookmarkletification:
http://chriszarate.github.io/bookmarkleter/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment