Update DisplayLargeMessages.plugin.js

This commit is contained in:
Mirco Wittrien 2021-03-17 09:59:07 +01:00
parent 08615b7e9f
commit 8b8463013e
1 changed files with 47 additions and 44 deletions

View File

@ -2,7 +2,7 @@
* @name DisplayLargeMessages
* @author DevilBro
* @authorId 278543574059057154
* @version 1.0.8
* @version 1.0.9
* @description Injects 'message.txt' into Discord or open any '.txt' File in a Window
* @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien
@ -17,12 +17,12 @@ module.exports = (_ => {
"info": {
"name": "DisplayLargeMessages",
"author": "DevilBro",
"version": "1.0.8",
"version": "1.0.9",
"description": "Injects 'message.txt' into Discord or open any '.txt' File in a Window"
},
"changeLog": {
"improved": {
"Open in Popout": "The open button now gets added to any .txt file not only 'message.txt'"
"Open in Popout": "The Open in extra Window now gets added for any plain text file and tries to wrap it in a code block with syntax highlighting"
}
}
};
@ -65,18 +65,19 @@ module.exports = (_ => {
return template.content.firstElementChild;
}
} : (([Plugin, BDFDB]) => {
const plainFileTypes = ["ada", "adb", "ads", "applescript", "as", "asc", "ascii", "ascx", "asm", "asmx", "asp", "aspx", "atom", "au3", "awk", "bas", "bash", "bashrc", "bat", "bbcolors", "bcp", "bdsgroup", "bdsproj", "bib", "bowerrc", "c", "cbl", "cc", "cfc", "cfg", "cfm", "cfml", "cgi", "cjs", "clj", "cljs", "cls", "cmake", "cmd", "cnf", "cob", "code-snippets", "coffee", "coffeekup", "conf", "cp", "cpp", "cpt", "cpy", "crt", "cs", "csh", "cson", "csproj", "csr", "css", "csslintrc", "csv", "ctl", "curlrc", "cxx", "d", "dart", "dfm", "diff", "dof", "dpk", "dpr", "dproj", "dtd", "eco", "editorconfig", "ejs", "el", "elm", "emacs", "eml", "ent", "erb", "erl", "eslintignore", "eslintrc", "ex", "exs", "f", "f03", "f77", "f90", "f95", "fish", "for", "fpp", "frm", "fs", "fsproj", "fsx", "ftn", "gemrc", "gemspec", "gitattributes", "gitconfig", "gitignore", "gitkeep", "gitmodules", "go", "gpp", "gradle", "graphql", "groovy", "groupproj", "grunit", "gtmpl", "gvimrc", "h", "haml", "hbs", "hgignore", "hh", "hpp", "hrl", "hs", "hta", "htaccess", "htc", "htm", "html", "htpasswd", "hxx", "iced", "iml", "inc", "inf", "info", "ini", "ino", "int", "irbrc", "itcl", "itermcolors", "itk", "jade", "java", "jhtm", "jhtml", "js", "jscsrc", "jshintignore", "jshintrc", "json", "json5", "jsonld", "jsp", "jspx", "jsx", "ksh", "less", "lhs", "lisp", "log", "ls", "lsp", "lua", "m", "m4", "mak", "map", "markdown", "master", "md", "mdown", "mdwn", "mdx", "metadata", "mht", "mhtml", "mjs", "mk", "mkd", "mkdn", "mkdown", "ml", "mli", "mm", "mxml", "nfm", "nfo", "noon", "npmignore", "npmrc", "nuspec", "nvmrc", "ops", "pas", "pasm", "patch", "pbxproj", "pch", "pem", "pg", "php", "php3", "php4", "php5", "phpt", "phtml", "pir", "pl", "pm", "pmc", "pod", "pot", "prettierrc", "properties", "props", "pt", "pug", "purs", "py", "pyx", "r", "rake", "rb", "rbw", "rc", "rdoc", "rdoc_options", "resx", "rexx", "rhtml", "rjs", "rlib", "ron", "rs", "rss", "rst", "rtf", "rvmrc", "rxml", "s", "sass", "scala", "scm", "scss", "seestyle", "sh", "shtml", "sln", "sls", "spec", "sql", "sqlite", "sqlproj", "srt", "ss", "sss", "st", "strings", "sty", "styl", "stylus", "sub", "sublime-build", "sublime-commands", "sublime-completions", "sublime-keymap", "sublime-macro", "sublime-menu", "sublime-project", "sublime-settings", "sublime-workspace", "sv", "svc", "svg", "swift", "t", "tcl", "tcsh", "terminal", "tex", "text", "textile", "tg", "tk", "tmLanguage", "tmpl", "tmTheme", "tpl", "ts", "tsv", "tsx", "tt", "tt2", "ttml", "twig", "txt", "v", "vb", "vbproj", "vbs", "vcproj", "vcxproj", "vh", "vhd", "vhdl", "vim", "viminfo", "vimrc", "vm", "vue", "webapp", "webmanifest", "wsc", "x-php", "xaml", "xht", "xhtml", "xml", "xs", "xsd", "xsl", "xslt", "y", "yaml", "yml", "zsh", "zshrc"];
var encodedMessages, requestedMessages, pendingRequests, oldMessages, updateTimeout;
var settings = {}, amounts = {};
return class DisplayLargeMessages extends Plugin {
onLoad () {
this.defaults = {
settings: {
onDemand: {value: false, description: "Inject the content of 'message.txt' on demand and not automatically"},
addOpenButton: {value: true, description: "Add a button to preview the contents of 'message.txt' in a popup"}
general: {
onDemand: {value: false, description: "Inject the Content of 'Message.txt' On-Demand and not automatically"},
addOpenButton: {value: true, description: "Add a Button to Preview the Contents of Plain Files in an extra Window"}
},
amounts: {
maxFileSize: {value: 10, min: 0, description: "Max Filesize a file will be read automatically", note: "in KB / 0 = inject all / ignored in On-Demand"}
maxFileSize: {value: 10, min: 0, description: "Max File Size a File will be read automatically", note: "in KB / 0 = inject all / ignored in On-Demand"}
}
};
@ -139,35 +140,39 @@ module.exports = (_ => {
}
getSettingsPanel (collapseStates = {}) {
let settingsPanel, settingsItems = [];
let settingsPanel;
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, {
collapseStates: collapseStates,
children: _ => {
let settingsItems = [];
for (let key in settings) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key],
onChange: _ => {
if (key == "onDemand") BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
for (let key in this.defaults.general) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["general", key],
label: this.defaults.general[key].description,
value: this.settings.general[key],
onChange: key == "onDemand" && (_ => BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates))
}));
for (let key in this.defaults.amounts) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "TextInput",
childProps: {
type: "number"
},
plugin: this,
keys: ["amounts", key],
disabled: key == "maxFileSize" && this.settings.general.onDemand,
label: this.defaults.amounts[key].description,
note: this.defaults.amounts[key].note,
basis: "20%",
min: this.defaults.amounts[key].min,
max: this.defaults.amounts[key].max,
value: this.settings.amounts[key]
}));
return settingsItems.flat(10);
}
}));
for (let key in amounts) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "TextInput",
childProps: {
type: "number"
},
plugin: this,
keys: ["amounts", key],
disabled: key == "maxFileSize" && settings.onDemand,
label: this.defaults.amounts[key].description,
note: this.defaults.amounts[key].note,
basis: "20%",
min: this.defaults.amounts[key].min,
max: this.defaults.amounts[key].max,
value: amounts[key]
}));
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
});
}
onSettingsClosed () {
@ -180,10 +185,7 @@ module.exports = (_ => {
}
}
forceUpdateAll () {
settings = BDFDB.DataUtils.get(this, "settings");
amounts = BDFDB.DataUtils.get(this, "amounts");
forceUpdateAll () {
BDFDB.PatchUtils.forceAllUpdates(this);
BDFDB.MessageUtils.rerenderAll();
}
@ -234,8 +236,8 @@ module.exports = (_ => {
stream.content.attachments = oldMessages[message.id].attachments;
delete oldMessages[message.id];
}
else if (!settings.onDemand && !requestedMessages.includes(message.id)) for (let attachment of message.attachments) {
if (attachment.filename == "message.txt" && (!amounts.maxFileSize || (amounts.maxFileSize >= attachment.size/1024))) {
else if (!this.settings.general.onDemand && !requestedMessages.includes(message.id)) for (let attachment of message.attachments) {
if (attachment.filename == "message.txt" && (!this.settings.amounts.maxFileSize || (this.settings.amounts.maxFileSize >= attachment.size/1024))) {
requestedMessages.push(message.id);
BDFDB.LibraryRequires.request(attachment.url, (error, response, body) => {
encodedMessages[message.id] = {
@ -251,8 +253,9 @@ module.exports = (_ => {
processAttachment (e) {
if (typeof e.instance.props.filename == "string") {
let fileType = (e.instance.props.filename.split(".").pop() || "").toLowerCase();
e.returnvalue.props.children.splice(2, 0, [
e.instance.props.filename == "message.txt" && (settings.onDemand || amounts.maxFileSize && (amounts.maxFileSize < e.instance.props.size/1024)) && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
e.instance.props.filename == "message.txt" && (this.settings.general.onDemand || this.settings.amounts.maxFileSize && (this.settings.amounts.maxFileSize < e.instance.props.size/1024)) && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: this.labels.button_injectattachment,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Anchor, {
className: BDFDB.disCN._displaylargemessagesinjectbuttonwrapper,
@ -283,7 +286,7 @@ module.exports = (_ => {
}
})
}),
e.instance.props.filename.endsWith(".txt") && settings.addOpenButton && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
fileType && plainFileTypes.includes(fileType) && this.settings.general.addOpenButton && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: BDFDB.LanguageUtils.LanguageStrings.OPEN,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Anchor, {
className: BDFDB.disCN._displaylargemessagespopoutbuttonwrapper,
@ -312,7 +315,7 @@ module.exports = (_ => {
message: new BDFDB.DiscordObjects.Message({
author: message.author,
channel_id: channel.id,
content: body
content: e.instance.props.filename == "message.txt" ? body : `\`\`\`${fileType}\n${body}\`\`\``
}),
channel: channel
})