Autoplay Reddit

autoplays videos on reddit

Stan na 03-09-2016. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Autoplay Reddit
// @version      1.0
// @description  autoplays videos on reddit
// @author       abbott
// @match        *://*.reddit.com/*
// @namespace https://greasyfork.org/users/59102
// ==/UserScript==

var idx = 0; // video idx
var youtube = '.*(youtu.be\/|v\/|e\/|u\/\w+\/|embed\/|v=)([^#\&\?]*).*'; // youtube video id regex
var player;

window.onLoad = function() {
   // youtube api
   var tag = document.createElement('script');
   tag.src = "https://www.youtube.com/iframe_api";
   var firstScriptTag = document.getElementsByTagName('script')[0];
   firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 
}

function onYouTubeIframeAPIReady() {
  if (document.getElementsByClassName('expando-button collapsed video').length <= idx) { // loads next page when done
    document.getElementsByClassName('next-button')[0].getElementsByTagName('a')[0].click();
  }
  var match = document.getElementsByClassName('title may-blank outbound ')[idx].href.match(youtube)[2]; // gets youtube video id

  document.getElementsByClassName('expando-button collapsed video')[idx].click(); // display video
  window.location.hash = document.getElementsByClassName('expando-button video expanded')[0].parentNode.parentNode.id; // move window to video location
  document.getElementsByTagName('iframe')[2].src = 'https://www.youtube.com/embed/' + match + '?enablejsapi=1'; // replaces reddit media iframe
  document.getElementsByTagName('iframe')[2].enablejsapi = "1";

  player = new YT.Player(document.getElementsByTagName('iframe')[2].id, { // attach api to video, should start at index 2
    events: {
      'onReady': onPlayerReady, // autoplay
      'onStateChange': onPlayerStateChange // on end play next video
    }
  });

  idx++;
}

function onPlayerReady() {
  player.playVideo(); // autoplay video
}

function onPlayerStateChange(event) {
  if(event.data === 0) { // when the video ends          
    document.getElementsByClassName('expando-button video expanded')[0].click(); // close video
    onYouTubeIframeAPIReady();
  }
}

document.onkeydown = function(e) { // skips current video if n is pressed
  if (e.keyCode == 78) { // n
    document.getElementsByClassName('expando-button video expanded')[0].click(); // close video
    onYouTubeIframeAPIReady();
  }
};