Created
March 23, 2012 20:52
-
-
Save esundahl/2174933 to your computer and use it in GitHub Desktop.
This is a quick and dirty little javascript bookmarklet that I wrote to toggle between different development environments
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
[{ | |
"environments": [ | |
"site.dev", | |
"site.staging", | |
"site.com" | |
] | |
}] |
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
class ToggleEnvironment | |
constructor: -> | |
@url = window.location | |
@hostname = window.location.hostname | |
@script = document.createElement "script" | |
@loadjQuery() | |
loadjQuery: -> | |
@script.type = "text/javascript"; | |
@script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"; | |
document.body.appendChild(@script); | |
@tryReady() | |
tryReady: => | |
if typeof jQuery == "undefined" | |
setTimeout(@tryReady, 200) | |
else | |
@getConfig() | |
console.log "Waiting for jQuery" | |
getConfig: -> | |
jQuery.getJSON '/switcher/env.json', (data) => | |
@toggle data | |
toggle: (data) -> | |
for i, environment of data[0].environments | |
if environment is @hostname | |
next = parseInt(i) + 1 | |
unless next < data[0].environments.length | |
next = 0 | |
window.location.hostname = data[0].environments[next] | |
toggle = new ToggleEnvironment() |
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
(function() { | |
var ToggleEnvironment, toggle, | |
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | |
ToggleEnvironment = (function() { | |
function ToggleEnvironment() { | |
this.tryReady = __bind(this.tryReady, this); this.url = window.location; | |
this.hostname = window.location.hostname; | |
this.script = document.createElement("script"); | |
this.loadjQuery(); | |
} | |
ToggleEnvironment.prototype.loadjQuery = function() { | |
this.script.type = "text/javascript"; | |
this.script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"; | |
document.body.appendChild(this.script); | |
return this.tryReady(); | |
}; | |
ToggleEnvironment.prototype.tryReady = function() { | |
if (typeof jQuery === "undefined") { | |
setTimeout(this.tryReady, 200); | |
} else { | |
this.getConfig(); | |
} | |
return console.log("Waiting for jQuery"); | |
}; | |
ToggleEnvironment.prototype.getConfig = function() { | |
var _this = this; | |
return jQuery.getJSON('/switcher/env.json', function(data) { | |
return _this.toggle(data); | |
}); | |
}; | |
ToggleEnvironment.prototype.toggle = function(data) { | |
var environment, i, next, _ref, _results; | |
_ref = data[0].environments; | |
_results = []; | |
for (i in _ref) { | |
environment = _ref[i]; | |
if (environment === this.hostname) { | |
next = parseInt(i) + 1; | |
if (!(next < data[0].environments.length)) next = 0; | |
_results.push(window.location.hostname = data[0].environments[next]); | |
} else { | |
_results.push(void 0); | |
} | |
} | |
return _results; | |
}; | |
return ToggleEnvironment; | |
})(); | |
toggle = new ToggleEnvironment(); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment