Video Time Saver (Firebase)

Lưu và khôi phục thời gian xem video từ Firebase

目前為 2025-03-08 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Video Time Saver (Firebase)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Lưu và khôi phục thời gian xem video từ Firebase
// @author       Bui Quoc Dung
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @run-at       document-end
// ==/UserScript==

(function () {
    "use strict";

    // 🔹 Cấu hình Firebase
      const FIREBASE_URL = "PASTE YOUR FIREBASE LINK";

    let video = null;
    let VIDEO_ID = "";

    // 🔍 1. Hàm lấy ID video
    function getVideoID() {
        const url = new URL(window.location.href);
        if (url.hostname.includes("youtube.com")) {
            return "youtube_" + url.searchParams.get("v"); // ID video YouTube
        }
        return window.location.pathname.replace(/[^a-zA-Z0-9]/g, ""); // ID cho trang khác
    }

    // 🔍 2. Tìm video (hỗ trợ cả video chuẩn & Plyr.js)
    function findVideo() {
        video = document.querySelector("video") || findPlyrVideo();
        if (video) {
            console.log("🎥 Video found:", video);
            loadVideoTime();
            video.addEventListener("timeupdate", saveVideoTime);
        } else {
            console.log("⏳ Chờ video tải...");
            setTimeout(findVideo, 1000);
        }
    }

    // 🔍 3. Kiểm tra Plyr.js hoặc Video.js
    function findPlyrVideo() {
        if (typeof Plyr !== "undefined" && Plyr.instances.length > 0) {
            return Plyr.instances[0].elements.container.querySelector("video");
        }
        return null;
    }

    // ⬇️ 4. Lấy thời gian video từ Firebase
    function loadVideoTime() {
        GM_xmlhttpRequest({
            method: "GET",
            url: FIREBASE_URL,
            onload: function (response) {
                const data = JSON.parse(response.responseText);
                if (data && data[VIDEO_ID]) {
                    const savedTime = data[VIDEO_ID].time;
                    console.log("⏩ Khôi phục video tại:", savedTime);
                    video.currentTime = savedTime;
                }
            },
        });
    }

    // ⬆️ 5. Lưu thời gian video lên Firebase mỗi 5 giây
    function saveVideoTime() {
        if (!video || video.paused || video.ended) return;

        const time = Math.floor(video.currentTime);
        console.log("💾 Lưu thời gian:", time);

        GM_xmlhttpRequest({
            method: "PATCH",
            url: FIREBASE_URL,
            headers: { "Content-Type": "application/json" },
            data: JSON.stringify({ [VIDEO_ID]: { time: time } }),
        });
    }

    // 🔄 6. Theo dõi thay đổi URL trên YouTube (hỗ trợ SPA)
    function observeURLChanges() {
        let lastURL = window.location.href;
        setInterval(() => {
            if (window.location.href !== lastURL) {
                console.log("🔄 URL changed, reloading script...");
                lastURL = window.location.href;
                VIDEO_ID = getVideoID();
                findVideo();
            }
        }, 1000);
    }

    // 🚀 Chạy script
    VIDEO_ID = getVideoID();
    findVideo();
    observeURLChanges(); // Theo dõi sự thay đổi URL trên YouTube
})();