Upload files to "/"
parent
ec3e5a9644
commit
aae0df0a80
|
@ -0,0 +1,143 @@
|
|||
// Picks the first default chatbox, just to have something here by default
|
||||
// I ASSUME, this is done automatically when the page loads, but IDK how JS is actually implemented by browsers
|
||||
// You can't force me to read the documentation, I REFUSE! I just WON'T, OK?? Haha.
|
||||
let textBox = document.querySelector('[id*="reply-form-body-"]');
|
||||
let vari = "";
|
||||
let originalFunction = null;
|
||||
let fucName = null;
|
||||
let funcFull = "";
|
||||
let Marseys = [];
|
||||
|
||||
//The comments are terrible, I know. Too bad!
|
||||
|
||||
const specialCases = {
|
||||
"å": ":aa:", "Å": ":aa:",
|
||||
"æ": ":ae:", "Æ": ":ae:",
|
||||
"ø": ":oo:", "Ø": ":oo:",
|
||||
" ": ":space:",
|
||||
"?": ":questionmark:",
|
||||
"!": ":exclamationpoint:",
|
||||
".": ":period:",
|
||||
",": ":comma:",
|
||||
"-": ":dash:",
|
||||
"'": ":apostrophe:",
|
||||
'"': ":quotation:",
|
||||
"/": ":slash:", "\\": ":!slash:",
|
||||
"+": ":plus:",
|
||||
"*": ":asterisk:",
|
||||
"<": ":angle:", ">": ":!angle:",
|
||||
"=": ":equals:",
|
||||
"_": ":underscore:",
|
||||
"^": ":land:",
|
||||
":": ":colon:",
|
||||
"(": ":paren:", ")": ":!paren:",
|
||||
"{": ":brace:", "}": ":!brace:",
|
||||
"@": ":at:",
|
||||
"&": ":ampersand:",
|
||||
"|": ":vert:",
|
||||
"%": ":percent:",
|
||||
"#": ":pound:",
|
||||
"¤": "\n" // Use "¤" as a placeholder for newlines. Who uses ¤ for anything anyway?
|
||||
};
|
||||
|
||||
//This pile of shit was made by @Count_Sprpr, I hate JS like I hate the antichrist
|
||||
//Things to MAYBE improve:
|
||||
//3. Fix bugs that I haven't experienced or thought of
|
||||
|
||||
//Removes the function in the button element so that clicking the button won't immediately post the comment
|
||||
//This triggers on hover
|
||||
document.addEventListener("mouseover", function (event) {
|
||||
if (event.target.id.includes("save-reply-to-p") || event.target.id.includes("save-reply-to-c") || event.target.id.includes("save-reply-to-u")) {
|
||||
const dataOnClickValue = event.target.getAttribute('data-onclick');
|
||||
if (dataOnClickValue === null) {
|
||||
return;
|
||||
}
|
||||
fucName = dataOnClickValue.match(/(\w+)\(/);
|
||||
vari = dataOnClickValue.match(/\(([^)]*)\)/); // Match arguments inside the parentheses
|
||||
funcFull = fucName[1] + vari[0];
|
||||
event.target.removeAttribute('data-onclick');
|
||||
}
|
||||
});
|
||||
|
||||
//Adds the function back when we stop hovering over, this should hopefully prevent weird behavior.
|
||||
document.addEventListener("mouseout", function (event) {
|
||||
if (event.target.id.includes("save-reply-to-p") || event.target.id.includes("save-reply-to-c") || event.target.id.includes("save-reply-to-u")) {
|
||||
event.target.setAttribute('data-onclick', funcFull);
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("click", function (event) {
|
||||
// Will automatically set the textbox when you hit the "reply" button to spawn it
|
||||
if (event.target.id.includes("toggle-reply-")) {
|
||||
let id = "reply-form-body-" + event.target.id.toString().substr(13);
|
||||
id = '[id*="' + id + '"]';
|
||||
textBox = document.querySelector(id);
|
||||
}
|
||||
// This should handle posts
|
||||
if (event.target.id.toString() === "submit-btn") {
|
||||
document.querySelector('[id*="post-title"]').value = formattText(document.querySelector('[id*="post-title"]').value);
|
||||
document.querySelector('[id*="post-text"]').value = formattText(document.querySelector('[id*="post-text"]').value);
|
||||
}
|
||||
//If you reply to someone, click the textbox you write in, and it will be set here
|
||||
if (event.target.tagName === "TEXTAREA") {
|
||||
textBox = event.target;
|
||||
}
|
||||
|
||||
if (event.target.id.includes("save-reply-to-p") || event.target.id.includes("save-reply-to-c") || event.target.id.includes("save-reply-to-u")) {
|
||||
event.preventDefault();
|
||||
let unformattedText = textBox.value;
|
||||
//If the text has already been formatted, indicated by the unformatted text starting with ":", don't do it again. This is flawed, I know.
|
||||
if (unformattedText[0] === ":") {
|
||||
return;
|
||||
}
|
||||
// Do I need to initialize this to a string? Probably not, but it makes it easier to work with in VScode
|
||||
// I HATE dynamic typing, I HATE dynamic typing, I HATE dynamic typing
|
||||
let formattedText = "";
|
||||
formattedText = formattText(unformattedText);
|
||||
//Replace text
|
||||
textBox.value = formattedText;
|
||||
//Add the onclick function back to the button.
|
||||
event.target.setAttribute('data-onclick', funcFull);
|
||||
//Click the button.
|
||||
event.target.click();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Helper function to format the text
|
||||
function formattText(unformattedText) {
|
||||
//To fix newlines I replace them with ¤, this is bad-ish, I know
|
||||
unformattedText = unformattedText.replace(/\n/g, "¤");
|
||||
unformattedText = replaceSubstrings(unformattedText);
|
||||
let formattedText = "";
|
||||
for (let i = 0; i < unformattedText.length; i++) {
|
||||
// Checks ASCII codes, normal text is the most common symbols, so having this here first should hopefully improve performance
|
||||
// Apparently you can't test test char with String[i] in JS. Hurray.
|
||||
if ((unformattedText.charCodeAt(i) >= 48 && unformattedText.charCodeAt(i) < 58)
|
||||
|| (unformattedText.charCodeAt(i) >= 65 && unformattedText.charCodeAt(i) < 91)
|
||||
|| (unformattedText.charCodeAt(i) >= 97 && unformattedText.charCodeAt(i) < 123)) {
|
||||
// Normal cases, why can't they all be like this??
|
||||
formattedText += ":" + unformattedText[i] + ":";
|
||||
} else if (unformattedText[i] === "€") {
|
||||
formattedText += Marseys.shift();
|
||||
} else if (specialCases[unformattedText[i]] !== undefined) {
|
||||
formattedText += specialCases[unformattedText[i]];
|
||||
} else {
|
||||
formattedText += unformattedText[i];
|
||||
}
|
||||
}
|
||||
return formattedText;
|
||||
}
|
||||
|
||||
function replaceSubstrings(unformattedText) {
|
||||
// Regular expression to find substrings that start and end with ":" and "["+"]". Thanks chatGPT
|
||||
let regex = /(:[^:]*:|\[[^\]]*\])/g;
|
||||
let match;
|
||||
// Find all matches and add to the array
|
||||
while ((match = regex.exec(unformattedText)) !== null) {
|
||||
Marseys.push(match[0].trim()); // Add full match including the colons
|
||||
}
|
||||
// Replace all substrings with "€"
|
||||
let modifiedString = unformattedText.replace(regex, "€");
|
||||
return modifiedString;
|
||||
}
|
Loading…
Reference in New Issue