YouTube Continue

Resume YouTube videos where you left off.

目前為 2018-03-13 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Continue
// @namespace    https://greasyfork.org/en/users/68133-nevertheless
// @version      1.05
// @description  Resume YouTube videos where you left off.
// @author       Nevertheless
// @license      MIT
// @match        *://www.youtube.com/*
// @exclude      *://www.youtube.com/tv*
// @exclude      *://www.youtube.com/live_chat*
// @run-at       document-idle
// @grant        GM_getValue
// @grant        GM_setValue
// @noframes
// ==/UserScript==

var doc = document;
var win = window;
var old_addr = win.location.href;
var player = null;
var video_id = null;
var video_duration = null;
var initialResumeDone = false;

if (win.frameElement) throw new Error("Stopped JavaScript.");


function getPlayer() {
    player = doc.getElementById("movie_player");
    if (typeof player === undefined || player === null) {
        console.warn("[Youtube Resume] Player not defined.");
        return;
    } else {
        video_id = player.getVideoData().video_id;
        video_duration = player.getDuration();
        if (old_addr != win.location.href) {
            initialResumeDone = false;
            old_addr = win.location.href;
        }

        if (!initialResumeDone)
            resumeVideo();
        else
            saveProgress();
    }
}

function getSavedProgress() {
    return GM_getValue("p_" + video_id, 0);
}

function resumeVideo() {
    var resume_to = getSavedProgress();
    var should_resume = true;
    video_duration = player.getDuration();

    if (video_duration > 0) {
        should_resume = (video_duration - resume_to) >= video_duration * 0.05;
    }

    // if (resume_to > 10) {
    //     resume_to -= 5;
    // }

    if (should_resume) {
        player.seekTo(resume_to, true);
    }

    var currentTime = player.getCurrentTime();

    if (currentTime >= resume_to || resume_to <= 0) {
        initialResumeDone = true;
    }
}

function saveProgress() {
    GM_setValue("p_" + video_id, player.getCurrentTime());
}


win.setInterval(getPlayer, 3000);
getPlayer();