Hide Watched Videos on PH

Hides watched videos on xHamster and adds a toggle button to turn this feature on/off. Saves state to cookies.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Hide Watched Videos on PH
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Hides watched videos on xHamster and adds a toggle button to turn this feature on/off. Saves state to cookies.
// @author       Your Name
// @match        *://*.pornhub.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Create floating toggle button
    const button = document.createElement('button');
    button.textContent = 'Watched On';
    button.style.position = 'fixed';
    button.style.top = '60px';
    button.style.right = '10px';
    button.style.zIndex = '9999';
    button.style.padding = '10px 15px';
    button.style.backgroundColor = '#FF5722';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
    button.style.fontSize = '14px';
    document.body.appendChild(button);

    // Helper functions for working with cookies
    function setCookie(name, value, days) {
        const expires = new Date();
        expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
        document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`;
    }

    function getCookie(name) {
        const cookies = document.cookie.split('; ');
        for (const cookie of cookies) {
            const [key, value] = cookie.split('=');
            if (key === name) return value;
        }
        return null;
    }

    // Initialize state from cookies or default
    let hideWatched = getCookie('hideWatched') === 'true';

    // Update button text based on state
    function updateButtonText() {
        button.textContent = hideWatched ? 'Watched On' : 'Watched Off';
    }

    // Function to hide/show watched videos
    function toggleWatchedVideos() {
        const videoCards = document.querySelectorAll('li.pcVideoListItem.js-pop.videoblock');
        videoCards.forEach((card) => {
            const watchedBadge = card.querySelector('div.bgShadeEffect.hideOnPreview.watchedVideo');
            if (watchedBadge) {
                card.style.display = hideWatched ? '' : 'none';
            }
        });
    }

    // Add click event to the button
    button.addEventListener('click', () => {
        hideWatched = !hideWatched;
        setCookie('hideWatched', hideWatched, 30); // Save state for 30 days
        updateButtonText();
        toggleWatchedVideos();
    });

    // Initial setup
    updateButtonText();
    toggleWatchedVideos();

    // Observe changes to dynamically loaded content
    const observer = new MutationObserver(() => {
        toggleWatchedVideos();
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();