Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name YouTube Easy Resume
- // @namespace http://tampermonkey.net/
- // @version 0.0.7
- // @description Automatyczne wznawianie filmów. Naprawiono błąd cofania czasu. Usunięto ikonę z playera.
- // @match http://youtube.com/*
- // @match https://www.youtube.com/*
- // @grant GM.getValue
- // @grant GM.setValue
- // @grant GM_registerMenuCommand
- // @run-at document-end
- // ==/UserScript==
- (async function() {
- 'use strict';
- let userSettings = {};
- let isResuming = false;
- let lastSavedTime = 0;
- let currentVideoId = null;
- // Funkcja pomocnicza DOM
- function el(tag, props, ...children) {
- const element = document.createElement(tag);
- if (props) {
- for (const key of Object.keys(props)) {
- if (key === 'style' && typeof props[key] === 'object') Object.assign(element.style, props[key]);
- else if (key.startsWith('on') && typeof props[key] === 'function') element.addEventListener(key.substring(2).toLowerCase(), props[key]);
- else if (key === 'textContent') element.textContent = props[key];
- else element[key] = props[key];
- }
- }
- for (const child of children) {
- if (child != null) element.appendChild(typeof child === 'string' ? document.createTextNode(child) : child);
- }
- return element;
- }
- class YouTubeAutoResume {
- constructor() {
- this.init();
- GM_registerMenuCommand("Watchlist & Ustawienia", () => this.showUI());
- }
- async init() {
- userSettings = await GM.getValue("settings") || {
- minWatchTime: 10,
- minVideoLength: 30,
- markPlayedTime: 20,
- deleteAfter: 30
- };
- // Obsługa SPA (przejścia między filmami)
- document.addEventListener('yt-navigate-finish', () => this.processVideo());
- // Na wypadek bezpośredniego wejścia z linku
- this.processVideo();
- }
- extractID(url) {
- try {
- const urlObj = new URL(url);
- return urlObj.searchParams.get("v");
- } catch(e) { return null; }
- }
- async processVideo() {
- const id = this.extractID(window.location.href);
- if (!id) return;
- currentVideoId = id;
- isResuming = false;
- const video = document.querySelector("video");
- const player = document.getElementById("movie_player");
- if (!video || !player || isNaN(video.duration) || video.duration === 0) {
- setTimeout(() => this.processVideo(), 500);
- return;
- }
- const videos = await GM.getValue("videos") || [];
- const stored = videos.find(v => this.extractID(v.videolink) === id);
- if (stored && !stored.complete && stored.time > userSettings.minWatchTime) {
- this.applySmartSeek(video, player, stored.time);
- }
- this.setupMonitor(video);
- }
- // Naprawa błędu cofania o 20 sekund
- applySmartSeek(video, player, targetTime) {
- isResuming = true;
- const performSeek = () => {
- if (typeof player.seekTo === 'function') {
- player.seekTo(targetTime, true);
- } else {
- video.currentTime = targetTime;
- }
- };
- performSeek();
- // SEEK GUARDIAN: Przez 3 sekundy walczymy z natywnym cofaniem YouTube
- let checks = 0;
- const guardian = setInterval(() => {
- // Jeśli YouTube cofnął film o więcej niż 2 sekundy względem celu
- if (video.currentTime < targetTime - 2) {
- performSeek();
- }
- checks++;
- if (checks > 30) { // 3 sekundy (30 * 100ms)
- clearInterval(guardian);
- isResuming = false;
- }
- }, 100);
- }
- async saveProgress(video) {
- if (isResuming || !video || video.currentTime < 2) return;
- const id = this.extractID(window.location.href);
- if (!id) return;
- const videos = await GM.getValue("videos") || [];
- const title = (document.querySelector("h1.ytd-watch-metadata, h1.title")?.textContent || "YouTube Video").trim();
- const videoObj = {
- videolink: window.location.href,
- time: video.currentTime,
- duration: video.duration,
- title: title,
- complete: (video.duration - video.currentTime) < userSettings.markPlayedTime,
- timestamp: Date.now()
- };
- const filtered = videos.filter(v => this.extractID(v.videolink) !== id);
- filtered.push(videoObj);
- // Zachowaj tylko ostatnie 100 filmów
- if (filtered.length > 100) filtered.shift();
- await GM.setValue("videos", filtered);
- }
- setupMonitor(video) {
- if (video.dataset.resumeActive) return;
- video.dataset.resumeActive = "true";
- video.addEventListener('timeupdate', () => {
- const now = Date.now();
- if (now - lastSavedTime < 5000) return; // Zapis co 5 sek
- lastSavedTime = now;
- this.saveProgress(video);
- });
- }
- async showUI() {
- const existing = document.getElementById('resume-ui-overlay');
- if (existing) { existing.remove(); return; }
- const videos = await GM.getValue("videos") || [];
- const overlay = el('div', {
- id: 'resume-ui-overlay',
- 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' }
- });
- const content = el('div', {
- style: { background: '#212121', color: 'white', padding: '25px', borderRadius: '12px', width: '500px', maxHeight: '80vh', overflowY: 'auto', border: '1px solid #333' }
- },
- el('div', { style: { display: 'flex', justifyContent: 'space-between', marginBottom: '20px' } },
- el('h2', { textContent: 'Historia Oglądania', style: { margin: '0' } }),
- el('button', { textContent: '✕', style: { background: 'none', border: 'none', color: 'white', fontSize: '20px', cursor: 'pointer' }, onClick: () => overlay.remove() })
- )
- );
- videos.slice().reverse().forEach(v => {
- const progress = Math.floor((v.time / v.duration) * 100);
- content.appendChild(el('div', { style: { padding: '10px', borderBottom: '1px solid #333', display: 'flex', flexDirection: 'column', gap: '5px' } },
- el('a', { href: v.videolink, textContent: v.title, style: { color: '#3ea6ff', textDecoration: 'none', fontWeight: '500' } }),
- el('div', { style: { fontSize: '12px', color: '#aaa' }, textContent: `Postęp: ${progress}% (${Math.floor(v.time)}s)` })
- ));
- });
- if (videos.length === 0) {
- content.appendChild(el('div', { textContent: 'Brak zapisanych filmów.', style: { textAlign: 'center', padding: '20px', color: '#888' } }));
- }
- overlay.appendChild(content);
- document.body.appendChild(overlay);
- }
- }
- new YouTubeAutoResume();
- })();
Advertisement