Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Fast environment switcher for web developers, via bookmarklet.
/**
* @file: Environment switcher bookmarklet (source)
*
* @usage: @TODO
* Notes:
* - Generate bookmarklet using: http://ted.mielczarek.org/code/mozilla/bookmarklet.html
*/
var curLoc = new String(window.location.href);
var newLoc = '';
// Prefixes for each environment
var prodPrefix = 'www.acme.com/subdir';
var qaPrefix = 'www.qa.acme.com/subdir';
var devPrefix = 'localhost:8000';
// env object relating each env key with its default config
var envs = {
'prod': {
'curPrefix': prodPrefix,
'destPrefix': 'dev' //devPrefix
},
'qa': {
'curPrefix': qaPrefix,
'destPrefix': 'dev' //devPrefix
},
'dev': {
'curPrefix': devPrefix,
'destPrefix': 'prod' //prodPrefix
}
};
// Shortcuts: key is shortcut, value is key on envs object.
var shortcuts = {
'p': 'prod',
'q': 'qa',
'd': 'dev'
};
// Determine current env and default destination for redirection
for (env in envs) {
if (curLoc.indexOf(envs[env].curPrefix) > -1) {
var curPrefix = envs[env].curPrefix;
var defaultDestPrefix = envs[env].destPrefix;
}
}
// Allow user to override the default
var destPrefix = prompt('Enter destination environment (prod, qa, dev)', defaultDestPrefix);
// Try to expand shortcut
if (shortcuts.hasOwnProperty(destPrefix)) {
destPrefix = shortcuts[destPrefix];
}
// Generate newLoc
if (envs.hasOwnProperty(destPrefix)) {
newLoc = curLoc.replace(curPrefix, envs[destPrefix].curPrefix);
}
// Browse to newLoc
if (newLoc !== curLoc && newLoc.length > 0) {
window.location = newLoc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment