Skip to content

Instantly share code, notes, and snippets.

@nicholastay
Last active September 29, 2017 08:11
Show Gist options
  • Save nicholastay/fad3889c30b963aa2f2a4a8529792212 to your computer and use it in GitHub Desktop.
Save nicholastay/fad3889c30b963aa2f2a4a8529792212 to your computer and use it in GitHub Desktop.
Chat history from BTTV's servers in FFZ.

BTTVChatHistory4FFZ

This userscript is now deprecated, as Twitch themselves have implemented recent chat history. BTTV also returns an empty array now. You should uninstall this userscript now.

As it may or may not still be in testing, if their native chat history does not work for you, try the following command in the dev tools console while on twitch, then refresh the page to test if it worked: window.Twitch.experiments.overrideExperimentValue('MESSAGE_HISTORY', 'on');

Does exactly what it sounds like.

Install

  • Any userscript manager
  • Click the raw button next to the script below.
  • ???
  • Profit!

Changelog

v0.1.0

  • Initial working.

v0.1.1

  • Fix multi-room weirdness.
  • Add checks for BTTV being installed.
// ==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();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment