//META{"name":"AutoCorrect","website":"https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/README.md","source":"https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/AutoCorrect.plugin.js"}*//
class AutoCorrect {
getName() { return "AutoCorrect"; }
getDescription() { return "Automatically replaces misspelled words with the first correction, with optional automatic capitalization and punctuation. Requires either Windows 8 or above, Mac, or DevilBro's SpellCheck plugin."; }
getVersion() { return "1.3.5"; }
getAuthor() { return "Metalloriff"; }
getChanges() {
return {
"1.2.4" :
`
Fixed learned words.
Words ending with any non-alphanumeric character will no longer be automatically punctuated, instead of only words ending with punctuation.
Added a "prevent multiple spaces between words" setting.
Fixed auto-completes not completing when sending.
Fixed auto-emojis.
Fixed a bunch of little bugs, and probably created some.
Code blocks are no longer corrected.
`,
"1.3.4" :
`
Fixed the plugin failing to load for the first time if attach to all fields was disabled.
Fixed newlines putting random spaces.
Fixed the inability to put spaces in code blocks.
Added a "minimum auto-punctuate word count" setting.
`
};
}
load() {}
start() {
let libLoadedEvent = () => {
try{ this.onLibLoaded(); }
catch(err) { console.error(this.getName(), "fatal error, plugin could not be started!", err); try { this.stop(); } catch(err) { console.error(this.getName() + ".stop()", err); } }
};
let lib = document.getElementById("NeatoBurritoLibrary");
if(!lib) {
lib = document.createElement("script");
lib.id = "NeatoBurritoLibrary";
lib.type = "text/javascript";
lib.src = "https://rawgit.com/Metalloriff/BetterDiscordPlugins/master/Lib/NeatoBurritoLibrary.js";
document.head.appendChild(lib);
}
this.forceLoadTimeout = setTimeout(libLoadedEvent, 30000);
if(typeof window.NeatoLib !== "undefined") libLoadedEvent();
else lib.addEventListener("load", libLoadedEvent);
}
getSettingsPanel() {
setTimeout(() => {
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createToggleGroup("ac-toggle-group", "General options", [
{ title : "Use DevilBro's SpellCheck", value : "useDevilsChecker", setValue : this.settings.useDevilsChecker },
{ title : "Don't correct the same word twice", value : "dontCorrectTwice", setValue : this.settings.dontCorrectTwice },
{ title : "Automatically capitalize the first letter of every sentence", value : "autoCapitalization", setValue : this.settings.autoCapitalization },
{ title : "Automatically punctuate on send and on double space", value : "autoPunctuation", setValue : this.settings.autoPunctuation },
{ title : "Automatically correct spelling errors", value : "autoCorrect", setValue : this.settings.autoCorrect },
{ title : "Ignore messages beginning with a space", value : "ignoreEmptyStart", setValue : this.settings.ignoreEmptyStart },
{ title : "Attach corrector to every text field (set this to false for just the chatbox)", value : "attachEverywhere", setValue : this.settings.attachEverywhere },
{ title : "Prevent multiple spaces between words", value : "preventDoubleSpaces", setValue : this.settings.preventDoubleSpaces }
], choice => {
this.settings[choice.value] = !this.settings[choice.value];
this.saveSettings();
}), this.getName());
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createNewTextField("Ignored prefixes", this.settings.ignoredPrefixes, e => {
this.settings.ignoredPrefixes = e.target.value;
this.saveSettings();
}, { description : "Any messages beginning with any of these prefixes will be ignored. Good for preventing bot commands from failing due to corrections. Separate with spaces." }), this.getName());
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createHint("Hint: You can stop a word from being corrected by selecting the word, right clicking, and clicking \"Learn Word\", or by adding it to your learned words list in the settings."), this.getName());
let group = NeatoLib.Settings.Elements.createGroup("Override replacers", { style : "background-color:rgba(0,0,0,0.2);border-radius:5px;padding:10px;text-align:center;" }), list = group.lastChild;
group.insertAdjacentHTML("afterbegin", `<style>.ac-selected{background-color:#7289da;border-radius:5px;}</style>`);
let buildGroup = () => {
list.innerHTML = "";
this.selectedReplacer = -1;
for(let i = 0; i < this.settings.overrideReplacers.length; i++) {
list.insertAdjacentHTML("beforeend", `<div type="text" class="ac-replacer-field"><input value="${this.settings.overrideReplacers[i].word}" style="${NeatoLib.Settings.Styles.textField}width:45%;margin:2.5%;border:2px solid white;"><input value="${this.settings.overrideReplacers[i].replacement}" style="${NeatoLib.Settings.Styles.textField}width:45%;margin:2.5%;border:2px solid white;"></div>`);
let added = document.getElementsByClassName("ac-replacer-field")[i];
added.addEventListener("click", e => {
for(let selected of group.getElementsByClassName("ac-replacer-field")) selected.classList.remove("ac-selected");
this.selectedReplacer = i;
e.currentTarget.classList.add("ac-selected");
});
let fields = added.getElementsByTagName("input");
for(let ii = 0; ii < fields.length; ii++) fields[ii].addEventListener("focusout", e => {
this.settings.overrideReplacers[i] = { word : fields[0].value, replacement : fields[1].value };
this.saveSettings();
});
}
list.insertAdjacentElement("beforeend", NeatoLib.Settings.Elements.createButton("Add New Replacer", () => {
this.settings.overrideReplacers.push({ word : "", replacement : "" });
this.saveSettings();
buildGroup();
}, "margin-right:20px"));
list.insertAdjacentElement("beforeend", NeatoLib.Settings.Elements.createButton("Remove Selected", () => {
if(this.selectedReplacer == -1) {
NeatoLib.showToast("No replacer selected!");
return;
}
this.settings.overrideReplacers.splice(this.selectedReplacer, 1);
this.saveSettings();
buildGroup();
NeatoLib.showToast("Replacer removed!", "success");
}, "margin-left:20px"));
};
NeatoLib.Settings.pushElement(group, this.getName());
buildGroup();
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createNewTextField("Learned words (separate with spaces)", this.settings.learnedWords.join(" "), e => {
this.settings.learnedWords = e.target.value.trim().toLowerCase().split(" ").filter(x => x != "");
this.saveSettings();
}), this.getName());
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createNewTextField("Auto-punctuation: minimum word count", this.settings.autoPunctuationMin, e => {
this.settings.autoPunctuationMin = e.target.value;
this.saveSettings();
}), this.getName(), { tooltip : "If this is above 0, this many or more words will be required before auto-punctuating" });
NeatoLib.Settings.pushChangelogElements(this);
}, 0);
return NeatoLib.Settings.Elements.pluginNameLabel(this.getName());
}
saveSettings() {
if(this.settings.useDevilsChecker) {
this.devilsChecker = BdApi.getPlugin("SpellCheck");
let toggle = document.getElementById("ac-toggle-group-0");
if(toggle) toggle = toggle.getElementsByTagName("input")[0];
if(this.devilsChecker) {
if(window.pluginCookie["SpellCheck"] == false) {
NeatoLib.showToast("DevilBro's SpellCheck is not enabled! Please enable it and re-enable this setting.", "error");
if(toggle) toggle.click();
}else for(let i = 0; i < this.settings.learne