diff options
author | Evgeny Kuznetsov <evgeny@kuznetsov.md> | 2022-11-30 13:59:07 +0300 |
---|---|---|
committer | Evgeny Kuznetsov <evgeny@kuznetsov.md> | 2022-11-30 13:59:07 +0300 |
commit | b7f376656951b837d1f3c766734a0049afe86b44 (patch) | |
tree | eade2a83fcd8dd329e61079a941648f5976381c3 | |
parent | 6efd15dfe3e7a34853e38f9a7ac1657e2a62f71d (diff) | |
download | hovercard.js-b7f376656951b837d1f3c766734a0049afe86b44.tar.gz hovercard.js-b7f376656951b837d1f3c766734a0049afe86b44.zip |
feat!: use user-supplied class name
-rw-r--r-- | hovercard.js | 76 |
1 files changed, 37 insertions, 39 deletions
diff --git a/hovercard.js b/hovercard.js index c8bd491..f9de005 100644 --- a/hovercard.js +++ b/hovercard.js @@ -1,18 +1,13 @@ +// 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 -var baseURL = document.domain // these will not attempt to get fetched, so can be used to override things var hovercards = { - curURL : {}, - baseURL: {} + curURL : {} }; -var article = document.getElementsByTagName("article"); - - if (article) { - article = article[0]; - } - function hover (target_link) { var hovercard_data = hovercards[target_link.href.replace(/\/+$/, "")]; if (hovercard_data) { @@ -41,35 +36,38 @@ var article = document.getElementsByTagName("article"); } } - var links = article.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]) { - 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(); + 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]) { + 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 |