Skip to content

Instantly share code, notes, and snippets.

@christophergregory
Created October 17, 2012 13:05
Show Gist options
  • Save christophergregory/3905403 to your computer and use it in GitHub Desktop.
Save christophergregory/3905403 to your computer and use it in GitHub Desktop.
JavaScript: OpenKeyVal Wrapper
/*
OpenKeyVal Wrapper
Author: Chris Gregory (chris@sinelabs.com)
Description: Obscures openkeyvalue variable names for added protection against variable name overwrites
*/
var OKV = (function($, sudoKey) {
var okv = {},
sudoAPIkey = sudoKey || "KqTZsiBoeZMnERP"; // Default sudoKey
// Obscures keys with a sudoKey to prevent overwrites
function prefix(key) {
return sudoAPIkey + "-" + key;
}
okv.setItem = function(key, value, callback) {
var args = arguments.length;
$.ajax({
url: "https://secure.openkeyval.org/store/",
data: prefix(key) + "=" + value,
dataType: "jsonp",
success: function(data){
if (args === 3) {
callback(key, value);
} else {
console.log( key + " - " + value );
}
}
});
}
okv.getItem = function(key, callback) {
var args = arguments.length;
$.ajax({
url: "https://secure.openkeyval.org/" + prefix(key),
dataType: "jsonp",
success: function(data){
if (args === 2) {
callback(data, key);
} else {
console.log( key + " - " + data );
}
}
});
}
return okv;
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment