2019-09-20 22:32:52 +02:00
//META{"name":"TimedLightDarkMode","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/TimedLightDarkMode","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/TimedLightDarkMode/TimedLightDarkMode.plugin.js"}*//
2019-03-07 23:15:46 +01:00
class TimedLightDarkMode {
getName ( ) { return "TimedLightDarkMode" ; }
2019-11-10 19:27:14 +01:00
getVersion ( ) { return "1.0.5" ; }
2019-03-07 23:15:46 +01:00
getAuthor ( ) { return "DevilBro" ; }
2019-04-17 10:56:54 +02:00
getDescription ( ) { return "Allows you to automatically change light/dark mode depending on the time of day. Slider is added to the 'Appearance' settings." ; }
2019-03-07 23:15:46 +01:00
2019-09-04 12:34:02 +02:00
constructor ( ) {
2019-04-17 10:56:54 +02:00
this . changelog = {
2019-11-10 19:27:14 +01:00
"improved" : [ [ "New Library Structure & React" , "Restructured my Library and switched to React rendering instead of DOM manipulation" ] ]
2019-04-17 10:56:54 +02:00
} ;
2019-09-04 12:34:02 +02:00
2019-11-14 17:56:26 +01:00
this . patchedModules = {
after : {
RadioGroup : "componentDidMount"
}
2019-03-07 23:15:46 +01:00
} ;
2019-09-04 12:34:02 +02:00
}
initConstructor ( ) {
2019-03-07 23:15:46 +01:00
this . defaults = {
2019-03-07 23:52:24 +01:00
settings : {
running : { value : true }
} ,
2019-03-07 23:15:46 +01:00
values : {
timer1 : { value : 25 } ,
timer2 : { value : 75 }
}
} ;
}
//legacy
load ( ) { }
start ( ) {
if ( ! global . BDFDB ) global . BDFDB = { myPlugins : { } } ;
if ( global . BDFDB && global . BDFDB . myPlugins && typeof global . BDFDB . myPlugins == "object" ) global . BDFDB . myPlugins [ this . getName ( ) ] = this ;
2019-11-10 19:27:14 +01:00
let libraryScript = document . querySelector ( 'head script#BDFDBLibraryScript' ) ;
2019-05-26 13:55:26 +02:00
if ( ! libraryScript || ( performance . now ( ) - libraryScript . getAttribute ( "date" ) ) > 600000 ) {
2019-03-07 23:15:46 +01:00
if ( libraryScript ) libraryScript . remove ( ) ;
libraryScript = document . createElement ( "script" ) ;
2019-05-26 13:55:26 +02:00
libraryScript . setAttribute ( "id" , "BDFDBLibraryScript" ) ;
2019-03-07 23:15:46 +01:00
libraryScript . setAttribute ( "type" , "text/javascript" ) ;
2019-10-18 10:56:41 +02:00
libraryScript . setAttribute ( "src" , "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js" ) ;
2019-03-07 23:15:46 +01:00
libraryScript . setAttribute ( "date" , performance . now ( ) ) ;
2019-05-26 13:55:26 +02:00
libraryScript . addEventListener ( "load" , ( ) => { this . initialize ( ) ; } ) ;
2019-03-07 23:15:46 +01:00
document . head . appendChild ( libraryScript ) ;
2019-05-26 13:55:26 +02:00
}
else if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) this . initialize ( ) ;
2019-11-01 10:27:07 +01:00
this . startTimeout = setTimeout ( ( ) => {
try { return this . initialize ( ) ; }
catch ( err ) { console . error ( ` %c[ ${ this . getName ( ) } ]%c ` , "color: #3a71c1; font-weight: 700;" , "" , "Fatal Error: Could not initiate plugin! " + err ) ; }
} , 30000 ) ;
2019-03-07 23:15:46 +01:00
}
initialize ( ) {
if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
if ( this . started ) return ;
2019-10-22 18:55:25 +02:00
BDFDB . PluginUtils . init ( this ) ;
2019-09-04 12:34:02 +02:00
2019-10-22 18:55:25 +02:00
BDFDB . ModuleUtils . forceAllUpdates ( this ) ;
2019-09-04 12:34:02 +02:00
2019-03-07 23:15:46 +01:00
this . startInterval ( ) ;
}
2019-11-01 10:14:50 +01:00
else console . error ( ` %c[ ${ this . getName ( ) } ]%c ` , "color: #3a71c1; font-weight: 700;" , "" , "Fatal Error: Could not load BD functions!" ) ;
2019-03-07 23:15:46 +01:00
}
stop ( ) {
if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
2019-10-22 11:37:23 +02:00
this . stopping = true ;
2019-11-01 11:09:32 +01:00
BDFDB . TimeUtils . clear ( this . checkInterval ) ;
2019-09-04 12:34:02 +02:00
2019-10-23 11:10:01 +02:00
BDFDB . DOMUtils . remove ( ".TLDM-settingsbox" ) ;
2019-09-04 12:34:02 +02:00
2019-10-22 18:55:25 +02:00
BDFDB . PluginUtils . clear ( this ) ;
2019-03-07 23:15:46 +01:00
}
}
2019-09-04 12:34:02 +02:00
2019-03-07 23:15:46 +01:00
// begin of own functions
2019-09-04 12:34:02 +02:00
2019-11-10 19:27:14 +01:00
processRadioGroup ( e ) {
if ( e . instance . props && Array . isArray ( e . instance . props . options ) && e . instance . props . options [ 0 ] && ( e . instance . props . options [ 0 ] . value == "light" || e . instance . props . options [ 0 ] . value == "dark" ) && e . instance . props . options [ 1 ] && ( e . instance . props . options [ 1 ] . value == "light" || e . instance . props . options [ 1 ] . value == "dark" ) && e . node . parentElement . firstElementChild . innerText && e . node . parentElement . firstElementChild . innerText . toUpperCase ( ) == BDFDB . LanguageUtils . LanguageStrings . THEME . toUpperCase ( ) ) {
2019-10-22 23:04:35 +02:00
let settings = BDFDB . DataUtils . get ( this , "settings" ) ;
2019-11-10 19:27:14 +01:00
let values = BDFDB . DataUtils . get ( this , "values" ) ;
let settingsbox = BDFDB . DOMUtils . create ( ` <div class=" ${ BDFDB . disCN . margintop8 } TLDM-settingsbox"><div class=" ${ BDFDB . disCNS . flex + BDFDB . disCNS . horizontal + BDFDB . disCNS . justifystart + BDFDB . disCNS . aligncenter + BDFDB . disCN . nowrap } " style="flex: 1 1 auto;"><h5 class=" ${ BDFDB . disCN . h5 } "> ${ BDFDB . LanguageUtils . LanguageStrings . THEME } Timer</h5><div class=" ${ BDFDB . disCNS . flexchild + BDFDB . disCNS . switchenabled + BDFDB . disCNS . switch + BDFDB . disCNS . switchvalue + BDFDB . disCNS . switchsizedefault + BDFDB . disCNS . switchsize + BDFDB . disCN . switchthemedefault } " style="flex: 0 0 auto;"><input type="checkbox" value="settings running" class=" ${ BDFDB . disCNS . switchinnerenabled + BDFDB . disCN . switchinner } settings-switch" ${ settings . running ? " checked" : "" } ></div></div><div class=" ${ BDFDB . disCNS . slider + BDFDB . disCN . margintop20 } ${ ! settings . running ? ( " " + BDFDB . disCN . sliderdisabled ) : "" } "><input type="number" key="timer1" class=" ${ BDFDB . disCN . sliderinput } " value=" ${ values . timer1 } " readonly=""><input type="number" key="timer2" class=" ${ BDFDB . disCN . sliderinput } " value=" ${ values . timer2 } " readonly=""><div class=" ${ BDFDB . disCN . slidertrack } "><div class=" ${ BDFDB . disCN . slidermark } " style="left: 0%;"><div class=" ${ BDFDB . disCN . slidermarkvalue } ">00:00</div><div class=" ${ BDFDB . disCN . slidermarkdash } "></div></div><div class=" ${ BDFDB . disCN . slidermark } " style="left: 12.5%;"><div class=" ${ BDFDB . disCN . slidermarkvalue } ">03:00</div><div class=" ${ BDFDB . disCN . slidermarkdash } "></div></div><div class=" ${ BDFDB . disCN . slidermark } " style="left: 25%;"><div class=" ${ BDFDB . disCN . slidermarkvalue } ">06:00</div><div class=" ${ BDFDB . disCN . slidermarkdash } "></div></div><div class=" ${ BDFDB . disCN . slidermark } " style="left: 37.5%;"><div class=" ${ BDFDB . disCN . slidermarkvalue } ">09:00</div><div class=" ${ BDFDB . disCN . slidermarkdash } "></div></div><div class=" ${ BDFDB . disCN . slidermark } " style="left: 50%;"><div class=" ${ BDFDB . disCN . slidermarkvalue } ">12:00</div><div class=" ${ BDFDB . disCN . slidermarkdash } "></div></div><div class=" ${ BDFDB . disCN . slidermark } " style="left: 62.5%;"><div class=" ${ BDFDB . disCN . slidermarkvalue } ">15:00</div><div class=" ${ BDFDB . disCN . slidermarkdash } "></div></div><div class=" ${ BDFDB . disCN . slidermark } " style="left: 75%;"><div class=" ${ BDFDB . disCN . slidermarkvalue } ">18:00</div><div class=" ${ BDFDB . disCN . slidermarkdash } "></div></div><div class=" ${ BDFDB . disCN . slidermark } " style="left: 87.5%;"><div class=" ${ BDFDB . disCN . slidermarkvalue } ">21:00</div><div class=" ${ BDFDB . disCN . slidermarkdash } "></div></div><div class=" ${ BDFDB . disCN . slidermark } " style="left: 100%;"><div class=" ${ BDFDB . disCN . slidermarkvalue } ">24:00</div><div class=" ${ BDFDB . disCN . slidermarkdash } "></div></div></div><div class=" ${ BDFDB . disCN . sliderbar } "><div class=" ${ BDFDB . disCN . sliderbarfill } "></div></div><div class=" ${ BDFDB . disCN . slidertrack } "><div class=" ${ BDFDB . disCN . slidergrabber } timer-grabber" key="timer1" style="left: ${ values . timer1 } %;"></div><div class=" ${ BDFDB . disCN . slidergrabber } timer-grabber" key="timer2" style="left: ${ values . timer2 } %;"></div><div class=" ${ BDFDB . disCN . slidergrabber } date-grabber" key="current" style="left: ${ this . getPercent ( new Date ( ) ) } %; cursor: help !important; height: 12px; margin-top: -7px;"></div></div></div></div> ` ) ;
e . node . parentElement . appendChild ( settingsbox ) ;
let slider = settingsbox . querySelector ( BDFDB . dotCN . slider ) ;
2019-09-04 12:34:02 +02:00
2019-03-07 23:52:24 +01:00
BDFDB . initElements ( settingsbox , this ) ;
2019-03-07 23:15:46 +01:00
this . updateSlider ( slider , values ) ;
2019-11-10 19:27:14 +01:00
BDFDB . ListenerUtils . addToChildren ( settingsbox , "mousedown" , BDFDB . dotCN . slidergrabber + ".timer-grabber" , event => { this . dragSlider ( event . currentTarget ) ; } ) ;
BDFDB . ListenerUtils . addToChildren ( settingsbox , "mouseenter" , BDFDB . dotCN . slidergrabber + ".date-grabber" , event => { this . showCurrentTime ( event . currentTarget ) ; } ) ;
BDFDB . ListenerUtils . addToChildren ( settingsbox , "click" , ".settings-switch" , event => {
2019-03-07 23:52:24 +01:00
this . startInterval ( ) ;
2019-11-10 19:27:14 +01:00
BDFDB . DOMUtils . toggleClass ( slider , BDFDB . disCN . sliderdisabled , ! event . currentTarget . checked ) ;
2019-03-07 23:52:24 +01:00
} ) ;
2019-03-07 23:15:46 +01:00
}
}
2019-09-04 12:34:02 +02:00
2019-03-07 23:15:46 +01:00
startInterval ( ) {
2019-11-01 11:09:32 +01:00
BDFDB . TimeUtils . clear ( this . checkInterval ) ;
2019-10-22 20:16:05 +02:00
if ( BDFDB . DataUtils . get ( this , "settings" , "running" ) ) {
2019-11-10 19:27:14 +01:00
let values = BDFDB . DataUtils . get ( this , "values" ) ;
let inverted = values . timer1 > values . timer2 ;
let timer1LOW = this . getTime ( values . timer1 ) , timer2LOW = this . getTime ( values . timer2 ) ;
let timer1HIGH = this . getHighTime ( timer2LOW ) , timer2HIGH = this . getHighTime ( timer1LOW ) ;
let check = ( ) => {
let currenttime = new Date ( ) ;
let currenthours = currenttime . getHours ( ) ;
let currentminutes = currenttime . getMinutes ( ) ;
2019-03-07 23:52:24 +01:00
if ( inverted ) this . changeTheme ( ! ( this . checkTime ( timer1LOW , timer1HIGH , [ currenthours , currentminutes ] ) ) ) ;
else this . changeTheme ( this . checkTime ( timer2LOW , timer2HIGH , [ currenthours , currentminutes ] ) ) ;
2019-04-17 10:56:54 +02:00
} ;
check ( ) ;
2019-11-01 11:09:32 +01:00
this . checkInterval = BDFDB . TimeUtils . interval ( check , 60000 ) ;
2019-03-07 23:52:24 +01:00
}
2019-03-07 23:15:46 +01:00
}
2019-09-04 12:34:02 +02:00
2019-03-07 23:15:46 +01:00
checkTime ( timerLOW , timerHIGH , time ) {
return timerHIGH [ 0 ] > time [ 0 ] || timerHIGH [ 0 ] == time [ 0 ] && timerHIGH [ 1 ] >= time [ 1 ] || time [ 0 ] > timerLOW [ 0 ] || time [ 0 ] == timerLOW [ 0 ] && time [ 1 ] >= timerLOW [ 1 ] ;
}
2019-09-04 12:34:02 +02:00
2019-03-07 23:15:46 +01:00
changeTheme ( dark ) {
2019-11-10 19:27:14 +01:00
let theme = BDFDB . DiscordUtils . getTheme ( ) ;
2019-11-01 10:50:15 +01:00
if ( dark && theme == BDFDB . disCN . themelight ) BDFDB . LibraryModules . SettingsUtils . updateLocalSettings ( { theme : "dark" } ) ;
else if ( ! dark && theme == BDFDB . disCN . themedark ) BDFDB . LibraryModules . SettingsUtils . updateLocalSettings ( { theme : "light" } ) ;
2019-03-07 23:15:46 +01:00
}
2019-09-04 12:34:02 +02:00
2019-04-17 10:56:54 +02:00
showCurrentTime ( grabber ) {
2019-11-10 19:27:14 +01:00
let currenttime = new Date ( ) ;
let currenthours = currenttime . getHours ( ) ;
let currentminutes = currenttime . getMinutes ( ) ;
let bubble = BDFDB . DOMUtils . create ( ` <span class=" ${ BDFDB . disCN . sliderbubble } "> ${ ( currenthours > 9 ? currenthours : ( "0" + currenthours ) ) + ":" + ( currentminutes > 9 ? currentminutes : ( "0" + currentminutes ) ) } </span> ` ) ;
2019-04-17 10:56:54 +02:00
grabber . appendChild ( bubble ) ;
grabber . style . setProperty ( "left" , ` ${ this . getPercent ( currenttime ) } % ` ) ;
2019-11-10 19:27:14 +01:00
let mouseleave = ( ) => {
2019-10-23 11:10:01 +02:00
BDFDB . DOMUtils . remove ( bubble ) ;
2019-04-17 10:56:54 +02:00
grabber . removeEventListener ( "mouseleave" , mouseleave ) ;
} ;
grabber . addEventListener ( "mouseleave" , mouseleave ) ;
}
2019-09-04 12:34:02 +02:00
2019-03-07 23:15:46 +01:00
dragSlider ( grabber ) {
2019-11-10 19:27:14 +01:00
let track = grabber . parentNode ;
2019-10-23 11:10:01 +02:00
if ( BDFDB . DOMUtils . containsClass ( track . parentNode , BDFDB . disCN . sliderdisabled ) ) return ;
2019-11-10 19:27:14 +01:00
let type = grabber . getAttribute ( "key" ) ;
let input = track . parentNode . querySelector ( ` ${ BDFDB . dotCN . sliderinput } [key=" ${ type } "] ` ) ;
let values = BDFDB . DataUtils . get ( this , "values" ) ;
2019-03-07 23:15:46 +01:00
2019-10-23 11:10:01 +02:00
BDFDB . DOMUtils . appendLocalStyle ( "disableTextSelection" , ` *{user-select: none !important;} ` ) ;
2019-03-07 23:15:46 +01:00
2019-11-10 19:27:14 +01:00
let value = values [ type ] ;
let othervalue = type == "timer1" ? values . timer2 : values . timer1 ;
let sY = 0 ;
let sHalfW = BDFDB . DOMUtils . getRects ( grabber ) . width / 2 ;
let sMinX = BDFDB . DOMUtils . getRects ( track ) . left ;
let sMaxX = sMinX + BDFDB . DOMUtils . getRects ( track ) . width ;
let bubble = BDFDB . DOMUtils . create ( ` <span class=" ${ BDFDB . disCN . sliderbubble } "> ${ this . getTime ( value , true ) } </span> ` ) ;
2019-03-07 23:15:46 +01:00
grabber . appendChild ( bubble ) ;
2019-11-10 19:27:14 +01:00
let mouseup = ( ) => {
2019-03-07 23:15:46 +01:00
document . removeEventListener ( "mouseup" , mouseup ) ;
document . removeEventListener ( "mousemove" , mousemove ) ;
2019-10-23 11:10:01 +02:00
BDFDB . DOMUtils . remove ( bubble ) ;
BDFDB . DOMUtils . removeLocalStyle ( "disableTextSelection" ) ;
2019-10-22 20:16:05 +02:00
BDFDB . DataUtils . save ( value , this , "values" , type ) ;
2019-03-07 23:15:46 +01:00
this . startInterval ( ) ;
} ;
2019-11-10 19:27:14 +01:00
let mousemove = e => {
2019-03-07 23:15:46 +01:00
sY = e . clientX > sMaxX ? sMaxX - sHalfW : ( e . clientX < sMinX ? sMinX - sHalfW : e . clientX - sHalfW ) ;
2019-10-23 11:10:01 +02:00
value = BDFDB . NumberUtils . mapRange ( [ sMinX - sHalfW , sMaxX - sHalfW ] , [ 0 , 100 ] , sY ) ;
2019-03-07 23:15:46 +01:00
input . value = value ;
grabber . style . setProperty ( "left" , value + "%" ) ;
bubble . innerText = this . getTime ( value , true ) ;
values [ type ] = value ;
this . updateSlider ( track . parentNode , values ) ;
} ;
document . addEventListener ( "mouseup" , mouseup ) ;
document . addEventListener ( "mousemove" , mousemove ) ;
}
2019-09-04 12:34:02 +02:00
2019-03-07 23:15:46 +01:00
updateSlider ( slider , values ) {
2019-11-10 19:27:14 +01:00
let bar = slider . querySelector ( BDFDB . dotCN . sliderbar ) ;
let fill = slider . querySelector ( BDFDB . dotCN . sliderbarfill ) ;
let inverted = values . timer1 > values . timer2 ;
2019-03-07 23:15:46 +01:00
fill . style . setProperty ( "width" , ( inverted ? ( values . timer1 - values . timer2 ) : ( values . timer2 - values . timer1 ) ) + "%" ) ;
fill . style . setProperty ( "margin-left" , ( inverted ? values . timer2 : values . timer1 ) + "%" ) ;
fill . style . setProperty ( "background-color" , inverted ? "#66757F" : "#E0C460" , "important" ) ;
bar . style . setProperty ( "background-color" , inverted ? "#E0C460" : "#66757F" , "important" ) ;
}
2019-09-04 12:34:02 +02:00
2019-03-07 23:15:46 +01:00
getTime ( percent , stringify ) {
2019-11-10 19:27:14 +01:00
let time = BDFDB . NumberUtils . mapRange ( [ 0 , 100 ] , [ 0 , 1440 ] , percent ) / 60 ;
let hours = Math . floor ( time ) ;
let minutes = Math . floor ( ( time - hours ) * 60 ) ;
2019-03-07 23:15:46 +01:00
return stringify ? ( hours > 9 ? hours : ( "0" + hours ) ) + ":" + ( minutes > 9 ? minutes : ( "0" + minutes ) ) : [ hours , minutes ] ;
}
2019-09-04 12:34:02 +02:00
2019-04-17 10:56:54 +02:00
getPercent ( time ) {
if ( ! time ) return 0 ;
2019-11-10 19:27:14 +01:00
let hours = Array . isArray ( time ) ? time [ 0 ] : ( typeof time == "object" && typeof time . getHours == "function" ? time . getHours ( ) : 0 ) ;
let minutes = Array . isArray ( time ) ? time [ 1 ] : ( typeof time == "object" && typeof time . getMinutes == "function" ? time . getMinutes ( ) : 0 ) ;
2019-10-23 11:10:01 +02:00
return BDFDB . NumberUtils . mapRange ( [ 0 , 1440 ] , [ 0 , 100 ] , ( hours * 60 ) + minutes ) ;
2019-04-17 10:56:54 +02:00
}
2019-09-04 12:34:02 +02:00
2019-03-07 23:15:46 +01:00
getHighTime ( timer ) {
2019-11-10 19:27:14 +01:00
let hours = timer [ 0 ] ;
let minutes = timer [ 1 ] - 1 ;
2019-03-07 23:15:46 +01:00
if ( minutes < 0 ) {
minutes = 59 ;
hours -= 1 ;
}
if ( hours < 0 ) hours = 0 ;
return [ hours , minutes ] ;
}
}