Video Time Saver (Firebase)

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

  1. // ==UserScript==
  2. // @name Video Time Saver (Firebase)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Lưu và khôi phục thời gian xem video từ Firebase
  6. // @author Bui Quoc Dung
  7. // @match *://*/*
  8. // @grant GM_xmlhttpRequest
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. // 🔹 Cấu hình Firebase
  16. const FIREBASE_URL = "PASTE YOUR FIREBASE LINK";
  17.  
  18. let video = null;
  19. let VIDEO_ID = "";
  20.  
  21. // 🔍 1. Hàm lấy ID video
  22. function getVideoID() {
  23. const url = new URL(window.location.href);
  24. if (url.hostname.includes("youtube.com")) {
  25. return "youtube_" + url.searchParams.get("v"); // ID video YouTube
  26. }
  27. return window.location.pathname.replace(/[^a-zA-Z0-9]/g, ""); // ID cho trang khác
  28. }
  29.  
  30. // 🔍 2. Tìm video (hỗ trợ cả video chuẩn & Plyr.js)
  31. function findVideo() {
  32. video = document.querySelector("video") || findPlyrVideo();
  33. if (video) {
  34. console.log("🎥 Video found:", video);
  35. loadVideoTime();
  36. video.addEventListener("timeupdate", saveVideoTime);
  37. } else {
  38. console.log("⏳ Chờ video tải...");
  39. setTimeout(findVideo, 1000);
  40. }
  41. }
  42.  
  43. // 🔍 3. Kiểm tra Plyr.js hoặc Video.js
  44. function findPlyrVideo() {
  45. if (typeof Plyr !== "undefined" && Plyr.instances.length > 0) {
  46. return Plyr.instances[0].elements.container.querySelector("video");
  47. }
  48. return null;
  49. }
  50.  
  51. // ⬇️ 4. Lấy thời gian video từ Firebase
  52. function loadVideoTime() {
  53. GM_xmlhttpRequest({
  54. method: "GET",
  55. url: FIREBASE_URL,
  56. onload: function (response) {
  57. const data = JSON.parse(response.responseText);
  58. if (data && data[VIDEO_ID]) {
  59. const savedTime = data[VIDEO_ID].time;
  60. console.log("⏩ Khôi phục video tại:", savedTime);
  61. video.currentTime = savedTime;
  62. }
  63. },
  64. });
  65. }
  66.  
  67. // ⬆️ 5. Lưu thời gian video lên Firebase mỗi 5 giây
  68. function saveVideoTime() {
  69. if (!video || video.paused || video.ended) return;
  70.  
  71. const time = Math.floor(video.currentTime);
  72. console.log("💾 Lưu thời gian:", time);
  73.  
  74. GM_xmlhttpRequest({
  75. method: "PATCH",
  76. url: FIREBASE_URL,
  77. headers: { "Content-Type": "application/json" },
  78. data: JSON.stringify({ [VIDEO_ID]: { time: time } }),
  79. });
  80. }
  81.  
  82. // 🔄 6. Theo dõi thay đổi URL trên YouTube (hỗ trợ SPA)
  83. function observeURLChanges() {
  84. let lastURL = window.location.href;
  85. setInterval(() => {
  86. if (window.location.href !== lastURL) {
  87. console.log("🔄 URL changed, reloading script...");
  88. lastURL = window.location.href;
  89. VIDEO_ID = getVideoID();
  90. findVideo();
  91. }
  92. }, 1000);
  93. }
  94.  
  95. // 🚀 Chạy script
  96. VIDEO_ID = getVideoID();
  97. findVideo();
  98. observeURLChanges(); // Theo dõi sự thay đổi URL trên YouTube
  99. })();