SheridanCoriana6

YouTube Easy Resume

May 2nd, 2026
118
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. // ==UserScript==
  2. // @name YouTube Easy Resume
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.7
  5. // @description Automatyczne wznawianie filmów. Naprawiono błąd cofania czasu. Usunięto ikonę z playera.
  6. // @match http://youtube.com/*
  7. // @match https://www.youtube.com/*
  8. // @grant GM.getValue
  9. // @grant GM.setValue
  10. // @grant GM_registerMenuCommand
  11. // @run-at document-end
  12. // ==/UserScript==
  13.  
  14. (async function() {
  15. 'use strict';
  16.  
  17. let userSettings = {};
  18. let isResuming = false;
  19. let lastSavedTime = 0;
  20. let currentVideoId = null;
  21.  
  22. // Funkcja pomocnicza DOM
  23. function el(tag, props, ...children) {
  24. const element = document.createElement(tag);
  25. if (props) {
  26. for (const key of Object.keys(props)) {
  27. if (key === 'style' && typeof props[key] === 'object') Object.assign(element.style, props[key]);
  28. else if (key.startsWith('on') && typeof props[key] === 'function') element.addEventListener(key.substring(2).toLowerCase(), props[key]);
  29. else if (key === 'textContent') element.textContent = props[key];
  30. else element[key] = props[key];
  31. }
  32. }
  33. for (const child of children) {
  34. if (child != null) element.appendChild(typeof child === 'string' ? document.createTextNode(child) : child);
  35. }
  36. return element;
  37. }
  38.  
  39. class YouTubeAutoResume {
  40. constructor() {
  41. this.init();
  42. GM_registerMenuCommand("Watchlist & Ustawienia", () => this.showUI());
  43. }
  44.  
  45. async init() {
  46. userSettings = await GM.getValue("settings") || {
  47. minWatchTime: 10,
  48. minVideoLength: 30,
  49. markPlayedTime: 20,
  50. deleteAfter: 30
  51. };
  52.  
  53. // Obsługa SPA (przejścia między filmami)
  54. document.addEventListener('yt-navigate-finish', () => this.processVideo());
  55. // Na wypadek bezpośredniego wejścia z linku
  56. this.processVideo();
  57. }
  58.  
  59. extractID(url) {
  60. try {
  61. const urlObj = new URL(url);
  62. return urlObj.searchParams.get("v");
  63. } catch(e) { return null; }
  64. }
  65.  
  66. async processVideo() {
  67. const id = this.extractID(window.location.href);
  68. if (!id) return;
  69.  
  70. currentVideoId = id;
  71. isResuming = false;
  72.  
  73. const video = document.querySelector("video");
  74. const player = document.getElementById("movie_player");
  75.  
  76. if (!video || !player || isNaN(video.duration) || video.duration === 0) {
  77. setTimeout(() => this.processVideo(), 500);
  78. return;
  79. }
  80.  
  81. const videos = await GM.getValue("videos") || [];
  82. const stored = videos.find(v => this.extractID(v.videolink) === id);
  83.  
  84. if (stored && !stored.complete && stored.time > userSettings.minWatchTime) {
  85. this.applySmartSeek(video, player, stored.time);
  86. }
  87.  
  88. this.setupMonitor(video);
  89. }
  90.  
  91. // Naprawa błędu cofania o 20 sekund
  92. applySmartSeek(video, player, targetTime) {
  93. isResuming = true;
  94.  
  95. const performSeek = () => {
  96. if (typeof player.seekTo === 'function') {
  97. player.seekTo(targetTime, true);
  98. } else {
  99. video.currentTime = targetTime;
  100. }
  101. };
  102.  
  103. performSeek();
  104.  
  105. // SEEK GUARDIAN: Przez 3 sekundy walczymy z natywnym cofaniem YouTube
  106. let checks = 0;
  107. const guardian = setInterval(() => {
  108. // Jeśli YouTube cofnął film o więcej niż 2 sekundy względem celu
  109. if (video.currentTime < targetTime - 2) {
  110. performSeek();
  111. }
  112.  
  113. checks++;
  114. if (checks > 30) { // 3 sekundy (30 * 100ms)
  115. clearInterval(guardian);
  116. isResuming = false;
  117. }
  118. }, 100);
  119. }
  120.  
  121. async saveProgress(video) {
  122. if (isResuming || !video || video.currentTime < 2) return;
  123.  
  124. const id = this.extractID(window.location.href);
  125. if (!id) return;
  126.  
  127. const videos = await GM.getValue("videos") || [];
  128. const title = (document.querySelector("h1.ytd-watch-metadata, h1.title")?.textContent || "YouTube Video").trim();
  129.  
  130. const videoObj = {
  131. videolink: window.location.href,
  132. time: video.currentTime,
  133. duration: video.duration,
  134. title: title,
  135. complete: (video.duration - video.currentTime) < userSettings.markPlayedTime,
  136. timestamp: Date.now()
  137. };
  138.  
  139. const filtered = videos.filter(v => this.extractID(v.videolink) !== id);
  140. filtered.push(videoObj);
  141.  
  142. // Zachowaj tylko ostatnie 100 filmów
  143. if (filtered.length > 100) filtered.shift();
  144. await GM.setValue("videos", filtered);
  145. }
  146.  
  147. setupMonitor(video) {
  148. if (video.dataset.resumeActive) return;
  149. video.dataset.resumeActive = "true";
  150.  
  151. video.addEventListener('timeupdate', () => {
  152. const now = Date.now();
  153. if (now - lastSavedTime < 5000) return; // Zapis co 5 sek
  154.  
  155. lastSavedTime = now;
  156. this.saveProgress(video);
  157. });
  158. }
  159.  
  160. async showUI() {
  161. const existing = document.getElementById('resume-ui-overlay');
  162. if (existing) { existing.remove(); return; }
  163.  
  164. const videos = await GM.getValue("videos") || [];
  165.  
  166. const overlay = el('div', {
  167. id: 'resume-ui-overlay',
  168. style: { position: 'fixed', top: '0', left: '0', width: '100%', height: '100%', backgroundColor: 'rgba(0,0,0,0.9)', zIndex: '10000', display: 'flex', justifyContent: 'center', alignItems: 'center', fontFamily: 'Roboto, Arial' }
  169. });
  170.  
  171. const content = el('div', {
  172. style: { background: '#212121', color: 'white', padding: '25px', borderRadius: '12px', width: '500px', maxHeight: '80vh', overflowY: 'auto', border: '1px solid #333' }
  173. },
  174. el('div', { style: { display: 'flex', justifyContent: 'space-between', marginBottom: '20px' } },
  175. el('h2', { textContent: 'Historia Oglądania', style: { margin: '0' } }),
  176. el('button', { textContent: '✕', style: { background: 'none', border: 'none', color: 'white', fontSize: '20px', cursor: 'pointer' }, onClick: () => overlay.remove() })
  177. )
  178. );
  179.  
  180. videos.slice().reverse().forEach(v => {
  181. const progress = Math.floor((v.time / v.duration) * 100);
  182. content.appendChild(el('div', { style: { padding: '10px', borderBottom: '1px solid #333', display: 'flex', flexDirection: 'column', gap: '5px' } },
  183. el('a', { href: v.videolink, textContent: v.title, style: { color: '#3ea6ff', textDecoration: 'none', fontWeight: '500' } }),
  184. el('div', { style: { fontSize: '12px', color: '#aaa' }, textContent: `Postęp: ${progress}% (${Math.floor(v.time)}s)` })
  185. ));
  186. });
  187.  
  188. if (videos.length === 0) {
  189. content.appendChild(el('div', { textContent: 'Brak zapisanych filmów.', style: { textAlign: 'center', padding: '20px', color: '#888' } }));
  190. }
  191.  
  192. overlay.appendChild(content);
  193. document.body.appendChild(overlay);
  194. }
  195. }
  196.  
  197. new YouTubeAutoResume();
  198. })();
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment