Update TimedLightDarkMode.plugin.js

This commit is contained in:
Mirco Wittrien 2019-11-10 19:27:14 +01:00
parent 68997f3c3e
commit 9dd3e1a778
1 changed files with 51 additions and 52 deletions

View File

@ -3,7 +3,7 @@
class TimedLightDarkMode {
getName () {return "TimedLightDarkMode";}
getVersion () {return "1.0.4";}
getVersion () {return "1.0.5";}
getAuthor () {return "DevilBro";}
@ -11,12 +11,11 @@ class TimedLightDarkMode {
constructor () {
this.changelog = {
"improved":[["'Improved'","Plugin no longer changes the theme in the remote settings, meaning it will not change the appearance settings for your other devices nor will it save settings after you reload, forbidden by BDs overstrict rules on automated API requests, since this is a time based plugin"]],
"fixed":[["Slider","Slider now properly gets added to the appearance settings again"]]
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
};
this.patchModules = {
"RadioGroup":"componentDidMount"
RadioGroup: "componentDidMount"
};
}
@ -38,7 +37,7 @@ class TimedLightDarkMode {
start () {
if (!global.BDFDB) global.BDFDB = {myPlugins:{}};
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this;
var libraryScript = document.querySelector('head script#BDFDBLibraryScript');
let libraryScript = document.querySelector('head script#BDFDBLibraryScript');
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
@ -83,21 +82,21 @@ class TimedLightDarkMode {
// begin of own functions
processRadioGroup (instance, wrapper, returnvalue) {
if (instance.props && Array.isArray(instance.props.options) && instance.props.options[0] && (instance.props.options[0].value == "light" || instance.props.options[0].value == "dark") && instance.props.options[1] && (instance.props.options[1].value == "light" || instance.props.options[1].value == "dark") && wrapper.parentElement.firstElementChild.innerText && wrapper.parentElement.firstElementChild.innerText.toUpperCase() == BDFDB.LanguageUtils.LanguageStrings.THEME.toUpperCase()) {
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()) {
let settings = BDFDB.DataUtils.get(this, "settings");
var values = BDFDB.DataUtils.get(this, "values");
var 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>`);
wrapper.parentElement.appendChild(settingsbox);
var slider = settingsbox.querySelector(BDFDB.dotCN.slider);
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);
BDFDB.initElements(settingsbox, this);
this.updateSlider(slider, values);
BDFDB.ListenerUtils.addToChildren(settingsbox, "mousedown", BDFDB.dotCN.slidergrabber + ".timer-grabber", e => {this.dragSlider(e.currentTarget);});
BDFDB.ListenerUtils.addToChildren(settingsbox, "mouseenter", BDFDB.dotCN.slidergrabber + ".date-grabber", e => {this.showCurrentTime(e.currentTarget);});
BDFDB.ListenerUtils.addToChildren(settingsbox, "click", ".settings-switch", e => {
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 => {
this.startInterval();
BDFDB.DOMUtils.toggleClass(slider, BDFDB.disCN.sliderdisabled, !e.currentTarget.checked);
BDFDB.DOMUtils.toggleClass(slider, BDFDB.disCN.sliderdisabled, !event.currentTarget.checked);
});
}
}
@ -105,14 +104,14 @@ class TimedLightDarkMode {
startInterval () {
BDFDB.TimeUtils.clear(this.checkInterval);
if (BDFDB.DataUtils.get(this, "settings", "running")) {
var values = BDFDB.DataUtils.get(this, "values");
var inverted = values.timer1 > values.timer2;
var timer1LOW = this.getTime(values.timer1), timer2LOW = this.getTime(values.timer2);
var timer1HIGH = this.getHighTime(timer2LOW), timer2HIGH = this.getHighTime(timer1LOW);
var check = () => {
var currenttime = new Date();
var currenthours = currenttime.getHours();
var currentminutes = currenttime.getMinutes();
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();
if (inverted) this.changeTheme(!(this.checkTime(timer1LOW, timer1HIGH, [currenthours, currentminutes])));
else this.changeTheme(this.checkTime(timer2LOW, timer2HIGH, [currenthours, currentminutes]));
};
@ -126,19 +125,19 @@ class TimedLightDarkMode {
}
changeTheme (dark) {
var theme = BDFDB.DiscordUtils.getTheme();
let theme = BDFDB.DiscordUtils.getTheme();
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"});
}
showCurrentTime (grabber) {
var currenttime = new Date();
var currenthours = currenttime.getHours();
var currentminutes = currenttime.getMinutes();
var bubble = BDFDB.DOMUtils.create(`<span class="${BDFDB.disCN.sliderbubble}">${(currenthours > 9 ? currenthours : ("0" + currenthours)) + ":" + (currentminutes > 9 ? currentminutes : ("0" + currentminutes))}</span>`);
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>`);
grabber.appendChild(bubble);
grabber.style.setProperty("left", `${this.getPercent(currenttime)}%`);
var mouseleave = () => {
let mouseleave = () => {
BDFDB.DOMUtils.remove(bubble);
grabber.removeEventListener("mouseleave", mouseleave);
};
@ -146,23 +145,23 @@ class TimedLightDarkMode {
}
dragSlider (grabber) {
var track = grabber.parentNode;
let track = grabber.parentNode;
if (BDFDB.DOMUtils.containsClass(track.parentNode, BDFDB.disCN.sliderdisabled)) return;
var type = grabber.getAttribute("key");
var input = track.parentNode.querySelector(`${BDFDB.dotCN.sliderinput}[key="${type}"]`);
var values = BDFDB.DataUtils.get(this, "values");
let type = grabber.getAttribute("key");
let input = track.parentNode.querySelector(`${BDFDB.dotCN.sliderinput}[key="${type}"]`);
let values = BDFDB.DataUtils.get(this, "values");
BDFDB.DOMUtils.appendLocalStyle("disableTextSelection", `*{user-select: none !important;}`);
var value = values[type];
var othervalue = type == "timer1" ? values.timer2 : values.timer1;
var sY = 0;
var sHalfW = BDFDB.DOMUtils.getRects(grabber).width/2;
var sMinX = BDFDB.DOMUtils.getRects(track).left;
var sMaxX = sMinX + BDFDB.DOMUtils.getRects(track).width;
var bubble = BDFDB.DOMUtils.create(`<span class="${BDFDB.disCN.sliderbubble}">${this.getTime(value, true)}</span>`);
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>`);
grabber.appendChild(bubble);
var mouseup = () => {
let mouseup = () => {
document.removeEventListener("mouseup", mouseup);
document.removeEventListener("mousemove", mousemove);
BDFDB.DOMUtils.remove(bubble);
@ -170,7 +169,7 @@ class TimedLightDarkMode {
BDFDB.DataUtils.save(value, this, "values", type);
this.startInterval();
};
var mousemove = e => {
let mousemove = e => {
sY = e.clientX > sMaxX ? sMaxX - sHalfW : (e.clientX < sMinX ? sMinX - sHalfW : e.clientX - sHalfW);
value = BDFDB.NumberUtils.mapRange([sMinX - sHalfW, sMaxX - sHalfW], [0, 100], sY);
input.value = value;
@ -184,9 +183,9 @@ class TimedLightDarkMode {
}
updateSlider (slider, values) {
var bar = slider.querySelector(BDFDB.dotCN.sliderbar);
var fill = slider.querySelector(BDFDB.dotCN.sliderbarfill);
var inverted = values.timer1 > values.timer2;
let bar = slider.querySelector(BDFDB.dotCN.sliderbar);
let fill = slider.querySelector(BDFDB.dotCN.sliderbarfill);
let inverted = values.timer1 > values.timer2;
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");
@ -194,22 +193,22 @@ class TimedLightDarkMode {
}
getTime (percent, stringify) {
var time = BDFDB.NumberUtils.mapRange([0, 100], [0, 1440], percent)/60;
var hours = Math.floor(time);
var minutes = Math.floor((time - hours) * 60);
let time = BDFDB.NumberUtils.mapRange([0, 100], [0, 1440], percent)/60;
let hours = Math.floor(time);
let minutes = Math.floor((time - hours) * 60);
return stringify ? (hours > 9 ? hours : ("0" + hours)) + ":" + (minutes > 9 ? minutes : ("0" + minutes)) : [hours,minutes];
}
getPercent (time) {
if (!time) return 0;
var hours = Array.isArray(time) ? time[0] : (typeof time == "object" && typeof time.getHours == "function" ? time.getHours() : 0);
var minutes = Array.isArray(time) ? time[1] : (typeof time == "object" && typeof time.getMinutes == "function" ? time.getMinutes() : 0);
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);
return BDFDB.NumberUtils.mapRange([0, 1440], [0, 100], (hours * 60) + minutes);
}
getHighTime (timer) {
var hours = timer[0];
var minutes = timer[1] - 1;
let hours = timer[0];
let minutes = timer[1] - 1;
if (minutes < 0) {
minutes = 59;
hours -= 1;