BetterDiscordApp-rauenzi/src/builtins/24hour.js

34 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-05-30 23:18:52 +02:00
import Builtin from "../structs/builtin";
import {Utilities, DiscordModules} from "modules";
export default new class TwentyFourHour extends Builtin {
get name() {return "24Hour";}
2019-06-06 06:28:43 +02:00
get category() {return "general";}
get id() {return "twentyFourHour";}
2019-05-30 23:18:52 +02:00
enabled() {
this.inject24Hour();
}
disabled() {
2019-06-03 22:25:08 +02:00
if (!this.cancel24Hour) return;
this.cancel24Hour();
delete this.cancel24Hour;
2019-05-30 23:18:52 +02:00
}
inject24Hour() {
if (this.cancel24Hour) return;
const twelveHour = new RegExp(`([0-9]{1,2}):([0-9]{1,2})\\s(AM|PM)`);
const convert = (data) => {
const matched = data.returnValue.match(twelveHour);
if (!matched || matched.length !== 4) return;
if (matched[3] === "AM") return data.returnValue = data.returnValue.replace(matched[0], `${matched[1] === "12" ? "00" : matched[1].padStart(2, "0")}:${matched[2]}`);
return data.returnValue = data.returnValue.replace(matched[0], `${matched[1] === "12" ? "12" : parseInt(matched[1]) + 12}:${matched[2]}`);
};
const cancelCozy = Utilities.monkeyPatch(DiscordModules.TimeFormatter, "calendarFormat", {after: convert}); // Called in Cozy mode
const cancelCompact = Utilities.monkeyPatch(DiscordModules.TimeFormatter, "dateFormat", {after: convert}); // Called in Compact mode
this.cancel24Hour = () => {cancelCozy(); cancelCompact();}; // Cancel both
}
};