teldosas / Facebook - Video Resume

// ==UserScript==
// @description     When playing a Facebook video, this script stores the current time (minus ten seconds) so that when the page is refreshed (be-it after reopening the browser or manual refresh) or after it gets reset due to bad internet connection, the video resumes from where it stopped. Inspired by 'Youtube - Resume'
// @name            Facebook - Video Resume
// @namespace       https://greasyfork.org/en/users/396351-teldosas
// @include         https://www.facebook.com/*
// @version         1
// @grant    				GM.getValue
// @grant    				GM.setValue
// @grant    				GM.notification
// @license MIT
// @run-at document-idle
// ==/UserScript==

const {max, floor} = Math;

const interval = 10e3;
var intervalId;
const rewind = 10;

window.eval(`const _wr = (type) => {
    const orig = history[type];
    return function() {
        const rv = orig.apply(this, arguments);
        const e = new Event(type);
        e.arguments = arguments;
        window.dispatchEvent(e);
        return rv;
    };
};
history.pushState = _wr('pushState'), history.replaceState = _wr('replaceState');
`);
const isVideoPath = (pathname) => /\/.*\/videos\/.*/.test(pathname);
var pathname = window.location.pathname;

const getTimeKey = () => `${pathname}fb-video`;
const setTime = (time) => GM.setValue(getTimeKey(), time);
const getTime = () => GM.getValue(getTimeKey());


const initialize = () => {
  var video;
	const vidIntervalId = setInterval(() => {
	    video = Array.from(document.querySelectorAll('video')).filter(e => e.getBoundingClientRect().x === 0)[0]
        if (video) {
          clearInterval(vidIntervalId);
          
          getTime().then(time => {
            video.currentTime = time;
          });
    
    
          intervalId = setInterval(() => {
            const time = max(0, floor(video.currentTime) - rewind);
            setTime(time);
          }, interval);
    
    
    
          video.addEventListener('error', (e) => {
            clearInterval(intervalId);
            setTimeout(() => initialize(), 1e3);
          });
          
          video.addEventListener('ended', (e) => {
            setTime(0);
          });
        }
  }, 10e2);
}

if (isVideoPath(pathname)) {
	initialize();
}

     
const pathChanged = (e) => {
  clearInterval(intervalId);
  pathname = window.location.pathname;
  if (isVideoPath(pathname)) {
		initialize();
  }
}

window.addEventListener('pushState', pathChanged);
window.addEventListener('replaceState', pathChanged);
window.addEventListener('popstate', pathChanged);

window.addEventListener('beforeunload', (e) => clearInterval(intervalId));