NYT Floating Audio Player

Makes the audio player on NYT articles float on scroll in the bottom center.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         NYT Floating Audio Player
// @namespace    http://tampermonkey.net/
// @license MIT
// @version      1.4
// @description  Makes the audio player on NYT articles float on scroll in the bottom center.
// @author       Bao
// @match        https://www.nytimes.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=nytimes.com
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // This is the CSS that will be applied to the audio player when it floats.
    // We use GM_addStyle to inject it into the page.
    GM_addStyle(`
        .floating-audio-player {
            position: fixed !important;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%); /* This horizontally centers the element */
            width: 600px;
            max-width: 90vw; /* Ensures it fits on smaller screens */
            background-color: white;
            z-index: 10000; /* A high z-index to ensure it's on top */
            padding: 15px;
            border-radius: 8px; /* Added rounded corners */
            border: 1px solid #e2e2e2;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); /* Enhanced shadow for better visibility */
            transition: all 0.3s ease-in-out; /* Smooth transition */
        }
    `);

    // We store the original position of the player here.
    let playerOriginalOffsetTop = 0;

    // This selector targets the specific div that contains the audio player controls
    // and the share buttons, ensuring the entire bar floats.
    const audioPlayer = document.querySelector('.css-qznc1j');

    // If there's no audio player on the page, we don't need to do anything else.
    if (!audioPlayer) {
        console.log("NYT Floating Audio Player: No audio player found on this page.");
        return;
    }

    // A small delay to ensure the page has fully loaded and we get the correct original position.
    setTimeout(() => {
        if (audioPlayer) {
            playerOriginalOffsetTop = audioPlayer.offsetTop;
        }
    }, 500);


    // This function runs every time the user scrolls.
    function handleScroll() {
        // If we haven't found the player or its position yet, do nothing.
        if (!audioPlayer || playerOriginalOffsetTop === 0) {
            return;
        }

        // Check if the user's scroll position is past the player's original position.
        if (window.scrollY > playerOriginalOffsetTop) {
            // If so, add the floating class.
            audioPlayer.classList.add('floating-audio-player');
        } else {
            // Otherwise, remove it.
            audioPlayer.classList.remove('floating-audio-player');
        }
    }

    // Listen for the 'scroll' event on the window.
    window.addEventListener('scroll', handleScroll);

})();