1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-06-28 15:27:43 +00:00
FrankerFaceZ/src/ext/betterttv.js

312 lines
9.5 KiB
JavaScript
Raw Normal View History

2015-01-15 13:58:42 -05:00
var FFZ = window.FrankerFaceZ,
2015-07-04 17:06:36 -04:00
constants = require('../constants'),
utils = require('../utils'),
2015-01-15 13:58:42 -05:00
SENDER_REGEX = /(\sdata-sender="[^"]*"(?=>))/;
// --------------------
// Initialization
// --------------------
FFZ.prototype.find_bttv = function(increment, delay) {
this.has_bttv = false;
if ( window.BTTVLOADED )
return this.setup_bttv(delay||0);
2015-01-15 13:58:42 -05:00
if ( delay >= 60000 )
this.log("BetterTTV was not detected after 60 seconds.");
else
setTimeout(this.find_bttv.bind(this, increment, (delay||0) + increment),
increment);
}
FFZ.prototype.setup_bttv = function(delay) {
this.log("BetterTTV was detected after " + delay + "ms. Hooking.");
2015-01-15 13:58:42 -05:00
this.has_bttv = true;
// Disable Dark if it's enabled.
document.body.classList.remove("ffz-dark");
if ( this._dark_style ) {
this._dark_style.parentElement.removeChild(this._dark_style);
this._dark_style = undefined;
}
if ( this._layout_style ) {
this._layout_style.parentElement.removeChild(this._layout_style);
this._layout_style = undefined;
}
if ( this._chat_style ) {
utils.update_css(this._chat_style, 'chat_font_size', '');
utils.update_css(this._chat_style, 'chat_ts_font_size', '');
}
2015-06-05 03:59:28 -04:00
// Disable Chat Tabs
if ( this._chatv ) {
if ( this.settings.group_tabs )
this._chatv.ffzDisableTabs();
this._chatv.ffzTeardownMenu();
this._chatv.ffzUnloadHost();
2015-06-05 03:59:28 -04:00
}
this.disconnect_extra_chat();
2015-07-04 17:06:36 -04:00
if ( this._roomv ) {
// Disable Chat Pause
if ( this.settings.chat_hover_pause )
this._roomv.ffzDisableFreeze();
// And hide the status
if ( this.settings.room_status )
this._roomv.ffzUpdateStatus();
}
// Disable style blocks.
this.toggle_style('chat-setup');
this.toggle_style('chat-padding');
this.toggle_style('chat-background');
this.toggle_style('chat-separator');
this.toggle_style('chat-separator-3d');
this.toggle_style('chat-separator-3d-inset');
this.toggle_style('chat-separator-wide');
this.toggle_style('chat-hc-text');
this.toggle_style('chat-hc-bold');
this.toggle_style('chat-hc-background');
this.toggle_style('chat-colors-gray');
this.toggle_style('badges-transparent');
// Disable other features too.
document.body.classList.remove('ffz-transparent-badges');
document.body.classList.remove("ffz-sidebar-swap");
document.body.classList.remove("ffz-portrait");
document.body.classList.remove("ffz-flip-dashboard");
// Remove Following Count
if ( this.settings.following_count ) {
this._schedule_following_count();
this._draw_following_count();
this._draw_following_channels();
}
2015-06-05 03:59:28 -04:00
// Remove Sub Count
if ( this.is_dashboard )
this._update_subscribers();
2015-07-04 17:06:36 -04:00
document.body.classList.add('ffz-bttv');
2015-01-15 13:58:42 -05:00
// Send Message Behavior
var original_send = BetterTTV.chat.helpers.sendMessage, f = this;
BetterTTV.chat.helpers.sendMessage = function(message) {
var cmd = message.split(' ', 1)[0].toLowerCase();
if ( cmd === "/ffz" )
f.run_ffz_command(message.substr(5), BetterTTV.chat.store.currentRoom);
2015-01-15 13:58:42 -05:00
else
return original_send(message);
}
// Ugly Hack for Current Room, as this is stripped out before we get to
// the actual privmsg renderer.
var original_handler = BetterTTV.chat.handlers.onPrivmsg,
2015-01-15 13:58:42 -05:00
received_room;
BetterTTV.chat.handlers.onPrivmsg = function(room, data) {
2015-01-15 13:58:42 -05:00
received_room = room;
var output = original_handler(room, data);
received_room = null;
return output;
}
// Message Display Behavior
var original_privmsg = BetterTTV.chat.templates.privmsg;
BetterTTV.chat.templates.privmsg = function(highlight, action, server, isMod, data) {
try {
// Handle badges.
f.bttv_badges(data);
// Now, do everything else manually because things are hard-coded.
2015-06-05 03:59:28 -04:00
return '<div class="chat-line'+(highlight?' highlight':'')+(action?' action':'')+(server?' admin':'')+'" data-sender="'+(data.sender||"").toLowerCase()+'" data-room="'+received_room+'">'+
BetterTTV.chat.templates.timestamp(data.time)+' '+
(isMod?BetterTTV.chat.templates.modicons():'')+' '+
BetterTTV.chat.templates.badges(data.badges)+
BetterTTV.chat.templates.from(data.nickname, data.color)+
BetterTTV.chat.templates.message(data.sender, data.message, data.emotes, action?data.color:false)+
'</div>';
} catch(err) {
f.log("Error: ", err);
return original_privmsg(highlight, action, server, isMod, data);
}
2015-01-15 13:58:42 -05:00
}
2015-07-04 17:06:36 -04:00
// Whispers too!
var original_whisper = BetterTTV.chat.templates.whisper;
BetterTTV.chat.templates.whisper = function(data) {
try {
// Handle badges.
f.bttv_badges(data);
// Now, do everything else manually because things are hard-coded.
return '<div class="chat-line whisper" data-sender="' + data.sender + '">' +
BetterTTV.chat.templates.timestamp(data.time) + ' ' +
(data.badges && data.badges.length ? BetterTTV.chat.templates.badges(data.badges) : '') +
BetterTTV.chat.templates.whisperName(data.sender, data.receiver, data.from, data.to, data.fromColor, data.toColor) +
BetterTTV.chat.templates.message(data.sender, data.message, data.emotes, false) +
'</div>';
} catch(err) {
f.log("Error: ", err);
return original_whisper(data);
}
}
// Message Renderer. I had to completely rewrite this method to get it to
// use my replacement emoticonizer.
var original_message = BetterTTV.chat.templates.message,
2015-01-15 13:58:42 -05:00
received_sender;
BetterTTV.chat.templates.message = function(sender, message, emotes, colored) {
try {
colored = colored || false;
var rawMessage = encodeURIComponent(message);
if(sender !== 'jtv') {
2015-06-05 03:59:28 -04:00
// Hackilly send our state across.
received_sender = sender;
var tokenizedMessage = BetterTTV.chat.templates.emoticonize(message, emotes);
received_sender = null;
for(var i=0; i<tokenizedMessage.length; i++) {
if(typeof tokenizedMessage[i] === 'string') {
tokenizedMessage[i] = BetterTTV.chat.templates.bttvMessageTokenize(sender, tokenizedMessage[i]);
} else {
tokenizedMessage[i] = tokenizedMessage[i][0];
}
}
message = tokenizedMessage.join(' ');
}
2015-06-05 03:59:28 -04:00
return '<span class="message" '+(colored?'style="color: '+colored+'" ':'')+'data-raw="'+rawMessage+'" data-emotes="'+(emotes ? encodeURIComponent(JSON.stringify(emotes)) : 'false')+'">'+message+'</span>';
} catch(err) {
f.log("Error: ", err);
return original_message(sender, message, emotes, colored);
}
};
2015-01-15 13:58:42 -05:00
// Emoticonize
var original_emoticonize = BetterTTV.chat.templates.emoticonize;
BetterTTV.chat.templates.emoticonize = function(message, emotes) {
var tokens = original_emoticonize(message, emotes),
2015-06-05 03:59:28 -04:00
room = (received_room || BetterTTV.getChannel()),
l_room = room && room.toLowerCase(),
l_sender = received_sender && received_sender.toLowerCase(),
sets = f.getEmotes(l_sender, l_room),
emotes = [],
user = f.get_user(),
mine = user && user.login === l_sender;
2015-01-15 13:58:42 -05:00
// Build a list of emotes that match.
_.each(sets, function(set_id) {
var set = f.emote_sets[set_id];
if ( ! set )
return;
2015-06-05 03:59:28 -04:00
_.each(set.emoticons, function(emote) {
2015-01-15 13:58:42 -05:00
_.any(tokens, function(token) {
return _.isString(token) && token.match(emote.regex);
}) && emotes.push(emote);
});
});
// Don't bother proceeding if we have no emotes.
2015-07-04 17:06:36 -04:00
if ( emotes.length ) {
// Why is emote parsing so bad? ;_;
_.each(emotes, function(emote) {
var tooltip = f._emote_tooltip(emote),
eo = ['<img class="emoticon html-tooltip" data-ffz-emote="' + emote.id + '" srcset="' + utils.quote_attr(emote.srcSet || "") + '" src="' + utils.quote_attr(emote.urls[1]) + '" data-regex="' + utils.quote_attr(emote.name) + '" title="' + utils.quote_attr(tooltip) + '">'],
2015-07-04 17:06:36 -04:00
old_tokens = tokens;
tokens = [];
for(var i=0; i < old_tokens.length; i++) {
var token = old_tokens[i];
if ( typeof token !== "string" ) {
2015-07-04 17:06:36 -04:00
tokens.push(token);
continue;
}
2015-01-15 13:58:42 -05:00
2015-07-04 17:06:36 -04:00
var tbits = token.split(emote.regex);
while(tbits.length) {
var bit = tbits.shift();
if ( tbits.length ) {
bit += tbits.shift();
if ( bit )
tokens.push(bit);
2015-01-15 13:58:42 -05:00
2015-07-04 17:06:36 -04:00
tbits.shift();
tokens.push(eo);
2015-01-15 13:58:42 -05:00
2015-07-04 17:06:36 -04:00
if ( mine && l_room )
f.add_usage(l_room, emote);
2015-07-04 17:06:36 -04:00
} else
tokens.push(bit);
}
}
});
}
// Sneak in Emojicon Processing
if ( f.settings.parse_emoji && f.emoji_data ) {
var old_tokens = tokens,
setting = f.settings.parse_emoji;
2015-07-04 17:06:36 -04:00
tokens = [];
2015-01-15 13:58:42 -05:00
for(var i=0; i < old_tokens.length; i++) {
var token = old_tokens[i];
2015-07-04 17:06:36 -04:00
if ( typeof token !== "string" ) {
2015-01-15 13:58:42 -05:00
tokens.push(token);
continue;
}
2015-07-04 17:06:36 -04:00
var tbits = token.split(constants.EMOJI_REGEX);
2015-06-05 03:59:28 -04:00
while(tbits.length) {
var bit = tbits.shift();
2015-07-04 17:06:36 -04:00
bit && tokens.push(bit);
2015-06-05 03:59:28 -04:00
2015-07-04 17:06:36 -04:00
if ( tbits.length ) {
var match = tbits.shift(),
variant = tbits.shift();
if ( variant === '\uFE0E' )
tokens.push(match);
2015-07-04 17:06:36 -04:00
else {
var eid = utils.emoji_to_codepoint(match, variant),
data = f.emoji_data[eid],
src = data && (setting === 2 ? data.noto_src : data.tw_src);
2015-07-04 17:06:36 -04:00
if ( data && src ) {
var image = src && f.settings.emote_image_hover ? '<img class="emoticon ffz-image-hover" src="' + src + '">' : '',
tooltip = image + "Emoji: " + data.raw + "<br>Name: " + data.name + (data.short_name ? "<br>Short Name: :" + data.short_name + ":" : ""),
code = utils.quote_attr(data.raw);
tokens.push(['<img class="emoticon emoji html-tooltip" height="18px" data-ffz-emoji="' + eid + '" src="' + utils.quote_attr(src) + '" data-regex="' + code + '" alt="' + code + '" title="' + utils.quote_attr(tooltip) + '">']);
2015-07-04 17:06:36 -04:00
} else
tokens.push(match + (variant || ""));
}
}
2015-06-05 03:59:28 -04:00
}
2015-01-15 13:58:42 -05:00
}
}
2015-01-15 13:58:42 -05:00
return tokens;
}
this.update_ui_link();
}