From 6c0724ac273746addd4aa322bb2cc2c6f7bba7c2 Mon Sep 17 00:00:00 2001 From: Evgeny Kuznetsov Date: Thu, 1 Dec 2022 00:08:48 +0300 Subject: feat: handle footnotes --- hovercard.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/hovercard.js b/hovercard.js index f9de005..a4b7506 100644 --- a/hovercard.js +++ b/hovercard.js @@ -13,7 +13,9 @@ var hovercards = { if (hovercard_data) { var new_box = document.createElement("div"); new_box.classList.add("hovercard"); - new_box.innerHTML = "" + hovercard_data["title"] + "" + if (hovercard_data["title"]) { + new_box.innerHTML = "" + hovercard_data["title"] + "" + } if (hovercard_data["description"]) { new_box.innerHTML = new_box.innerHTML + "

" + hovercard_data["description"] + "

"; } @@ -57,17 +59,25 @@ var hovercards = { }); let url = links[i].href.replace(/\/+$/, ""); if (!hovercards[url]) { - let req = 'https://indieweb-glue.evgenykuznetsov.org/api/opengraph?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); + 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} } - }; - xmlhttp.open("GET", r, true); - xmlhttp.send(); + } else { + let req = 'https://indieweb-glue.evgenykuznetsov.org/api/opengraph?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(); + } } } } \ No newline at end of file -- cgit v1.2.3