Created
June 23, 2011 15:12
-
-
Save koistya/1042718 to your computer and use it in GitHub Desktop.
This file contains 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
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