Skip to content

Instantly share code, notes, and snippets.

@chrisinajar
Created April 13, 2013 20:01
Show Gist options
  • Save chrisinajar/5379848 to your computer and use it in GitHub Desktop.
Save chrisinajar/5379848 to your computer and use it in GitHub Desktop.
Plug.DJ chat logger and log reader
// Basid plug.dj logger. Sloppy, doens't unlisten to events
(function() {
var load = function(name) {
try {
return JSON.parse(localStorage["_log_" + name]);
} catch (e) {
return null;
}
}
var save = function(name, value) {
localStorage["_log_" + name] = JSON.stringify(value);
}
var state = load("state") || {
keys: []
};
API.addEventListener(API.CHAT, function(chat) {
var d = new Date();
var key = "chat_" + d.getFullYear() + "_" + d.getMonth() + "_" + d.getDay() + "_" + d.getHours() + "_" + ~~(d.getMinutes()/10);
var curChats = load(key)
if (!curChats) {
state.keys.push(key);
save("state", state);
curChats = [];
}
curChats.push(chat);
save(key, curChats);
});
})();
// Dead simple reader
(function() {
var load = function(name) {
try {
return JSON.parse(localStorage["_log_" + name]);
} catch (e) {
return null;
}
}
var state = load("state")
if (!state) return console.log("No logs");
$.each(state.keys, function(i, key) {
var chats = load(key);
if (!chats)
return;
$.each(chats, function(i, chat) {
console.log(chat.from + ":", chat.message)
})
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment