Skip to content

Instantly share code, notes, and snippets.

@koistya
Created June 23, 2011 15:12
Show Gist options
  • Save koistya/1042718 to your computer and use it in GitHub Desktop.
Save koistya/1042718 to your computer and use it in GitHub Desktop.
var endpoints = null;
function addEndpoint(url) {
var i;
if (endpoints == null) {
endpoints = getEndpoints();
}
url = url.trim();
if (url.charAt(url.length - 1) != '/') {
url += "/";
}
for (i = 0; i < endpoints.length; i++) {
if (endpoints[i].url == url) {
return;
}
}
endpoints.push({ url: url });
localStorage.setItem("endpoints", JSON.stringify(endpoints));
}
function removeEndpoint(url) {
var i;
if (endpoints == null) {
endpoints = getEndpoints();
}
for (i = 0; i < endpoints.length; i++) {
if (endpoints[i].url == url) {
endpoints.slice(i);
localStorage.setItem("endpoints", endpoints);
break;
}
i++;
}
}
function getEndpoints() {
if (endpoints != null && endpoints instanceof Array) {
return endpoints;
}
endpointsJson = localStorage.getItem("endpoints");
if (endpointsJson == null) {
return [];
}
try {
endpoints = JSON.parse(endpointsJson);
return endpoints instanceof Array ? endpoints : [];
} catch (err) {
console.log("getEndpoints() failed to parse JSON: " + endpointsJson);
console.dir(err);
return [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment