XHamster - auto-select highest quality stream - TEST

auto-select highest quality stream from https://greasyfork.org/fr/scripts/546708-xhamster-mobile-html5-player/code

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         XHamster - auto-select highest quality stream - TEST
// @version      2.00
// @description  auto-select highest quality stream from https://greasyfork.org/fr/scripts/546708-xhamster-mobile-html5-player/code
// @icon         https://external-content.duckduckgo.com/ip3/fr.xhamster.com.ico
// @author       janvier57
// @namespace    https://greasyfork.org/fr/users/7434-janvier56?sort=updated
// @license MIT
// @match        *://xhamster.com/*
// @match        *://*.xhamster.com/*
// @grant        none
// @run-at       document-end

// ==/UserScript==

(function() {
    'use strict';

    function checkAndSelectHighestQuality() {
        const qualitySelector = document.querySelector('.quality.chooser-control');
        if (!qualitySelector) {
            setTimeout(checkAndSelectHighestQuality, 1000);
            return;
        }

        console.log('checkAndSelectHighestQuality');
        const qualityOptions = Array.from(qualitySelector.children);
        console.log('qualityOptions:', qualityOptions);
        if (qualityOptions.length === 0) {
            setTimeout(checkAndSelectHighestQuality, 1000);
            return;
        }

        const hdOptions = qualityOptions.filter(option => option.classList.contains('HD'));
        console.log('hdOptions:', hdOptions);
        const chosenOption = qualitySelector.querySelector('.chosen');
        console.log('chosenOption:', chosenOption);
        if (hdOptions.length > 0 && (!chosenOption || !chosenOption.classList.contains('HD'))) {
            hdOptions.sort((a, b) => parseInt(b.textContent.trim().replace('p', '')) - parseInt(a.textContent.trim().replace('p', '')));
            console.log('Selecting highest quality option:', hdOptions[0]);
            hdOptions[0].click();
        }
    }

    checkAndSelectHighestQuality();
})();