website/js/customElements/image.js

34 lines
1.2 KiB
JavaScript
Raw Normal View History

class CustomImage extends HTMLElement {
async connectedCallback(){
const originalURL = new URL(this.getAttribute("src"), document.baseURI).href;
var graphql = JSON.stringify({
2022-05-01 12:56:43 +02:00
query: "query($url: String!) {imgproxy(url: $url)}",
variables: {
"url": originalURL
}
})
var requestOptions = {
method: 'POST',
body: graphql,
headers: { 'Content-Type': 'application/json' }
};
let imgproxy = (await (await fetch("/API/graphql.php", requestOptions)).json()).data.imgproxy;
let image = document.createElement("img");
image.src = imgproxy;
image.setAttribute("alt", this.getAttribute("alt"));
image.setAttribute("title", this.getAttribute("title"));
image.setAttribute("class", this.getAttribute("class"));
image.setAttribute("style", this.getAttribute("style"));
image.setAttribute("width", this.getAttribute("width"));
image.setAttribute("height", this.getAttribute("height"));
image.setAttribute("loading", "lazy");
image.setAttribute("original-src", originalURL);
this.appendChild(image);
}
}
customElements.define("jl-img", CustomImage)