YoutubePlayBack

Have you ever closed a YouTube video by accident, or have you gone to another one and when you come back the video starts from 0? With this extension it won't happen anymore

当前为 2024-02-15 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @license MIT
// @name         YoutubePlayBack
// @namespace    http://tampermonkey.net/
// @version      1.1.1
// @description  Have you ever closed a YouTube video by accident, or have you gone to another one and when you come back the video starts from 0? With this extension it won't happen anymore
// @author       Costin Alexandru Sandu
// @match        https://www.youtube.com/watch*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none
// ==/UserScript==

(function() {
   	'strict'

	var savedProgressAlreadySet = false

  	function executeFnInPageContext (fn) {
      const fnStringified = fn.toString()
    	return window.eval('('+fnStringified+')'+'()')
    }

  	function getVideoCurrentTime () {
      const currentTime = executeFnInPageContext(() => {
      	const player = document.querySelector('#movie_player')
				return player.getCurrentTime()
      })
      return currentTime
    }

  	function getVideoId () {
    	const id = executeFnInPageContext(() => {
      	const player = document.querySelector('#movie_player')
				return player.getVideoData().video_id
      })
      return id
    }

  	function playerExists () {
     	const exists = executeFnInPageContext(() => {
      	const player = document.querySelector('#movie_player')
				return Boolean(player)
      })
      return exists
    }

  	function setVideoProgress (progress) {
      window.eval('var progress ='+ progress)
     	executeFnInPageContext(() => {
      	const player = document.querySelector('#movie_player')
		player.seekTo(window.progress)
      })
      window.eval('delete progress')
    }

  	function saveVideoProgress() {
      const videoProgress = getVideoCurrentTime()
      const videoId = getVideoId()
      window.localStorage.setItem(videoId, videoProgress)
    }

  	function getSavedVideoProgress() {
      const videoId = getVideoId()
      const savedVideoProgress = window.localStorage.getItem(videoId)

      return savedVideoProgress
    }

  	function logError(message) {
     	console.log('[YoutubeRememberLastTime] '+message)
    }
    function setSavedProgress () {
        const savedProgress = getSavedVideoProgress();
        setVideoProgress(savedProgress)
        savedProgressAlreadySet = true
    }

    function isReadyToSetSavedProgress () {
        return !savedProgressAlreadySet && playerExists() && getSavedVideoProgress()
    }

  	function main() {
      if (isReadyToSetSavedProgress()) {
          setSavedProgress()
      }
      saveVideoProgress()
    }

  	function initialize() {
        if (isReadyToSetSavedProgress()) {
            setSavedProgress()
        }
    	setInterval(main, 1500)
    }

  	initialize()
})();