Last active
August 29, 2015 14:09
-
-
Save kent013/d917d22929915debd949 to your computer and use it in GitHub Desktop.
chatworkLogger.js
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
//Open Chatwork and login | |
//Paste this script into JS Console. | |
var users = {}; | |
var data = {} | |
//Do not change this value to lower than 1sec... | |
var loadingInterval = 1000; | |
var lastMessageId = null; | |
var loadRoomTimerId = null; | |
var currentRoomIndex = 0; | |
function loadRooms(){ | |
jQuery("#_roomMore").trigger("click"); | |
setTimeout(function(){ | |
var rooms = jQuery("._room"); | |
jQuery.each(rooms, function(k, v){ | |
var r = jQuery(v); | |
var d = {} | |
d.id = r.data("rid"); | |
d.name = r.attr("aria-label"); | |
d.messages = {}; | |
data[d.id] = d; | |
}); | |
loadRoom(Object.keys(data)[currentRoomIndex]); | |
}, loadingInterval); | |
} | |
function loadRoom(roomId){ | |
jQuery("li._room[data-rid='" + roomId + "']").trigger("click"); | |
setTimeout(function(){ | |
loadMessages(roomId); | |
}, loadingInterval); | |
} | |
function loadMessages(roomId){ | |
saveMessages(roomId); | |
var lastMessageId = jQuery("._message").first().attr("id"); | |
jQuery("#_timeLine").scrollTo(0); | |
setTimeout(function(){ | |
if(lastMessageId != jQuery("._message").first().attr("id")){ | |
loadMessages(roomId); | |
}else{ | |
onLoadMessagesCompleted(roomId); | |
} | |
}, loadingInterval); | |
} | |
function saveMessages(roomId){ | |
var messages = jQuery("._message"); | |
var lastMessage = null; | |
jQuery.each(messages, function(k, v){ | |
var m = jQuery(v); | |
var d = {}; | |
d.id = m.data("mid"); | |
if(!data[roomId].messages[d.id]){ | |
d.roomid = m.data("rid"); | |
d.created_at = m.find("div._timeStamp").data("tm"); | |
if(m.find("div._timeStamp").data("utm")){ | |
d.updated_at = m.find("div._timeStamp").data("utm"); | |
} | |
d.body = m.find("div.chatTimeLineMessageArea pre").html(); | |
if(m.find(".chatTimeLineTo").length > 0){ | |
d.recipients = []; | |
jQuery.each(m.find(".chatTimeLineTo + img"), function(k, v){ | |
var recipientId = jQuery(v).data("aid"); | |
d.recipients.push(recipientId); | |
}); | |
} | |
if(m.find(".chatTimeLineReply").length > 0){ | |
d.inReplyTo = []; | |
jQuery.each(m.find(".chatTimeLineReply"), function(k, v){ | |
var u = jQuery(v.nextSibling); | |
v = jQuery(v); | |
var reply = {}; | |
reply.messageid = v.data("mid"); | |
reply.roomid = v.data("rid"); | |
reply.userid = u.data("aid"); | |
d.inReplyTo.push(reply); | |
}); | |
} | |
if(m.find(".chatQuote").length > 0){ | |
d.quotes = []; | |
jQuery.each(m.find(".chatQuote"), function(k, v){ | |
v = jQuery(v); | |
var quote = {}; | |
quote.userid = v.find("div.title > img").data("aid"); | |
quote.body = v.find("div.quoteText").html(); | |
d.quotes.push(quote); | |
}); | |
} | |
if(m.find("._messageLink").length > 0){ | |
d.links = []; | |
jQuery.each(m.find("._messageLink"), function(k, v){ | |
v = jQuery(v); | |
var link = {}; | |
link.roomid = v.data("rid"); | |
link.messageid = v.data("mid"); | |
d.links.push(link); | |
}); | |
} | |
d.isContainingTask = m.find("div.chatInfoTaskArea").length != 0; | |
if(m.find("p._speakerName span").length == 0){ | |
m = lastMessage; | |
}else{ | |
lastMessage = m; | |
} | |
d.userid = parseInt(m.find("p._speakerName span").attr("class").replace("_nameAid", "")); | |
d.username = m.find("p._speakerName span").text(); | |
d.companyname = m.find(".chatNameOrgname span").text(); | |
data[roomId].messages[d.id] = d; | |
} | |
}); | |
console.debug(Object.keys(data[roomId].messages).length + " messages are loaded"); | |
} | |
function loadUserData(){ | |
$("#_openContactWindow").trigger("click"); | |
setTimeout(function(){ | |
$("#_contactWindowTabMyContact").trigger("click"); | |
setTimeout(function(){ | |
var contacts = $("._contactList"); | |
jQuery.each(contacts, function(k, v){ | |
v = jQuery(v); | |
var u = {}; | |
u.id = v.data("aid"); | |
u.avatar = v.find("._avatar").attr("src"); | |
u.name = v.find("._profileTipCheckLabel > span").text(); | |
if(v.find("#_profileTipOrgName").length > 0){ | |
u.companyname = v.find("#_profileTipOrgName span").text(); | |
} | |
if(v.find(".profileTipChatWorkId").length > 0){ | |
u.chatworkid = v.find(".profileTipChatWorkId span a span").text(); | |
} | |
users[u.id] = u; | |
}); | |
onLoadUsersCompleted(); | |
}, loadingInterval); | |
}, loadingInterval); | |
} | |
function lookupQuoteMessages(roomId){ | |
jQuery.each(data[roomId].messages, function(k, v){ | |
if(v.quotes){ | |
var quotes = []; | |
jQuery.each(v.quotes, function(k, v){ | |
var q = lookupQuoteMessage(roomId, v); | |
quotes.push(q); | |
}); | |
v.quotes = quotes; | |
} | |
}); | |
} | |
function lookupQuoteMessage(roomId, quote){ | |
var retval = null; | |
jQuery.each(data[roomId].messages, function(k, v){ | |
if(v.body.match(quote.body) && | |
v.userid == quote.userid){ | |
retval = v; | |
v.body = quote.body; | |
return false; | |
} | |
}); | |
if(!retval){ | |
retval = quote; | |
} | |
return retval; | |
} | |
function onLoadMessagesCompleted(roomId){ | |
lookupQuoteMessages(roomId); | |
console.log(data[roomId]); | |
currentRoomIndex++; | |
var roomIds = Object.keys(data); | |
if(roomIds.length > currentRoomIndex){ | |
loadRoom(roomIds[currentRoomIndex]); | |
}else{ | |
onLoadRoomsCompleted(); | |
} | |
} | |
function onLoadUsersCompleted(){ | |
console.log(users); | |
loadRooms(); | |
} | |
function onLoadRoomsCompleted(){ | |
console.log(data); | |
} | |
loadUserData(); | |
/* | |
var roomId = "19969036"; | |
data[roomId] = {}; | |
data[roomId].messages = {}; | |
saveMessages(roomId); | |
lookupQuoteMessages(roomId); | |
console.log(data[roomId].messages["457778052"]);*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment