💾 Archived View for she12.midnight.pub › addArt.js captured on 2023-09-28 at 15:44:07. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

/* When not sourcing this file (together with my/any art collection),

* but pasting it directly into your virgin theme,

* you'll have to uncomment the following comment block

* aswell as the one at the bottom.

*/

/*

</style>

<script type="text/javascript" src="https://she12.midnight.pub/art.js"></script>

<script type="text/javascript">

function addAddArtButtons() {

/*

* Get the relevant DOM elements:

* textArea as a tARgeT to append the art

* formContainer to append the buttons to

*/

const textArea = document.getElementsByTagName("textarea")[0];

const formContainer = document.getElementsByTagName("form")[0];

/*

* Check if textArea and formContainer exist

* to avoid messing with sites like "site" or "notifications"

* which don't have a textArea or neither

*/

if (typeof textArea !== 'undefined' && typeof formContainer !== 'undefined') {

/*

* Add the buttons and dropdown menu

*/

formContainer.innerHTML = formContainer.innerHTML

+ '<select id="artList" name="artList"></select>'

+ '<input id="addArtBtn" value="Add Art" type="button">'

+ '<input id="delArtBtn" value="Del Art" type="button">';

/*

* Get the dropdown...

*/

const artList = document.getElementById("artList");

/*

* ..and populate it with art...

*/

for (aPiece in art) {

var listItem = document.createElement("option");

listItem.textContent = aPiece;

listItem.value = art[aPiece];

artList.appendChild(listItem);

}

/*

* ..and since they MIGHT be loaded anyway, the logos aswell

*/

for (logo in logos) {

var listItem = document.createElement("option");

listItem.textContent = 'logo/' + logo;

listItem.value = logos[logo] + '\n';

/* listItem.style = "color: purple;"; */

artList.appendChild(listItem);

}

/*

* Get and define the behaviour of the buttons

*/

const addArtBtn = document.getElementById("addArtBtn");

addArtBtn.onclick = function() {

/*

* Get the textarea again because?!

*/

const textArea = document.getElementsByTagName("textarea")[0];

/*

* Append the current selected art piece from the dropdown list,

* as preformatted gemtext

*/

textArea.value = textArea.value

+ '\n```\n'

+ artList.value

+ '```\n';

}

const delArtBtn = document.getElementById("delArtBtn");

delArtBtn.onclick = function() {

/*

* Textarea, one more time

*/

const textArea = document.getElementsByTagName("textarea")[0];

/*

* Replace the last occurence of a preformatted gemtext block

* with nothing

* Not going to explain the regex here, I am just glad it works!

* Basically we look for at least one character

* surrounded

* but not followed by

* and not containing

* three backticks (```)

*/

textArea.value = textArea.value.replace(/\n```\n((?!```\n)[\S\s])+\n```\n(?![\S\s]*\n```\n)/, "");

}

}

}

/*

* Only load when not at midnight root, because:

* 1. It would break other window.onload functions

* like change_logo

* 2. There is no textarea here anyway

* 3. Onload is still mandatory to ensure all DOM elements are fully loaded

*/

if (window.location != "https://midnight.pub/") {

window.onload = addAddArtButtons;

}

/*

</script>

<style type="text/css">