2015-01-20 20:25:26 -05:00
var FFZ = window . FrankerFaceZ ,
reg _escape = function ( str ) {
return str . replace ( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g , "\\$&" ) ;
} ;
2015-01-20 01:53:18 -05:00
// ---------------------
// Initialization
// ---------------------
FFZ . prototype . setup _line = function ( ) {
this . log ( "Hooking the Ember Line controller." ) ;
var Line = App . _ _container _ _ . resolve ( 'controller:line' ) ,
f = this ;
Line . reopen ( {
tokenizedMessage : function ( ) {
// Add our own step to the tokenization procedure.
2015-01-20 20:25:26 -05:00
var tokens = f . _emoticonize ( this , this . _super ( ) ) ,
user = f . get _user ( ) ;
if ( ! user || this . get ( "model.from" ) != user . login )
tokens = f . _mentionize ( this , tokens ) ;
return tokens ;
2015-01-20 01:53:18 -05:00
} . property ( "model.message" , "isModeratorOrHigher" , "controllers.emoticons.emoticons.[]" )
// TODO: Copy the new properties from the new Twitch!
} ) ;
this . log ( "Hooking the Ember Line view." ) ;
var Line = App . _ _container _ _ . resolve ( 'view:line' ) ;
Line . reopen ( {
didInsertElement : function ( ) {
this . _super ( ) ;
var el = this . get ( 'element' ) ,
user = this . get ( 'context.model.from' ) ;
el . setAttribute ( 'data-room' , this . get ( 'context.parentController.content.id' ) ) ;
el . setAttribute ( 'data-sender' , user ) ;
f . render _badge ( this ) ;
2015-01-20 20:25:26 -05:00
if ( localStorage . ffzCapitalize != 'false' )
2015-01-20 01:53:18 -05:00
f . capitalize ( this , user ) ;
}
} ) ;
// Store the capitalization of our own name.
var user = this . get _user ( ) ;
if ( user && user . name )
FFZ . capitalization [ user . login ] = [ user . name , Date . now ( ) ] ;
2015-01-20 20:25:26 -05:00
// Load the mention words.
if ( localStorage . ffzMentionize )
this . mention _words = JSON . parse ( localStorage . ffzMentionize ) ;
else
this . mention _words = [ ] ;
2015-01-20 01:53:18 -05:00
}
// ---------------------
// Capitalization
// ---------------------
FFZ . capitalization = { } ;
FFZ . _cap _fetching = 0 ;
FFZ . get _capitalization = function ( name , callback ) {
name = name . toLowerCase ( ) ;
if ( name == "jtv" || name == "twitchnotify" )
return name ;
var old _data = FFZ . capitalization [ name ] ;
if ( old _data ) {
if ( Date . now ( ) - old _data [ 1 ] < 3600000 )
return old _data [ 0 ] ;
}
if ( FFZ . _cap _fetching < 5 ) {
FFZ . _cap _fetching ++ ;
Twitch . api . get ( "users/" + name )
. always ( function ( data ) {
var cap _name = data . display _name || name ;
FFZ . capitalization [ name ] = [ cap _name , Date . now ( ) ] ;
FFZ . _cap _fetching -- ;
2015-01-20 20:25:26 -05:00
typeof callback === "function" && callback ( cap _name ) ;
2015-01-20 01:53:18 -05:00
} ) ;
}
return old _data ? old _data [ 0 ] : name ;
}
FFZ . prototype . capitalize = function ( view , user ) {
var name = FFZ . get _capitalization ( user , this . capitalize . bind ( this , view ) ) ;
if ( name )
view . $ ( '.from' ) . text ( name ) ;
}
FFZ . chat _commands . capitalization = function ( room , args ) {
var enabled , args = args && args . length ? args [ 0 ] . toLowerCase ( ) : null ;
if ( args == "y" || args == "yes" || args == "true" || args == "on" )
enabled = true ;
else if ( args == "n" || args == "no" || args == "false" || args == "off" )
enabled = false ;
if ( enabled === undefined )
return "Chat Name Capitalization is currently " + ( localStorage . ffzCapitalize != "false" ? "enabled." : "disabled." ) ;
localStorage . ffzCapitalize = enabled ;
return "Chat Name Capitalization is now " + ( enabled ? "enabled." : "disabled." ) ;
}
FFZ . chat _commands . capitalization . help = "Usage: /ffz capitalization <on|off>\nEnable or disable Chat Name Capitalization. This setting does not work with BetterTTV." ;
2015-01-20 20:25:26 -05:00
// ---------------------
// Extra Mentions
// ---------------------
FFZ . _regex _cache = { } ;
FFZ . get _regex = function ( word ) {
return FFZ . _regex _cache [ word ] = FFZ . _regex _cache [ word ] || RegExp ( "\\b" + reg _escape ( word ) + "\\b" , "i" ) ;
}
FFZ . prototype . _mentionize = function ( controller , tokens ) {
if ( ! this . mention _words )
return tokens ;
if ( typeof tokens == "string" )
tokens = [ tokens ] ;
_ . each ( this . mention _words , function ( word ) {
var eo = { mentionedUser : word , own : false } ;
tokens = _ . compact ( _ . flatten ( _ . map ( tokens , function ( token ) {
if ( _ . isObject ( token ) )
return token ;
var tbits = token . split ( FFZ . get _regex ( word ) ) , bits = [ ] ;
tbits . forEach ( function ( val , ind ) {
bits . push ( val ) ;
if ( ind !== tbits . length - 1 )
bits . push ( eo ) ;
} ) ;
return bits ;
} ) ) ) ;
} ) ;
return tokens ;
}
FFZ . chat _commands . mentionize = function ( room , args ) {
if ( args && args . length ) {
this . mention _words = args . join ( " " ) . trim ( ) . split ( /\W*,\W*/ ) ;
if ( this . mention _words . length == 1 && this . mention _words [ 0 ] == "disable" )
this . mention _words = [ ] ;
localStorage . ffzMentionize = JSON . stringify ( this . mention _words ) ;
}
if ( this . mention _words . length )
return "The following words will be treated as mentions: " + this . mention _words . join ( ", " ) ;
else
return "There are no words set that will be treated as mentions." ;
}
FFZ . chat _commands . mentionize . help = "Usage: /ffz mentionize <comma, separated, word, list|disable>\nSet a list of words that will also be treated as mentions and be displayed specially in chat." ;
2015-01-20 01:53:18 -05:00
// ---------------------
// Emoticon Replacement
// ---------------------
FFZ . prototype . _emoticonize = function ( controller , tokens ) {
var room _id = controller . get ( "parentController.model.id" ) ,
user _id = controller . get ( "model.from" ) ,
f = this ;
// Get our sets.
var sets = this . getEmotes ( user _id , room _id ) ,
emotes = [ ] ;
// Build a list of emotes that match.
_ . each ( sets , function ( set _id ) {
var set = f . emote _sets [ set _id ] ;
if ( ! set )
return ;
_ . each ( set . emotes , function ( emote ) {
_ . any ( tokens , function ( token ) {
return _ . isString ( token ) && token . match ( emote . regex ) ;
} ) && emotes . push ( emote ) ;
} ) ;
} ) ;
// Don't bother proceeding if we have no emotes.
if ( ! emotes . length )
return tokens ;
// Now that we have all the matching tokens, do crazy stuff.
if ( typeof tokens == "string" )
tokens = [ tokens ] ;
// This is weird stuff I basically copied from the old Twitch code.
// Here, for each emote, we split apart every text token and we
// put it back together with the matching bits of text replaced
// with an object telling Twitch's line template how to render the
// emoticon.
_ . each ( emotes , function ( emote ) {
//var eo = {isEmoticon:true, cls: emote.klass};
var eo = { isEmoticon : true , cls : emote . klass , emoticonSrc : emote . url , altText : ( emote . hidden ? "???" : emote . name ) } ;
tokens = _ . compact ( _ . flatten ( _ . map ( tokens , function ( token ) {
if ( _ . isObject ( token ) )
return token ;
var tbits = token . split ( emote . regex ) , bits = [ ] ;
tbits . forEach ( function ( val , ind ) {
bits . push ( val ) ;
if ( ind !== tbits . length - 1 )
bits . push ( eo ) ;
} ) ;
return bits ;
} ) ) ) ;
} ) ;
return tokens ;
2015-01-12 17:58:07 -05:00
}