// class name to use hovercards on can be passes as "data-class" parameter var cl = document.currentScript.getAttribute("data-class") || "content"; var curURL = document.URL // these will not attempt to get fetched, so can be used to override things var hovercards = { curURL : {} }; function hover (target_link) { var hovercard_data = hovercards[target_link.href.replace(/\/+$/, "")]; if (hovercard_data) { var new_box = document.createElement("div"); new_box.classList.add("hovercard"); if (hovercard_data["pname"]) { console.log(hovercard_data); var nick = "" if (hovercard_data["nickname"]) { nick = " (" + hovercard_data["nickname"] + ")" } new_box.innerHTML = "" + hovercard_data["pname"] + nick + "" } else { if (hovercard_data["title"]) { new_box.innerHTML = "" + hovercard_data["title"] + "" } } if (hovercard_data["description"]) { new_box.innerHTML = new_box.innerHTML + "

" + hovercard_data["description"] + "

"; } if (hovercard_data["note"]) { new_box.innerHTML = new_box.innerHTML + "

" + hovercard_data["note"] + "

"; } // if image available, add it if (hovercard_data["uphoto"]) { new_box.innerHTML = "" + hovercard_data["pname"] + "
" + new_box.innerHTML; } else { if (hovercard_data["image"]) { new_box.innerHTML = "" + hovercard_data["title"] + "
" + new_box.innerHTML; } } new_box.style.top = window.scrollY + target_link.getBoundingClientRect().top + "px"; new_box.style.left = window.scrollX + target_link.getBoundingClientRect().left + "px"; new_box.id = "tooltip"; document.body.appendChild(new_box); } } function unhover (target_link) { var active_hovercard = document.getElementById("tooltip"); if (active_hovercard) { active_hovercard.parentNode.removeChild(active_hovercard); } } var article = document.getElementsByClassName(cl); for (let a of article) { var links = a.getElementsByTagName("a"); for (var i = 0; i < links.length; i++) { links[i].addEventListener("mouseover", function() { hover(this); }); links[i].addEventListener("mouseout", function() { unhover(this); }); // these handlers are so that the hovercard will appear/disappear // with keyboard actions links[i].addEventListener("onfocus", function() { hover(this); }); links[i].addEventListener("onfocusout", function() { unhover(this); }); let url = links[i].href.replace(/\/+$/, ""); if (!hovercards[url]) { if (links[i].hostname == window.location.hostname && links[i].pathname == window.location.pathname) { if (links[i].hash) { // this is an achor somewhere on the same page, give a peek in case it's a footnote let desc = document.getElementById(links[i].hash.replace("#", "")).innerText hovercards[url] = {"description": desc} } } else { let req = 'https://indieweb-glue.evgenykuznetsov.org/api/pageinfo?url='; if (Boolean(links[i].closest('.h-card'))) { req = 'https://indieweb-glue.evgenykuznetsov.org/api/hcard?url='; }; req += url; const r = encodeURI(req); var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { hovercards[url] = JSON.parse(this.responseText); } }; xmlhttp.open("GET", r, true); xmlhttp.send(); } } } }