Focus always and replay if video is paused

To learn more faster and efficiently

当前为 2024-07-28 提交的版本,查看 最新版本

// ==UserScript==
// @name         Focus always and replay if video is paused
// @name:zh-TW   始終保持焦點並在視頻暫停時自動播放
// @namespace    http://tampermonkey.net/
// @version      0.3.4
// @description  To learn more faster and efficiently
// @description:zh-tw   此腳本旨在提高在線學習效率,透過自動保持視頻播放視窗的焦點並在視頻暫停時自動重播。適用於 `https://iedu.foxconn.com/*` 網站,能夠確保學習過程中視頻連續播放,無需手動干預,特別適合忙碌且希望提高學習效率的用戶。
// @author       pjiaquan
// @match        https://iedu.foxconn.com/public/user/*
// @match        https://iedu.foxconn.com/public/play/*
// @run-at       document-start
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license      MIT
// ==/UserScript==
(function() {
    'use strict';

    window.onblur = null;
    window.blurred = false;
    let playingVideoTitle = "";

    document.hasFocus = () => true;
    window.onFocus = () => true;

    [
        "hidden",
        "mozHidden",
        "msHidden",
        "webkitHidden"
    ].forEach(prop_name => {
        Object.defineProperty(document, prop_name, {value: false});
    });

    Object.defineProperty(document, "visibilityState", {get: () => "visible"});
    Object.defineProperty(document, "webkitVisibilityState", {get: () => "visible"});

    document.onvisibilitychange = undefined;

    var event_handler = (event) => {
        if (["blur", "mouseleave", "mouseout"].includes(event.type) &&
            (event.target instanceof HTMLInputElement ||
             event.target instanceof HTMLAnchorElement ||
             event.target instanceof HTMLSpanElement)) {
            return; // exclude input, anchor, and span elements
        }
        event.preventDefault();
        event.stopPropagation();
        event.stopImmediatePropagation();
    };

    [
        "visibilitychange",
        "webkitvisibilitychange",
        "blur",
        "hasFocus",
        "mouseleave",
        "mouseout",
        "mozvisibilitychange",
        "msvisibilitychange"
    ].forEach(event_name => {
        window.addEventListener(event_name, event_handler, true);
        document.addEventListener(event_name, event_handler, true);
    });

    let videoData = {
        filename: null,
        waresMap: new Map()
    };

    // script1 functionality
    function updateVideoProgress() {
        // console.log('Check is playing or not');
        if (window.wares && window.wares.length > 0) {
            // console.log("wares array found and not empty:", window.wares);
            setTimeout(() => {
                if (!isVideoPlaying()) {
                    console.log('start!');
                    startPlayingVideo();
                } else {
                    // stopPlayingVideo();
                }
            }, 5000);
        } else {
            console.log("wares array not found or is empty");
        }
    }

    function setupVideoCheck() {
        //console.log('Page loaded, running script');
        setInterval(updateVideoProgress, 5000);

        // Create and style the button
//         const button = document.createElement('button');
//         button.textContent = 'Change Start Time';
//         button.style.position = 'fixed';
//         button.style.right = '20px';
//         button.style.bottom = '20px';
//         button.style.padding = '10px 20px';
//         button.style.backgroundColor = '#007bff';
//         button.style.color = '#fff';
//         button.style.border = 'none';
//         button.style.borderRadius = '5px';
//         button.style.cursor = 'pointer';

//         // Add click event listener to the button
//         button.addEventListener('click', changeStartTime);

//         // Append the button to the body
//         document.body.appendChild(button);

//         // Select the active dd element
//         const activeElement = document.querySelector('dl .active');

//         // Check if the active element exists and log its title attribute or inner text
//         if (activeElement) {
//             // console.log('Active element found:', activeElement);
//             // console.log('Title:', activeElement.getAttribute('title'));
//             playingVideoTitle = activeElement.getAttribute('title');
//             // console.log('Content:', activeElement.innerText);
//         } else {
//             console.log('No active element found');
//         }
    }



    function isVideoPlaying() {
        var videoElement = document.getElementById('realvideo_html5_api');
        if (videoElement && videoElement.tagName === 'VIDEO') {
            // console.log(videoElement);

            // Extract the src attribute
            var videoSrc = videoElement.getAttribute("src");

            // Extract the filename from the src URL
            var videoFilename = videoSrc.split('/').pop().split('.')[0];

            // Save the filename to the videoData object
            videoData.filename = videoFilename;

            // Map the videoFilename with wares if needed
            if (window.wares && window.wares.length > 0) {
                // console.log('playing: ' + playingVideoTitle);
                // console.log(videoData.waresMap.entries());


                window.wares.forEach((ware, index) => {
                    if(ware.name === playingVideoTitle) {
                        // const fiveMinutesInMs = 900000; // 5 minutes in milliseconds
                        // console.log('#1', ware.starttime);
                        // const newStartTime = ware.starttime - fiveMinutesInMs;
                        // ware.starttime = newStartTime;
                        // console.log('#2', ware.starttime);
                        // console.log(`Start time set to: ${new Date(newStartTime).toString()}`);
                        // console.log(ware);
                    }
                    // console.log('First key:', ware);
                    // console.log('First value:', ware);
                    // console.log(firstValue.starttime);

                    videoData.waresMap.set(videoFilename, ware); // Example mapping
                });
            }

            // Output the filename or save it as needed
            if (videoData && videoData.waresMap) {
                const firstEntry = videoData.waresMap.entries().next().value;
                if (firstEntry) {
                    const [firstKey, firstValue] = firstEntry;

                    // const date = new Date(firstValue.starttime);
                    // console.log(date.toString());

                    if(firstValue.isComplete === "Y") {
                        return true;

                    }
                } else {
                    console.log('Map is empty');
                }
            }

            return !videoElement.paused;
        } else {
            console.log('Video element not found or is not a video tag');
            return null;
        }
    }

    function stopPlayingVideo() {
        var videoElement = document.getElementById('realvideo_html5_api');
        if (videoElement && videoElement.tagName === 'VIDEO') {
            videoElement.pause();
            console.log("Video playback stopped");
        } else {
            console.log('Video element not found or is not a video tag');
        }
    }

    function startPlayingVideo() {
        var videoElement = document.getElementById('realvideo_html5_api');
        if (videoElement && videoElement.tagName === 'VIDEO') {
            videoElement.play()
                .then(() => console.log("Video playback started"))
                .catch((error) => console.error("Error attempting to play video:", error));
        } else {
            console.log('Video element not found or is not a video tag');
        }
    }

    // Function to change the start time of the video
    function changeStartTime() {
        var videoElement = document.getElementById('realvideo_html5_api');
        if (videoElement && videoElement.tagName === 'VIDEO') {

            // const fiveMinutesInMs = 300000; // 5 minutes in milliseconds
            // const newStartTime = videoData.starttime - fiveMinutesInMs;

            console.log(videoElement);

            //             videoElement.starttime = newStartTime;
            //             console.log(`New start time (milliseconds): ${newStartTime}`);
            //             console.log(`Start time set to: ${new Date(newStartTime).toString()}`);

            //             videoElement.play()
            //                 .then(() => console.log("Video playback started 2"))
            //                 .catch((error) => console.error("Error attempting to play video: 2", error));
        } else {
            console.log('Video element not found or is not a video tag');
        }
    }

    window.addEventListener('load', setupVideoCheck);

})();