// melonking.net / forum.melonland.net - Melon Dynamic Links System v0.2 // Displays random image or text links to melon projects and melonland community links! // Example link placeholder: // let melonLinks = {}; melonLinks.loopTime = 25; // In seconds melonLinks.loop = undefined; window.addEventListener("DOMContentLoaded", (event) => { if (melonLinks.elements != undefined) { return; // The script has already been setup } melonLinks.api = "https://brain.melonking.net/link"; melonLinks.elements = document.getElementsByClassName("melonLink"); updateLinks(); setInterval(updateLinks, melonLinks.loopTime * 1000); }); function updateLinks() { for (let i = 0; i < melonLinks.elements.length; i++) { let melonLinkElement = melonLinks.elements[i]; let format = melonLinkElement.dataset.melonLinkFormat; if (format == undefined) { console.log("Attempted to load a melon dynamic link, but the format data was missing!"); continue; } let isText = format.includes("text"); fetch(melonLinks.api + "?format=" + format) .then((res) => res.json()) .then((link) => { if (link.status != "success") { console.log(link.msg); return; } // First time setup of a link! if (!melonLinkElement.hasChildNodes()) { let link = document.createElement("a"); link.target = "_blank"; link.href = "#"; if (!isText) { let img = document.createElement("img"); img.alt = "Loading..."; img.src = "#"; link.appendChild(img); } melonLinkElement.appendChild(link); } melonLinkElement.firstChild.href = link.banner.url; // Text Link if (isText) { melonLinkElement.firstChild.innerText = link.banner.alt; return; } // Image Link melonLinkElement.firstChild.firstChild.alt = link.banner.alt; fetch(link.banner.image) .then((e) => e.blob()) .then((e) => { melonLinkElement.firstChild.firstChild.src = URL.createObjectURL(e); }); }) .catch((err) => console.log(err)); } }