MediaWiki:YouTube.js: Difference between revisions

From VRChat Wiki
(Created page with "// This could be replaced with https://www.mediawiki.org/wiki/Extension:YouTube, but it // was easier to just implement it myself instead of asking & waiting for it to be added. [...document.querySelectorAll(".youtube")].map((element) => { const { videoId, ...attributes } = element.dataset; const iframe = document.createElement("iframe"); iframe.setAttribute("width", 560); iframe.setAttribute("height", 315); iframe.setAttribute("frameborder", 0); // As suggested...")
 
No edit summary
 
Line 31: Line 31:
);
);


iframe.src = `https://www.youtube-nocookie.com/embed/${videoId}`;
iframe.src = `https://www.youtube-nocookie.com/embed/${videoId}?rel=0`;


element.replaceWith(iframe);
element.replaceWith(iframe);
});
});

Latest revision as of 17:51, 11 October 2024

// This could be replaced with https://www.mediawiki.org/wiki/Extension:YouTube, but it
// was easier to just implement it myself instead of asking & waiting for it to be added.

[...document.querySelectorAll(".youtube")].map((element) => {
	const { videoId, ...attributes } = element.dataset;

	const iframe = document.createElement("iframe");

	iframe.setAttribute("width", 560);
	iframe.setAttribute("height", 315);
	iframe.setAttribute("frameborder", 0);
	// As suggested by YouTube's embed copy.
	iframe.setAttribute(
		"allow",
		[
			"accelerometer",
			"autoplay",
			"clipboard-write",
			"encrypted-media",
			"gyroscope",
			"picture-in-picture",
			"web-share"
		].join("; ")
	);
	iframe.setAttribute("referrerpolicy", "strict-origin-when-cross-origin");
	iframe.setAttribute("allowfullscreen", true);

	// Set the attributes from the data-* attributes, overwriting the default ones.
	Object.entries(attributes).map(([key, value]) =>
		iframe.setAttribute(key, value)
	);

	iframe.src = `https://www.youtube-nocookie.com/embed/${videoId}?rel=0`;

	element.replaceWith(iframe);
});