|
// ==UserScript== |
|
// @name BTTVChatHistory4FFZ |
|
// @namespace http://nicholastay.github.io/ |
|
// @homepage https://gist.github.com/nicholastay/fad3889c30b963aa2f2a4a8529792212 |
|
// @version 0.1.1 |
|
// @description Chat history for FFZ, BTTV-style. |
|
// @author Nicholas Tay (Nexerq / n2468txd) <[email protected]> |
|
// @license Zlib/libpng |
|
// @match *://twitch.tv/* |
|
// @match *://www.twitch.tv/* |
|
// @grant none |
|
// @updateURL https://gist.github.com/nicholastay/fad3889c30b963aa2f2a4a8529792212/raw/BTTVChatHistory4FFZ.user.js |
|
// ==/UserScript== |
|
|
|
;(function() { |
|
function waitForFFZ() { |
|
var attempts = 0; |
|
var interv = setInterval(function() { |
|
if (window.FrankerFaceZ) { |
|
clearInterval(interv); |
|
console.log("[BTTVChatHistory4FFZ] Found FFZ, injecting."); |
|
inject(); |
|
return; |
|
} |
|
if (attempts > 10) { |
|
clearInterval(interv); |
|
console.log("[BTTVChatHistory4FFZ] Could not find FFZ."); |
|
return; |
|
} |
|
++attempts; |
|
}, 750); |
|
} |
|
|
|
var FFZ, api, inUse = false; |
|
function inject() { |
|
FFZ = window.FrankerFaceZ.get(); |
|
api = FFZ.api("BTTVChatHistory4FFZ"); |
|
api.log("Able to use FFZ api."); |
|
|
|
setupSettings(); |
|
attachCallbacks(); |
|
} |
|
|
|
function setupSettings() { |
|
window.FrankerFaceZ.settings_info.bttv_chat_history = { |
|
type: "boolean", |
|
value: true, |
|
category: "[Deprecated] BTTVChatHistory4FFZ", |
|
name: "Enabled", |
|
help: "Enable chat history to be pulled from BTTV servers on room join.", |
|
no_bttv: true, |
|
on_update: function(enabled) { |
|
inUse = !!enabled; |
|
} |
|
}; |
|
inUse = ffz.settings.get("bttv_chat_history"); |
|
} |
|
|
|
function attachCallbacks() { |
|
api.register_on_room_callback(onRoomJoin); |
|
} |
|
|
|
function onRoomJoin(roomId) { |
|
if (!inUse || FFZ.has_bttv || roomId[0] === "_") // _ seems to be metadata channel |
|
return; |
|
|
|
api.log("Grabbing past message history for channel: " + roomId); |
|
$.getJSON("https://api.betterttv.net/2/channels/" + roomId + "/history") |
|
.done(function(data) { |
|
if (!data.messages.length) |
|
return api.log("There was no past message history to add for channel '" + roomId + "'."); // no data |
|
api.log("Got " + data.messages.length + " past messages for channel '" + roomId + "', adding."); |
|
data.messages.forEach(function(message) { |
|
// ffz-ify the bttv data |
|
FFZ.rooms[roomId].room.addMessage({ |
|
color: message.user.color, |
|
date: new Date(jsifyDate(message.date)), |
|
from: message.user.name, |
|
message: message.message, |
|
style: undefined, |
|
tags: { |
|
"display-name": message.user.displayName, |
|
emotes: message.parsedEmotes |
|
} |
|
}); |
|
}); |
|
FFZ.room_message(FFZ.rooms[roomId], "BTTVChatHistory4FFZ: End of chat history from BTTV."); |
|
}) |
|
.fail(function() { |
|
api.log("Failed to get BTTV chat history for channel '" + roomId + "'."); |
|
}); |
|
} |
|
|
|
function jsifyDate(str) { |
|
// stupid js. |
|
return str.replace("T", " ").substring(0, str.indexOf(".")) + " UTC"; |
|
} |
|
|
|
waitForFFZ(); |
|
})(); |