Harden video_embed.js, store URL in data-src

This commit is contained in:
rubenwardy 2022-01-25 21:52:46 +00:00
parent ae88360e20
commit 8f2a87e5ed
1 changed files with 26 additions and 18 deletions

View File

@ -1,24 +1,32 @@
document.querySelectorAll(".video-embed").forEach(ele => { document.querySelectorAll(".video-embed").forEach(ele => {
const url = new URL(ele.getAttribute("href")); try {
const href = ele.getAttribute("href");
const url = new URL(href);
if (url.host == "www.youtube.com") { if (url.host == "www.youtube.com") {
ele.addEventListener("click", () => { ele.addEventListener("click", () => {
ele.parentNode.classList.add("d-block"); ele.parentNode.classList.add("d-block");
ele.classList.add("embed-responsive"); ele.classList.add("embed-responsive");
ele.classList.add("embed-responsive-16by9"); ele.classList.add("embed-responsive-16by9");
ele.innerHTML = ` ele.innerHTML = `
<iframe title="YouTube video player" frameborder="0" <iframe title="YouTube video player" frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen> allowfullscreen>
</iframe>`; </iframe>`;
const embedURL = new URL("https://www.youtube.com/"); const embedURL = new URL("https://www.youtube.com/");
embedURL.pathname = "/embed/" + url.searchParams.get("v"); embedURL.pathname = "/embed/" + url.searchParams.get("v");
embedURL.searchParams.set("autoplay", "1"); embedURL.searchParams.set("autoplay", "1");
const iframe = ele.children[0]; const iframe = ele.children[0];
iframe.setAttribute("src", embedURL); iframe.setAttribute("src", embedURL);
}); });
ele.removeAttribute("href");
ele.setAttribute("data-src", href);
ele.removeAttribute("href");
}
} catch (e) {
console.error(url);
return;
} }
}); });