mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-08-20 21:10:54 +00:00
Bunch o changes. I'm a bad person.
This commit is contained in:
parent
576c9569b2
commit
6264da62fc
25 changed files with 4610 additions and 1860 deletions
283
src/emoticons.js
283
src/emoticons.js
|
@ -5,15 +5,6 @@ var FFZ = window.FrankerFaceZ,
|
|||
utils = require('./utils'),
|
||||
|
||||
|
||||
loaded_global = function(set_id, success, data) {
|
||||
if ( ! success )
|
||||
return;
|
||||
|
||||
data.global = true;
|
||||
this.global_sets.push(set_id);
|
||||
},
|
||||
|
||||
|
||||
check_margins = function(margins, height) {
|
||||
var mlist = margins.split(/ +/);
|
||||
if ( mlist.length != 2 )
|
||||
|
@ -30,18 +21,29 @@ var FFZ = window.FrankerFaceZ,
|
|||
|
||||
|
||||
build_legacy_css = function(emote) {
|
||||
var margin = emote.margins;
|
||||
var margin = emote.margins, srcset = "";
|
||||
if ( ! margin )
|
||||
margin = ((emote.height - 18) / -2) + "px 0";
|
||||
return ".ffz-emote-" + emote.id + ' { background-image: url("' + emote.url + '"); height: ' + emote.height + "px; width: " + emote.width + "px; margin: " + margin + (emote.extra_css ? "; " + emote.extra_css : "") + "}\n";
|
||||
|
||||
if ( emote.urls[2] || emote.urls[4] ) {
|
||||
srcset = 'url("' + emote.urls[1] + '") 1x';
|
||||
if ( emote.urls[2] )
|
||||
srcset += ', url("' + emote.urls[2] + '") 2x';
|
||||
if ( emote.urls[4] )
|
||||
srcset += ', url("' + emote.urls[4] + '") 4x';
|
||||
|
||||
srcset = '-webkit-image-set(' + srcset + '); image-set(' + srcset + ');';
|
||||
}
|
||||
|
||||
return ".ffz-emote-" + emote.id + ' { background-image: url("' + emote.urls[1] + '"); height: ' + emote.height + "px; width: " + emote.width + "px; margin: " + margin + (srcset ? '; ' + srcset : '') + (emote.css ? "; " + emote.css : "") + "}\n";
|
||||
},
|
||||
|
||||
|
||||
build_new_css = function(emote) {
|
||||
if ( ! emote.margins && ! emote.extra_css )
|
||||
if ( ! emote.margins && ! emote.css )
|
||||
return build_legacy_css(emote);
|
||||
|
||||
return build_legacy_css(emote) + 'img[src="' + emote.url + '"] { ' + (emote.margins ? "margin: " + emote.margins + ";" : "") + (emote.extra_css || "") + " }\n";
|
||||
return build_legacy_css(emote) + 'img[src="' + emote.urls[1] + '"] { ' + (emote.margins ? "margin: " + emote.margins + ";" : "") + (emote.css || "") + " }\n";
|
||||
},
|
||||
|
||||
|
||||
|
@ -57,27 +59,103 @@ FFZ.prototype.setup_emoticons = function() {
|
|||
|
||||
this.emote_sets = {};
|
||||
this.global_sets = [];
|
||||
this.default_sets = [];
|
||||
this._last_emote_id = 0;
|
||||
|
||||
// Usage Data
|
||||
this.emote_usage = {};
|
||||
|
||||
|
||||
this.log("Creating emoticon style element.");
|
||||
var s = this._emote_style = document.createElement('style');
|
||||
s.id = "ffz-emoticon-css";
|
||||
document.head.appendChild(s);
|
||||
|
||||
this.log("Loading global emote set.");
|
||||
this.load_set("global", loaded_global.bind(this, "global"));
|
||||
this.log("Loading global emote sets.");
|
||||
this.load_global_sets();
|
||||
|
||||
this.log("Watching Twitch emoticon parser to ensure it loads.");
|
||||
this._twitch_emote_check = setTimeout(this.check_twitch_emotes.bind(this), 10000);
|
||||
}
|
||||
|
||||
|
||||
// ------------------------
|
||||
// Emote Usage
|
||||
// ------------------------
|
||||
|
||||
FFZ.prototype.add_usage = function(room_id, emote_id, count) {
|
||||
var rooms = this.emote_usage[emote_id] = this.emote_usage[emote_id] || {};
|
||||
rooms[room_id] = (rooms[room_id] || 0) + (count || 1);
|
||||
|
||||
if ( this._emote_report_scheduled )
|
||||
return;
|
||||
|
||||
this._emote_report_scheduled = setTimeout(this._report_emotes.bind(this), 30000);
|
||||
}
|
||||
|
||||
|
||||
FFZ.prototype._report_emotes = function() {
|
||||
if ( this._emote_report_scheduled )
|
||||
delete this._emote_report_scheduled;
|
||||
|
||||
var usage = this.emote_usage;
|
||||
this.emote_usage = {};
|
||||
this.ws_send("emoticon_uses", [usage], function(){}, true);
|
||||
}
|
||||
|
||||
|
||||
// ------------------------
|
||||
// Twitch Emoticon Checker
|
||||
// ------------------------
|
||||
|
||||
FFZ.prototype.check_twitch_emotes = function() {
|
||||
if ( this._twitch_emote_check ) {
|
||||
clearTimeout(this._twitch_emote_check);
|
||||
delete this._twitch_emote_check;
|
||||
}
|
||||
|
||||
var room;
|
||||
if ( this.rooms ) {
|
||||
for(var key in this.rooms) {
|
||||
if ( this.rooms.hasOwnProperty(key) ) {
|
||||
room = this.rooms[key];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! room || ! room.room || ! room.room.tmiSession ) {
|
||||
this._twitch_emote_check = setTimeout(this.check_twitch_emotes.bind(this), 10000);
|
||||
return;
|
||||
}
|
||||
|
||||
var parser = room.room.tmiSession._emotesParser,
|
||||
emotes = Object.keys(parser.emoticonRegexToIds).length;
|
||||
|
||||
// If we have emotes, we're done!
|
||||
if ( emotes > 0 )
|
||||
return;
|
||||
|
||||
// No emotes. Try loading them.
|
||||
var sets = parser.emoticonSetIds;
|
||||
parser.emoticonSetIds = "";
|
||||
parser.updateEmoticons(sets);
|
||||
|
||||
// Check again in a bit to see if we've got them.
|
||||
this._twitch_emote_check = setTimeout(this.check_twitch_emotes.bind(this), 10000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ---------------------
|
||||
// Set Management
|
||||
// ---------------------
|
||||
|
||||
FFZ.prototype.getEmotes = function(user_id, room_id) {
|
||||
var user = this.users[user_id],
|
||||
room = this.rooms[room_id];
|
||||
var user = this.users && this.users[user_id],
|
||||
room = this.rooms && this.rooms[room_id];
|
||||
|
||||
return _.union(user && user.sets || [], room && room.sets || [], this.global_sets);
|
||||
return _.union(user && user.sets || [], room && room.set && [room.set] || [], this.default_sets);
|
||||
}
|
||||
|
||||
|
||||
|
@ -86,16 +164,87 @@ FFZ.prototype.getEmotes = function(user_id, room_id) {
|
|||
// ---------------------
|
||||
|
||||
FFZ.ws_commands.reload_set = function(set_id) {
|
||||
if ( this.emote_sets.hasOwnProperty(set_id) )
|
||||
this.load_set(set_id);
|
||||
}
|
||||
|
||||
|
||||
FFZ.ws_commands.load_set = function(set_id) {
|
||||
this.load_set(set_id);
|
||||
}
|
||||
|
||||
|
||||
// ---------------------
|
||||
// Tooltip Powah!
|
||||
// ---------------------
|
||||
|
||||
FFZ.prototype._emote_tooltip = function(emote) {
|
||||
if ( ! emote )
|
||||
return null;
|
||||
|
||||
if ( emote._tooltip )
|
||||
return emote._tooltip;
|
||||
|
||||
var set = this.emote_sets[emote.set_id],
|
||||
owner = emote.owner,
|
||||
title = set && set.title || "Global";
|
||||
|
||||
emote._tooltip = "Emoticon: " + (emote.hidden ? "???" : emote.name) + "\nFFZ " + title + (owner ? "\nBy: " + owner.display_name : "");
|
||||
return emote._tooltip;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------
|
||||
// Set Loading
|
||||
// ---------------------
|
||||
|
||||
FFZ.prototype.load_set = function(set_id, callback) {
|
||||
return this._legacy_load_set(set_id, callback);
|
||||
FFZ.prototype.load_global_sets = function(callback, tries) {
|
||||
var f = this;
|
||||
jQuery.getJSON(constants.API_SERVER + "v1/set/global")
|
||||
.done(function(data) {
|
||||
f.default_sets = data.default_sets;
|
||||
var gs = f.global_sets = [],
|
||||
sets = data.sets || {};
|
||||
|
||||
for(var key in sets) {
|
||||
if ( ! sets.hasOwnProperty(key) )
|
||||
continue;
|
||||
|
||||
var set = sets[key];
|
||||
gs.push(key);
|
||||
f._load_set_json(key, undefined, set);
|
||||
}
|
||||
}).fail(function(data) {
|
||||
if ( data.status == 404 )
|
||||
return typeof callback == "function" && callback(false);
|
||||
|
||||
tries = tries || 0;
|
||||
tries++;
|
||||
if ( tries < 50 )
|
||||
return f.load_global_sets(callback, tries);
|
||||
|
||||
return typeof callback == "function" && callback(false);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
FFZ.prototype.load_set = function(set_id, callback, tries) {
|
||||
var f = this;
|
||||
jQuery.getJSON(constants.API_SERVER + "v1/set/" + set_id)
|
||||
.done(function(data) {
|
||||
f._load_set_json(set_id, callback, data && data.set);
|
||||
|
||||
}).fail(function(data) {
|
||||
if ( data.status == 404 )
|
||||
return typeof callback == "function" && callback(false);
|
||||
|
||||
tries = tries || 0;
|
||||
tries++;
|
||||
if ( tries < 10 )
|
||||
return f.load_set(set_id, callback, tries);
|
||||
|
||||
return typeof callback == "function" && callback(false);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
@ -108,97 +257,55 @@ FFZ.prototype.unload_set = function(set_id) {
|
|||
|
||||
utils.update_css(this._emote_style, set_id, null);
|
||||
delete this.emote_sets[set_id];
|
||||
|
||||
for(var i=0; i < set.users.length; i++) {
|
||||
var room = this.rooms[set.users[i]];
|
||||
if ( room )
|
||||
room.sets.removeObject(set_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
FFZ.prototype._load_set_json = function(set_id, callback, data) {
|
||||
if ( ! data )
|
||||
return typeof callback == "function" && callback(false);
|
||||
|
||||
// Store our set.
|
||||
this.emote_sets[set_id] = data;
|
||||
data.users = [];
|
||||
data.global = false;
|
||||
data.count = 0;
|
||||
|
||||
|
||||
// Iterate through all the emoticons, building CSS and regex objects as appropriate.
|
||||
var output_css = "";
|
||||
var output_css = "",
|
||||
ems = data.emoticons;
|
||||
|
||||
for(var key in data.emotes) {
|
||||
if ( ! data.emotes.hasOwnProperty(key) )
|
||||
continue;
|
||||
data.emoticons = {};
|
||||
|
||||
for(var i=0; i < ems.length; i++) {
|
||||
var emote = ems[i];
|
||||
|
||||
var emote = data.emotes[key];
|
||||
emote.klass = "ffz-emote-" + emote.id;
|
||||
emote.set_id = set_id;
|
||||
|
||||
emote.srcSet = emote.urls[1] + " 1x";
|
||||
if ( emote.urls[2] )
|
||||
emote.srcSet += ", " + emote.urls[2] + " 2x";
|
||||
if ( emote.urls[4] )
|
||||
emote.srcSet += ", " + emote.urls[4] + " 4x";
|
||||
|
||||
if ( emote.name[emote.name.length-1] === "!" )
|
||||
emote.regex = new RegExp("\\b" + emote.name + "(?=\\W|$)", "g");
|
||||
emote.regex = new RegExp("(^|\\W|\\b)(" + emote.name + ")(?=\\W|$)", "g");
|
||||
else
|
||||
emote.regex = new RegExp("\\b" + emote.name + "\\b", "g");
|
||||
emote.regex = new RegExp("(^|\\W|\\b)(" + emote.name + ")\\b", "g");
|
||||
|
||||
output_css += build_css(emote);
|
||||
data.count++;
|
||||
data.emoticons[emote.id] = emote;
|
||||
}
|
||||
|
||||
utils.update_css(this._emote_style, set_id, output_css + (data.extra_css || ""));
|
||||
this.log("Updated emoticons for set: " + set_id, data);
|
||||
utils.update_css(this._emote_style, set_id, output_css + (data.css || ""));
|
||||
this.log("Updated emoticons for set #" + set_id + ": " + data.title, data);
|
||||
|
||||
if ( this._cindex )
|
||||
this._cindex.ffzFixTitle();
|
||||
|
||||
this.update_ui_link();
|
||||
|
||||
if ( callback )
|
||||
callback(true, data);
|
||||
}
|
||||
|
||||
|
||||
FFZ.prototype._legacy_load_set = function(set_id, callback, tries) {
|
||||
jQuery.ajax(constants.SERVER + "channel/" + set_id + ".css", {cache: false, context:this})
|
||||
.done(function(data) {
|
||||
this._legacy_load_css(set_id, callback, data);
|
||||
|
||||
}).fail(function(data) {
|
||||
if ( data.status == 404 )
|
||||
return typeof callback == "function" && callback(false);
|
||||
|
||||
tries = tries || 0;
|
||||
tries++;
|
||||
if ( tries < 10 )
|
||||
return this._legacy_load_set(set_id, callback, tries);
|
||||
|
||||
return typeof callback == "function" && callback(false);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
FFZ.prototype._legacy_load_css = function(set_id, callback, data) {
|
||||
var emotes = {}, output = {id: set_id, emotes: emotes, extra_css: null}, f = this;
|
||||
|
||||
if ( set_id == "global" )
|
||||
output.title = "Global";
|
||||
else if ( set_id == "globalevent" )
|
||||
output.title = "Global Event";
|
||||
else if ( set_id == ".donor" )
|
||||
output.title = "Donor";
|
||||
|
||||
data = data.replace(CSS, function(match, klass, name, path, height, width, margins, extra) {
|
||||
height = parseInt(height); width = parseInt(width);
|
||||
margins = check_margins(margins, height);
|
||||
var hidden = path.substr(path.lastIndexOf("/") + 1, 1) === ".",
|
||||
id = ++f._last_emote_id,
|
||||
emote = {id: id, set_id: set_id, hidden: hidden, name: name, height: height, width: width, url: path, margins: margins, extra_css: extra};
|
||||
|
||||
emotes[id] = emote;
|
||||
return "";
|
||||
}).trim();
|
||||
|
||||
if ( data )
|
||||
data.replace(MOD_CSS, function(match, url) {
|
||||
if ( output.icon || url.substr(-11) !== 'modicon.png' )
|
||||
return;
|
||||
|
||||
output.icon = url;
|
||||
});
|
||||
|
||||
this._load_set_json(set_id, callback, output);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue