BetterDiscordAddons/Plugins/CopyRawMessage/CopyRawMessage.plugin.js

166 lines
7.2 KiB
JavaScript
Raw Normal View History

2020-07-26 17:02:25 +02:00
//META{"name":"CopyRawMessage","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/CopyRawMessage","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/CopyRawMessage/CopyRawMessage.plugin.js"}*//
2019-07-22 08:59:33 +02:00
2020-07-01 13:13:50 +02:00
var CopyRawMessage = (_ => {
var settings = {};
2020-02-04 08:20:40 +01:00
return class CopyRawMessage {
getName () {return "CopyRawMessage";}
2019-07-22 08:59:33 +02:00
2020-07-01 13:13:50 +02:00
getVersion () {return "1.1.1";}
2019-07-22 08:59:33 +02:00
2020-02-04 08:20:40 +01:00
getAuthor () {return "DevilBro";}
2019-07-22 08:59:33 +02:00
2020-02-04 08:20:40 +01:00
getDescription () {return "Adds a entry in the contextmenu when you right click a message that allows you to copy the raw contents of a message.";}
2019-07-22 08:59:33 +02:00
2020-02-04 08:20:40 +01:00
constructor () {
2020-05-14 17:33:14 +02:00
this.changelog = {
2020-07-01 13:13:50 +02:00
"improved":[["Only copy selection","Selecting a part of a message/embed and clicking copy raw will now only copy the selected part (including formating symbols like ~, _ and ` etc., can be disabled"]]
2020-02-04 08:20:40 +01:00
};
}
2019-09-04 12:34:02 +02:00
2020-07-01 13:13:50 +02:00
initConstructor () {
this.defaults = {
settings: {
copyOnlySelected: {value:true, description:"Only copy selected text of a message"}
}
};
}
getSettingsPanel (collapseStates = {}) {
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
let settings = BDFDB.DataUtils.get(this, "settings");
let settingsPanel, settingsItems = [];
for (let key in settings) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
className: BDFDB.disCN.marginbottom8,
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
}
2020-04-11 19:32:58 +02:00
// Legacy
2020-08-21 16:17:47 +02:00
load () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) BDFDB.PluginUtils.load(this);
}
2019-07-22 08:59:33 +02:00
2020-02-04 08:20:40 +01:00
start () {
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
libraryScript.setAttribute("date", performance.now());
libraryScript.addEventListener("load", _ => {this.initialize();});
document.head.appendChild(libraryScript);
}
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
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-07-22 08:59:33 +02:00
}
2020-02-04 08:20:40 +01:00
initialize () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
if (this.started) return;
BDFDB.PluginUtils.init(this);
2020-07-01 13:13:50 +02:00
this.forceUpdateAll();
2020-02-04 08:20:40 +01:00
}
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
2019-07-22 08:59:33 +02:00
}
2020-02-04 08:20:40 +01:00
stop () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
this.stopping = true;
2019-10-22 11:37:23 +02:00
2020-02-04 08:20:40 +01:00
BDFDB.PluginUtils.clear(this);
}
2019-07-22 08:59:33 +02:00
}
2019-09-04 12:34:02 +02:00
2020-04-11 19:32:58 +02:00
// Begin of own functions
2020-02-04 08:20:40 +01:00
2020-07-01 13:13:50 +02:00
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
this.forceUpdateAll();
}
}
2020-02-04 08:20:40 +01:00
onMessageContextMenu (e) {
2020-05-02 20:47:15 +02:00
if (e.instance.props.message) {
2020-07-01 13:13:50 +02:00
let content = e.instance.props.message.content;
2020-06-30 13:06:18 +02:00
let messageString = [e.instance.props.message.content, BDFDB.ArrayUtils.is(e.instance.props.message.attachments) && e.instance.props.message.attachments.map(n => n.url)].flat(10).filter(n => n).join("\n");
2020-07-01 13:13:50 +02:00
let selectedText = settings.copyOnlySelected && document.getSelection().toString().trim();
if (selectedText) messageString = BDFDB.StringUtils.extractSelection(messageString, selectedText);
2020-05-02 18:27:18 +02:00
let embed = BDFDB.DOMUtils.getParent(BDFDB.dotCN.embedwrapper, e.instance.props.target);
2020-06-23 22:20:03 +02:00
let embedData = e.instance.props.message.embeds[embed ? Array.from(embed.parentElement.querySelectorAll(BDFDB.dotCN.embedwrapper)).indexOf(embed) : -1];
2020-06-30 13:06:18 +02:00
let embedString = embedData && [embedData.rawTitle, embedData.rawDescription, BDFDB.ArrayUtils.is(embedData.fields) && embedData.fields.map(n => [n.rawName, n.rawValue]), BDFDB.ObjectUtils.is(embedData.image) && embedData.image.url, BDFDB.ObjectUtils.is(embedData.footer) && embedData.footer.text].flat(10).filter(n => n).join("\n");
2020-07-01 13:13:50 +02:00
if (selectedText) embedString = BDFDB.StringUtils.extractSelection(embedString, selectedText);
2020-05-19 16:07:47 +02:00
let hint = BDFDB.BDUtils.isPluginEnabled("MessageUtilities") ? BDFDB.BDUtils.getPlugin("MessageUtilities").getActiveShortcutString("Copy_Raw") : null;
2020-05-02 20:47:15 +02:00
let entries = [
2020-06-30 13:06:18 +02:00
messageString && BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
2020-05-02 20:47:15 +02:00
label: BDFDB.LanguageUtils.LanguageStrings.COPY_TEXT + " (Raw)",
2020-05-19 16:07:47 +02:00
id: BDFDB.ContextMenuUtils.createItemId(this.name, "copy-message"),
hint: hint && (_ => {
2020-05-20 14:40:43 +02:00
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.MenuItems.MenuHint, {
2020-05-19 16:07:47 +02:00
hint: hint
});
}),
2020-05-02 20:47:15 +02:00
action: _ => {
2020-06-30 13:06:18 +02:00
BDFDB.LibraryRequires.electron.clipboard.write({text:messageString});
2020-05-02 20:47:15 +02:00
}
}),
2020-06-23 22:20:03 +02:00
embedString && BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
2020-05-02 20:47:15 +02:00
label: BDFDB.LanguageUtils.LanguageStrings.COPY_TEXT + " (Raw Embed)",
2020-05-19 16:07:47 +02:00
id: BDFDB.ContextMenuUtils.createItemId(this.name, "copy-embed"),
2020-05-02 20:47:15 +02:00
action: _ => {
2020-06-23 22:20:03 +02:00
BDFDB.LibraryRequires.electron.clipboard.write({text:embedString});
2020-05-02 20:47:15 +02:00
}
})
].filter(n => n);
if (entries.length) {
2020-05-20 14:40:43 +02:00
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "devmode-copy-id", group: true});
2020-05-20 11:55:46 +02:00
children.splice(index > -1 ? index : children.length, 0, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuGroup, {
2020-05-02 20:47:15 +02:00
children: entries
}));
}
2020-02-04 08:20:40 +01:00
}
2019-07-22 08:59:33 +02:00
}
2019-08-19 19:22:49 +02:00
2020-02-04 08:20:40 +01:00
onMessageOptionContextMenu (e) {
2020-05-02 20:47:15 +02:00
if (e.instance.props.message && e.instance.props.message.content) {
2020-05-20 14:40:43 +02:00
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "mark-unread"});
2020-05-20 11:55:46 +02:00
children.splice(index + 1, 0, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
2020-02-04 08:20:40 +01:00
label: BDFDB.LanguageUtils.LanguageStrings.COPY_TEXT + " (Raw)",
2020-05-19 16:07:47 +02:00
id: BDFDB.ContextMenuUtils.createItemId(this.name, "copy-message"),
2020-05-14 17:33:14 +02:00
icon: _ => {
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.menuicon,
name: BDFDB.LibraryComponents.SvgIcon.Names.RAW_TEXT
});
},
2020-02-04 08:20:40 +01:00
action: _ => {
BDFDB.LibraryRequires.electron.clipboard.write({text:e.instance.props.message.content});
}
}));
}
2019-09-11 12:14:43 +02:00
}
2020-07-01 13:13:50 +02:00
forceUpdateAll () {
settings = BDFDB.DataUtils.get(this, "settings");
}
2019-08-19 19:22:49 +02:00
}
2020-07-26 16:39:51 +02:00
})();
module.exports = CopyRawMessage;