Youtube - Resume

Automatically start videos from where it stopped.

目前為 2023-11-04 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Youtube - Resume
// @namespace    https://greasyfork.org/en/users/670188-hacker09?sort=daily_installs
// @version      1
// @description  Automatically start videos from where it stopped.
// @author       hacker09
// @match        https://www.youtube.com/watch?v=*
// @icon         https://www.youtube.com/s/desktop/03f86491/img/favicon.ico
// @run-at       document-end
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_listValues
// @grant        GM_deleteValue
// ==/UserScript==

(function() {
  'use strict';
  document.querySelector('video').addEventListener('timeupdate', function() { //When the video is playing
    if (this.currentTime >= 5) { //If 5 or more secs have passed
      GM_setValue(document.querySelector('[video-id]').getAttribute('video-id'),{ "Last_Watched": new Date().getTime(), "StoppedAt": parseInt(this.currentTime)}); //Save the watched time
    } //Finishes the if condition
  }); //Finishes the Resume function

  GM_listValues().forEach(function(VidIDs) { //ForEach saved watched video time
    if ((GM_getValue(VidIDs, {}).Last_Watched) < (new Date().getTime() - 30 * 24 * 60 * 60 * 1000)) { //If 30+ days passed since the video was watched
      GM_deleteValue(VidIDs); //Delete the old saved watched video time
    }}); //Finishes the ForEach loop

  window.addEventListener('popstate', function() { document.querySelector('video').currentTime = GM_getValue(document.querySelector("meta[itemprop='identifier']").content, {}).StoppedAt; }); //Auto Resume if the video url changes
  document.querySelector('video').currentTime = GM_getValue(document.querySelector("meta[itemprop='identifier']").content, {}).StoppedAt; //Auto Resume the video onload
})();