Skip to content

Instantly share code, notes, and snippets.

@kris-luminar
Forked from esundahl/env.json
Created March 28, 2012 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kris-luminar/2228095 to your computer and use it in GitHub Desktop.
Save kris-luminar/2228095 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
[{
"environments": [
"site.dev",
"site.staging",
"site.com"
]
}]
[{
"environments": [
"site.dev",
"site.staging",
"site.com"
]
}]
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()
(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