BetterDiscordAddons/Plugins/TimedLightDarkMode/TimedLightDarkMode.plugin.js

255 lines
13 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name TimedLightDarkMode
* @authorId 278543574059057154
* @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien
* @patreon https://www.patreon.com/MircoWittrien
* @website https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/TimedLightDarkMode
* @source https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/TimedLightDarkMode/TimedLightDarkMode.plugin.js
2020-11-12 17:30:48 +01:00
* @updateUrl https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/TimedLightDarkMode/TimedLightDarkMode.plugin.js
2020-10-20 23:25:34 +02:00
*/
2019-03-07 23:15:46 +01:00
2020-09-19 20:49:33 +02:00
module.exports = (_ => {
2020-10-09 21:09:35 +02:00
const config = {
2020-09-19 20:49:33 +02:00
"info": {
"name": "TimedLightDarkMode",
"author": "DevilBro",
2021-01-23 13:22:18 +01:00
"version": "1.1.0",
2021-03-05 11:21:21 +01:00
"description": "Adds a Time Slider to the Appearance Settings"
2020-09-19 20:49:33 +02:00
}
};
2020-11-13 19:47:44 +01:00
2020-10-09 21:09:35 +02:00
return !window.BDFDB_Global || (!window.BDFDB_Global.loaded && !window.BDFDB_Global.started) ? class {
2021-01-06 12:38:36 +01:00
getName () {return config.info.name;}
getAuthor () {return config.info.author;}
getVersion () {return config.info.version;}
2021-02-01 17:13:13 +01:00
getDescription () {return `The Library Plugin needed for ${config.info.name} is missing. Open the Plugin Settings to download it. \n\n${config.info.description}`;}
downloadLibrary () {
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
2021-02-02 11:22:26 +01:00
if (!e && b && b.indexOf(`* @name BDFDB`) > -1) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
2021-02-01 17:13:13 +01:00
else BdApi.alert("Error", "Could not download BDFDB Library Plugin, try again later or download it manually from GitHub: https://github.com/mwittrien/BetterDiscordAddons/tree/master/Library/");
});
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
load () {
2020-11-19 16:51:14 +01:00
if (!window.BDFDB_Global || !Array.isArray(window.BDFDB_Global.pluginQueue)) window.BDFDB_Global = Object.assign({}, window.BDFDB_Global, {pluginQueue: []});
2020-09-19 20:49:33 +02:00
if (!window.BDFDB_Global.downloadModal) {
window.BDFDB_Global.downloadModal = true;
2021-01-14 16:14:44 +01:00
BdApi.showConfirmationModal("Library Missing", `The Library Plugin needed for ${config.info.name} is missing. Please click "Download Now" to install it.`, {
2020-09-19 20:49:33 +02:00
confirmText: "Download Now",
cancelText: "Cancel",
onCancel: _ => {delete window.BDFDB_Global.downloadModal;},
2020-09-20 08:15:13 +02:00
onConfirm: _ => {
delete window.BDFDB_Global.downloadModal;
2021-02-01 17:13:13 +01:00
this.downloadLibrary();
2020-09-20 08:15:13 +02:00
}
2020-09-19 20:49:33 +02:00
});
2020-06-08 20:41:38 +02:00
}
2020-09-19 20:49:33 +02:00
if (!window.BDFDB_Global.pluginQueue.includes(config.info.name)) window.BDFDB_Global.pluginQueue.push(config.info.name);
2020-10-09 21:09:35 +02:00
}
2021-01-06 12:38:36 +01:00
start () {this.load();}
stop () {}
getSettingsPanel () {
2020-11-28 23:12:09 +01:00
let template = document.createElement("template");
2021-01-14 16:14:44 +01:00
template.innerHTML = `<div style="color: var(--header-primary); font-size: 16px; font-weight: 300; white-space: pre; line-height: 22px;">The Library Plugin needed for ${config.info.name} is missing.\nPlease click <a style="font-weight: 500;">Download Now</a> to install it.</div>`;
2021-02-01 17:13:13 +01:00
template.content.firstElementChild.querySelector("a").addEventListener("click", this.downloadLibrary);
2020-11-28 23:12:09 +01:00
return template.content.firstElementChild;
}
2020-10-09 21:09:35 +02:00
} : (([Plugin, BDFDB]) => {
2020-09-19 20:49:33 +02:00
var checkInterval, changeTimeout, disableChanging;
var settings = {}, values = {};
2020-10-09 21:09:35 +02:00
return class TimedLightDarkMode extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2020-09-19 20:49:33 +02:00
this.defaults = {
settings: {
running: {value: true}
},
values: {
2020-11-19 16:51:14 +01:00
timer1: {value: 25},
timer2: {value: 75}
2020-09-19 20:49:33 +02:00
}
};
2020-08-21 20:14:57 +02:00
2020-09-19 20:49:33 +02:00
this.patchedModules = {
after: {
2020-11-21 19:59:21 +01:00
UserSettingsAppearance: "render"
2020-09-19 20:49:33 +02:00
}
};
}
2021-01-23 13:22:18 +01:00
onStart () {
2020-09-11 19:31:36 +02:00
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.SettingsUtils, "updateLocalSettings", {after: e => {
2021-01-23 13:22:18 +01:00
if (BDFDB.ObjectUtils.is(e.methodArguments[0]) && e.methodArguments[0].theme && settings.running) {
2020-08-21 20:14:57 +02:00
BDFDB.TimeUtils.clear(changeTimeout);
disableChanging = true;
changeTimeout = BDFDB.TimeUtils.timeout(_ => {
disableChanging = false;
}, 1000*60*10);
}
}});
2019-09-04 12:34:02 +02:00
2020-06-08 20:41:38 +02:00
this.startInterval();
2019-09-04 12:34:02 +02:00
2020-09-11 19:31:36 +02:00
BDFDB.PatchUtils.forceAllUpdates(this);
2020-06-08 20:41:38 +02:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop () {
2020-08-21 20:14:57 +02:00
BDFDB.TimeUtils.clear(checkInterval);
2019-09-04 12:34:02 +02:00
2020-06-08 20:41:38 +02:00
BDFDB.DOMUtils.remove(BDFDB.dotCN._timedlightdarkmodetimersettings);
}
2019-09-04 12:34:02 +02:00
2020-11-21 19:59:21 +01:00
processUserSettingsAppearance (e) {
let formItem = BDFDB.ReactUtils.findChild(e.returnvalue, {name: "FormItem", props: [["title", BDFDB.LanguageUtils.LanguageStrings.THEME]]});
if (formItem && formItem.props) {
let slider;
formItem.props.children = [
formItem.props.children,
BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCNS._timedlightdarkmodetimersettings + BDFDB.disCN.margintop20,
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["settings", "running"],
value: settings.running,
label: `${BDFDB.LanguageUtils.LanguageStrings.THEME} Timer`,
tag: BDFDB.LibraryComponents.FormComponents.FormTitle.Tags.H5,
childProps: {
checkedColor: BDFDB.DiscordConstants.Colors.STATUS_GREEN
},
onChange: (value, instance) => {
this.startInterval();
if (slider) BDFDB.DOMUtils.toggleClass(slider, BDFDB.disCN.sliderdisabled, !value);
}
}),
BDFDB.ReactUtils.elementToReact(BDFDB.DOMUtils.create(`<div class="${BDFDB.disCNS.slider + BDFDB.disCN.margintop20}${!settings.running ? (" " + BDFDB.disCN.sliderdisabled): ""}"><input type="number" timer="timer1" class="${BDFDB.disCN.sliderinput}" value="${values.timer1}" readonly=""><input type="number" timer="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.disCNS.slidergrabber + BDFDB.disCN._timedlightdarkmodetimergrabber}" timer="timer1" style="left: ${values.timer1}%;"></div><div class="${BDFDB.disCNS.slidergrabber + BDFDB.disCN._timedlightdarkmodetimergrabber}" timer="timer2" style="left: ${values.timer2}%;"></div><div class="${BDFDB.disCNS.slidergrabber + BDFDB.disCN._timedlightdarkmodedategrabber}" timer="current" style="left: ${this.getPercent(new Date())}%; cursor: help !important; height: 12px; margin-top: -7px;"></div></div></div>`), node => {
if (Node.prototype.isPrototypeOf(node)) {
slider = node;
this.updateSlider(slider, values);
BDFDB.ListenerUtils.addToChildren(slider, "mousedown", BDFDB.dotCN._timedlightdarkmodetimergrabber, event => {this.dragSlider(event.currentTarget);});
BDFDB.ListenerUtils.addToChildren(slider, "mouseenter", BDFDB.dotCN._timedlightdarkmodedategrabber, event => {this.showCurrentTime(event.currentTarget);});
}
})
]
})
].flat(10).filter(n => n);
2020-09-19 20:49:33 +02:00
}
2020-06-08 20:41:38 +02:00
}
2019-09-04 12:34:02 +02:00
2021-01-06 12:38:36 +01:00
startInterval () {
2020-09-19 20:49:33 +02:00
BDFDB.TimeUtils.clear(checkInterval);
settings = BDFDB.DataUtils.get(this, "settings");
values = BDFDB.DataUtils.get(this, "values");
2021-01-23 13:22:18 +01:00
disableChanging = false;
2020-09-19 20:49:33 +02:00
if (settings.running) {
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 = _ => {
if (disableChanging) return;
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]));
};
check();
checkInterval = BDFDB.TimeUtils.interval(_ => {check();}, 1000*60*1);
}
2020-06-08 20:41:38 +02:00
}
2019-09-04 12:34:02 +02:00
2020-09-19 20:49:33 +02: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
2020-09-19 20:49:33 +02:00
changeTheme (dark) {
let theme = BDFDB.DiscordUtils.getTheme();
2020-11-19 16:51:14 +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"});
2020-09-19 20:49:33 +02:00
}
2019-09-04 12:34:02 +02:00
2020-09-19 20:49:33 +02:00
showCurrentTime (grabber) {
let currentTime = new Date();
let currentHours = currentTime.getHours();
let currentMinutes = currentTime.getMinutes();
grabber.style.setProperty("left", `${this.getPercent(currentTime)}%`);
BDFDB.TooltipUtils.create(grabber, (currentHours > 9 ? currentHours : ("0" + currentHours)) + ":" + (currentMinutes > 9 ? currentMinutes : ("0" + currentMinutes)), {color: "grey"});
}
2019-09-04 12:34:02 +02:00
2020-09-19 20:49:33 +02:00
dragSlider (grabber) {
let track = grabber.parentNode;
if (BDFDB.DOMUtils.containsClass(track.parentNode, BDFDB.disCN.sliderdisabled)) return;
let timer = grabber.getAttribute("timer");
let input = track.parentNode.querySelector(`${BDFDB.dotCN.sliderinput}[timer="${timer}"]`);
BDFDB.DOMUtils.appendLocalStyle("disableTextSelection", `*{user-select: none !important;}`);
let value = values[timer];
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 tooltip = BDFDB.TooltipUtils.create(grabber, this.getTime(value, true), {color: "grey", perssist: true});
let tooltipContent = tooltip.querySelector(BDFDB.dotCN.tooltipcontent);
2020-11-21 19:59:21 +01:00
let mouseUp = _ => {
document.removeEventListener("mouseup", mouseUp);
document.removeEventListener("mousemove", mouseMove);
2020-09-19 20:49:33 +02:00
BDFDB.DOMUtils.removeLocalStyle("disableTextSelection");
BDFDB.DataUtils.save(value, this, "values", timer);
BDFDB.DOMUtils.remove(tooltip);
this.startInterval();
};
2020-11-21 19:59:21 +01:00
let mouseMove = e => {
2020-09-19 20:49:33 +02:00
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;
grabber.style.setProperty("left", value + "%");
tooltipContent.innerText = this.getTime(value, true);
tooltip.update();
values[timer] = value;
this.updateSlider(track.parentNode, values);
};
2020-11-21 19:59:21 +01:00
document.addEventListener("mouseup", mouseUp);
document.addEventListener("mousemove", mouseMove);
2020-09-19 20:49:33 +02:00
}
2019-09-04 12:34:02 +02:00
2020-09-19 20:49:33 +02:00
updateSlider (slider, values) {
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");
bar.style.setProperty("background-color", inverted ? "#E0C460" : "#66757F", "important");
}
2019-09-04 12:34:02 +02:00
2020-09-19 20:49:33 +02:00
getTime (percent, stringify) {
let time = BDFDB.NumberUtils.mapRange([0, 100], [0, 1440], percent)/60;
let hours = Math.floor(time);
let minutes = Math.floor((time - hours) * 60);
2020-11-21 19:59:21 +01:00
return stringify ? (hours > 9 ? hours : ("0" + hours)) + ":" + (minutes > 9 ? minutes : ("0" + minutes)) : [hours, minutes];
2020-09-19 20:49:33 +02:00
}
2019-09-04 12:34:02 +02:00
2020-09-19 20:49:33 +02:00
getPercent (time) {
if (!time) return 0;
2020-11-21 19:59:21 +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);
2020-09-19 20:49:33 +02:00
return BDFDB.NumberUtils.mapRange([0, 1440], [0, 100], (hours * 60) + minutes);
2020-06-08 20:41:38 +02:00
}
2020-07-26 16:39:51 +02:00
2020-09-19 20:49:33 +02:00
getHighTime (timer) {
let hours = timer[0];
let minutes = timer[1] - 1;
if (minutes < 0) {
minutes = 59;
hours -= 1;
}
if (hours < 0) hours = 0;
return [hours, minutes];
}
};
2020-10-09 21:09:35 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(config));
2020-09-19 20:49:33 +02:00
})();