Xhamster - AUTO Video Infinite LOOP v.10 Fix by smartacephale

Video Auto Infinite Replay - Fix by smartacephale (https://greasyfork.org/fr/scripts/493935-xhamster-improved)

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name            Xhamster - AUTO Video Infinite LOOP v.10 Fix by smartacephale

// @version         v.10.00
// @description	    Video Auto Infinite Replay - Fix by smartacephale (https://greasyfork.org/fr/scripts/493935-xhamster-improved)
// @icon            https://external-content.duckduckgo.com/ip3/fr.xhamster.com.ico
//
// @namespace       https://greasyfork.org/fr/users/7434-janvier56
// @homepage        https://greasyfork.org/fr/users/7434-janvier56

// @match           https://*.xhamster.com/videos/*
// @grant           none
// ==/UserScript==

function waitForElementExists(parent, selector, callback) {
	const observer = new MutationObserver((_mutations) => {
		const el = parent.querySelector(selector);
		if (el) {
			observer.disconnect();
			callback(el);
		}
	});
	observer.observe(document.body, { childList: true, subtree: true });
}

waitForElementExists(document, "video", forceInfinitePlay);

function forceInfinitePlay() {
	let video = document.querySelector("video");
	// if (!video) {
	// 	video = document
	// 		.querySelector("iframe")
	// 		.contentDocument.querySelector("video");
	// }
	console.log("Video element:", video);

	// const overlayMenu = document.querySelector(
	// 	"html:not(.tablet) .xplayer.overlay-menu-opened .xp-aftershot",
	// );
	// console.log("Overlay menu:", overlayMenu);
	// if (overlayMenu) {
	// 	overlayMenu.remove();
	// }

	// Add an event listener to loop the video
	video.addEventListener("ended", () => {
		console.log("Video ended, looping...");
		video.currentTime = 0;
		video.play();
	});
}