BetterDiscordAddons/Plugins/CompleteTimestamps/CompleteTimestamps.plugin.js

306 lines
13 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name CompleteTimestamps
2021-03-05 13:26:41 +01:00
* @author DevilBro
2020-10-20 23:25:34 +02:00
* @authorId 278543574059057154
2021-08-11 21:28:21 +02:00
* @version 1.5.9
2021-03-05 13:26:41 +01:00
* @description Replaces Timestamps with your own custom Timestamps
2020-10-20 23:25:34 +02:00
* @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien
* @patreon https://www.patreon.com/MircoWittrien
2021-03-09 15:10:55 +01:00
* @website https://mwittrien.github.io/
* @source https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/CompleteTimestamps/
2021-03-10 09:17:37 +01:00
* @updateUrl https://mwittrien.github.io/BetterDiscordAddons/Plugins/CompleteTimestamps/CompleteTimestamps.plugin.js
2020-10-20 23:25:34 +02:00
*/
2018-10-11 10:21:26 +02: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": "CompleteTimestamps",
"author": "DevilBro",
2021-08-11 21:28:21 +02:00
"version": "1.5.9",
2021-03-04 11:48:11 +01:00
"description": "Replaces Timestamps with your own custom Timestamps"
},
"changeLog": {
2021-08-11 21:28:21 +02:00
"fixed": {
"Markup Timestamps": "Now also targets message markup timestamps completely"
}
2019-05-26 13:55:26 +02:00
}
2020-09-19 20:49:33 +02:00
};
2020-11-13 19:47:44 +01:00
2021-08-11 17:33:29 +02:00
return (window.Lightcord && !Node.prototype.isPrototypeOf(window.Lightcord) || window.LightCord && !Node.prototype.isPrototypeOf(window.LightCord)) ? class {
2021-06-15 13:42:02 +02:00
getName () {return config.info.name;}
getAuthor () {return config.info.author;}
getVersion () {return config.info.version;}
getDescription () {return "Do not use LightCord!";}
load () {BdApi.alert("Attention!", "By using LightCord you are risking your Discord Account, due to using a 3rd Party Client. Switch to an official Discord Client (https://discord.com/) with the proper BD Injection (https://betterdiscord.app/)");}
start() {}
stop() {}
} : !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-03-05 13:14:18 +01:00
if (!e && b && r.statusCode == 200) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
2021-03-06 14:59:48 +01:00
else BdApi.alert("Error", "Could not download BDFDB Library Plugin. Try again later or download it manually from GitHub: https://mwittrien.github.io/downloader/?library");
2021-02-01 17:13:13 +01:00
});
}
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
});
}
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]) => {
2021-03-15 11:51:17 +01:00
var currentMode, tooltipIsSame;
2021-04-08 11:08:15 +02:00
var MessageTimestampComponent;
2020-09-19 20:49:33 +02:00
2020-10-09 21:09:35 +02:00
return class CompleteTimestamps extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2021-04-08 11:08:15 +02:00
MessageTimestampComponent = (BDFDB.ModuleUtils.findByName("MessageTimestamp", false) || {exports: null}).exports;
2020-09-19 20:49:33 +02:00
this.defaults = {
2021-03-15 11:51:17 +01:00
general: {
2021-03-06 20:50:00 +01:00
showInChat: {value: true, description: "Replace Chat Timestamps with complete Timestamps"},
showInEmbed: {value: true, description: "Replace Embed Timestamps with complete Timestamps"},
2021-07-28 19:56:09 +02:00
showInMarkup: {value: true, description: "Replace Markup Timestamps with complete Timestamps"},
2021-03-06 20:50:00 +01:00
showInAuditLogs: {value: true, description: "Replace Audit Log Timestamps with complete Timestamps"},
changeForChat: {value: true, description: "Change the Time for Chat Time Tooltips"},
2021-07-28 19:56:09 +02:00
changeForEdit: {value: true, description: "Change the Time for Edited Time Tooltips"},
changeForMarkup: {value: true, description: "Change the Time for Markup Timestamp Tooltips"}
2020-09-19 20:49:33 +02:00
},
2021-03-15 11:51:17 +01:00
dates: {
timestampDate: {value: {}, description: "Chat Timestamp"},
tooltipDate: {value: {}, description: "Tooltip Timestamp"}
2020-09-19 20:49:33 +02:00
}
};
this.patchedModules = {
2021-07-28 19:56:09 +02:00
before: {
Tooltip: "render"
},
2020-09-19 20:49:33 +02:00
after: {
Message: "default",
2021-04-08 11:08:15 +02:00
MessageTimestamp: "default",
2020-09-19 20:49:33 +02:00
Embed: "render",
2021-03-06 20:50:00 +01:00
SystemMessage: "default",
AuditLog: "render"
2020-09-19 20:49:33 +02:00
}
};
2021-02-20 13:41:20 +01:00
this.css = `
${BDFDB.dotCN.messagetimestamp} {
z-index: 1;
}
`;
2020-09-19 20:49:33 +02:00
}
2021-01-06 12:38:36 +01:00
onStart () {
2020-02-04 08:20:40 +01:00
this.forceUpdateAll();
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop () {
2020-02-04 08:20:40 +01:00
this.forceUpdateAll();
2020-09-07 13:07:20 +02:00
BDFDB.DOMUtils.removeLocalStyle(this.name + "CompactCorrection");
2020-02-04 08:20:40 +01:00
}
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
getSettingsPanel (collapseStates = {}) {
2021-03-15 11:51:17 +01:00
let settingsPanel;
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, {
2020-09-19 20:49:33 +02:00
collapseStates: collapseStates,
2021-03-15 11:51:17 +01:00
children: _ => {
let settingsItems = [];
settingsItems.push(Object.keys(this.defaults.general).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["general", key],
label: this.defaults.general[key].description,
value: this.settings.general[key]
})));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
className: BDFDB.disCN.marginbottom8
}));
settingsItems.push(Object.keys(this.defaults.dates).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.DateInput, Object.assign({}, this.settings.dates[key], {
label: this.defaults.dates[key].description,
onChange: valueObj => {
this.SettingsUpdated = true;
this.settings.dates[key] = valueObj;
BDFDB.DataUtils.save(this.settings.dates, this, "dates");
}
}))));
return settingsItems.flat(10);
}
});
2020-02-04 08:20:40 +01:00
}
2018-10-11 10:21:26 +02:00
2021-01-06 12:38:36 +01:00
onSettingsClosed () {
2020-09-19 20:49:33 +02:00
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
this.forceUpdateAll();
}
2020-02-04 08:20:40 +01:00
}
2021-01-06 12:38:36 +01:00
forceUpdateAll () {
2021-01-15 18:59:45 +01:00
currentMode = null;
2021-03-15 11:51:17 +01:00
tooltipIsSame = BDFDB.equals(this.settings.dates.timestampDate, this.settings.dates.tooltipDate);
2020-09-19 20:49:33 +02:00
BDFDB.PatchUtils.forceAllUpdates(this);
BDFDB.MessageUtils.rerenderAll();
2020-02-04 08:20:40 +01:00
}
2019-01-17 23:48:29 +01:00
2021-07-28 19:56:09 +02:00
processTooltip (e) {
if (typeof e.instance.props.tooltipClassName == "string" && e.instance.props.tooltipClassName.indexOf(BDFDB.disCN.messagemarkuptimestamptooltip) > -1) {
e.instance.props._originalText = e.instance.props._originalText || e.instance.props.text;
if (this.settings.general.showInMarkup) {
if (tooltipIsSame) e.instance.props.delay = 99999999999999999999;
2021-08-11 20:32:10 +02:00
let timestamp = this.formatTimestamp(this.settings.dates.timestampDate, e.instance.props._originalText);
2021-07-28 19:56:09 +02:00
let renderChildren = e.instance.props.children;
e.instance.props.children = (...args) => {
let renderedChildren = renderChildren(...args);
if (BDFDB.ArrayUtils.is(renderedChildren.props.children)) renderedChildren.props.children[1] = timestamp;
else renderedChildren.props.children = timestamp;
return renderedChildren;
};
}
if (this.settings.general.changeForMarkup) e.instance.props.text = this.formatTimestamp(this.settings.dates.tooltipDate, e.instance.props._originalText);
}
}
2020-09-19 20:49:33 +02:00
processMessage (e) {
2021-04-08 11:08:15 +02:00
if (MessageTimestampComponent) {
let timestamp = BDFDB.ReactUtils.findChild(e.returnvalue, {filter: c => c && c.type && c.type.type && c.type.type.displayName == "MessageTimestamp"});
if (timestamp) timestamp.type.type = MessageTimestampComponent.default;
2020-09-19 20:49:33 +02:00
}
2020-02-04 08:20:40 +01:00
}
2020-09-19 20:49:33 +02:00
2021-04-08 11:08:15 +02:00
processMessageTimestamp (e) {
let tooltipWrapper = BDFDB.ReactUtils.findChild(e.returnvalue, {name: "Tooltip"});
if (!tooltipWrapper) return;
let childClassName = BDFDB.ObjectUtils.get(e, "instance.props.children.props.className");
if (childClassName && childClassName.indexOf(BDFDB.disCN.messageedited) > -1) {
if (this.settings.general.changeForEdit) tooltipWrapper.props.text = this.formatTimestamp(this.settings.dates.tooltipDate, e.instance.props.timestamp._i);
}
else {
if (this.settings.general.showInChat && !e.instance.props.cozyAlt) {
if (tooltipIsSame) tooltipWrapper.props.delay = 99999999999999999999;
let timestamp = this.formatTimestamp(this.settings.dates.timestampDate, e.instance.props.timestamp._i);
let renderChildren = tooltipWrapper.props.children;
tooltipWrapper.props.children = (...args) => {
let renderedChildren = renderChildren(...args);
if (BDFDB.ArrayUtils.is(renderedChildren.props.children)) renderedChildren.props.children[1] = timestamp;
else renderedChildren.props.children = timestamp;
return renderedChildren;
};
this.setMaxWidth(e.returnvalue, e.instance.props.compact);
}
2021-07-28 19:56:09 +02:00
if (this.settings.general.changeForChat) tooltipWrapper.props.text = this.formatTimestamp(this.settings.dates.tooltipDate, e.instance.props.timestamp._i);
2020-09-19 20:49:33 +02:00
}
2020-02-04 08:20:40 +01:00
}
2020-09-19 20:49:33 +02:00
processEmbed (e) {
2021-03-15 11:51:17 +01:00
if (e.instance.props.embed && e.instance.props.embed.timestamp && this.settings.general.showInEmbed) {
2020-10-30 16:39:17 +01:00
let process = returnvalue => {
2020-11-19 16:51:14 +01:00
let [children, index] = BDFDB.ReactUtils.findParent(returnvalue, {props: [["className", BDFDB.disCN.embedfootertext]]});
2021-03-06 20:50:00 +01:00
if (index > -1) {
2021-03-15 12:37:03 +01:00
if (BDFDB.ArrayUtils.is(children[index].props.children)) children[index].props.children[children[index].props.children.length - 1] = this.formatTimestamp(this.settings.dates.timestampDate, e.instance.props.embed.timestamp._i);
else children[index].props.children = this.formatTimestamp(this.settings.dates.timestampDate, e.instance.props.embed.timestamp._i);
2021-03-06 20:50:00 +01:00
}
2020-10-30 16:39:17 +01:00
};
if (typeof e.returnvalue.props.children == "function") {
let childrenRender = e.returnvalue.props.children;
2021-07-05 16:33:10 +02:00
e.returnvalue.props.children = BDFDB.TimeUtils.suppress((...args) => {
2020-10-30 16:39:17 +01:00
let children = childrenRender(...args);
process(children);
return children;
2021-07-05 16:33:10 +02:00
}, "", this);
2020-10-30 16:39:17 +01:00
}
else process(e.returnvalue);
2020-02-04 08:20:40 +01:00
}
}
2019-04-07 21:33:13 +02:00
2020-09-19 20:49:33 +02:00
processSystemMessage (e) {
2021-03-15 11:51:17 +01:00
if (e.instance.props.timestamp && this.settings.general.showInChat) {
2020-09-19 20:49:33 +02:00
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {name: "time"});
2021-03-15 12:37:03 +01:00
if (index > -1) children[index].props.children = this.formatTimestamp(this.settings.dates.timestampDate, e.instance.props.timestamp._i);
2020-09-19 20:49:33 +02:00
}
2018-10-11 10:21:26 +02:00
}
2021-03-06 20:50:00 +01:00
processAuditLog (e) {
2021-03-15 11:51:17 +01:00
if (e.instance.props.log && this.settings.general.showInAuditLogs) {
2021-03-06 20:50:00 +01:00
if (typeof e.returnvalue.props.children == "function") {
let childrenRender = e.returnvalue.props.children;
2021-07-05 16:33:10 +02:00
e.returnvalue.props.children = BDFDB.TimeUtils.suppress((...args) => {
2021-03-06 20:50:00 +01:00
let children = childrenRender(...args);
this.editLog(e.instance.props.log, children);
return children;
2021-07-05 16:33:10 +02:00
}, "", this);
2021-03-06 20:50:00 +01:00
}
else this.editLog(e.instance.props.log, e.returnvalue);
}
}
editLog (log, returnvalue) {
if (!log || !returnvalue) return;
2021-04-08 11:08:15 +02:00
let [children, index] = BDFDB.ReactUtils.findParent(returnvalue, {props: [["className", BDFDB.disCN.auditlogtimestamp]]});
2021-03-15 12:37:03 +01:00
if (index > -1) children[index].props.children = this.formatTimestamp(this.settings.dates.timestampDate, log.timestampStart._i);
2021-03-06 20:50:00 +01:00
}
2020-09-19 20:49:33 +02:00
2021-03-15 12:37:03 +01:00
formatTimestamp (format, date) {
return BDFDB.LibraryModules.StringUtils.upperCaseFirstChar(BDFDB.LibraryComponents.DateInput.format(format, date));
}
2020-09-19 20:49:33 +02:00
setMaxWidth (timestamp, compact) {
if (currentMode != compact) {
currentMode = compact;
if (timestamp.props.className && typeof timestamp.type == "string") {
2021-03-15 12:37:03 +01:00
let tempTimestamp = BDFDB.DOMUtils.create(`<div class="${BDFDB.disCN.messagecompact}"><${timestamp.type} class="${timestamp.props.className}" style="width: auto !important;">${this.formatTimestamp(this.settings.dates.timestampDate, new Date(253402124399995))}</${timestamp.type}></div>`);
2020-09-19 20:49:33 +02:00
document.body.appendChild(tempTimestamp);
let width = BDFDB.DOMUtils.getRects(tempTimestamp.firstElementChild).width + 10;
tempTimestamp.remove();
BDFDB.DOMUtils.appendLocalStyle(this.name + "CompactCorrection", `
${BDFDB.dotCN.messagecompact + BDFDB.dotCN.messagewrapper} {
padding-left: ${44 + width}px;
}
${BDFDB.dotCNS.messagecompact + BDFDB.dotCN.messagecontents} {
margin-left: -${44 + width}px;
padding-left: ${44 + width}px;
text-indent: calc(-${44 + width}px - -1rem);
}
2021-04-08 20:04:09 +02:00
${BDFDB.dotCNS.messagecompact + BDFDB.dotCNS.messageheader + BDFDB.dotCN.messagetimestamp} {
2020-09-19 20:49:33 +02:00
width: ${width}px;
}
`);
}
}
}
};
2020-10-09 21:09:35 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(config));
2021-03-10 05:13:20 +01:00
})();