-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedditDefaultVideoPlayer.user.js
More file actions
78 lines (65 loc) · 2.8 KB
/
RedditDefaultVideoPlayer.user.js
File metadata and controls
78 lines (65 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// ==UserScript==
// @name Improved Reddit Video Player
// @namespace https://github.com/paradoxepoch
// @version 0.1
// @description Replaces the Reddit video player with the browser's default HTML5 player
// @author ParadoxEpoch
// @match https://www.reddit.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @grant none
// ==/UserScript==
// ! This userscript is a work in progress.
// ! * It only works on overlay Reddit posts (aka when clicking on a post from a subreddit, but not when visiting a post URL directly or refreshing on an overlay post).
// ! * It also currently only plays the 480p version of a video. If no 480p version is available, the video will fail to load.
// ! * Videos do not currently have sound.
(function () {
'use strict';
async function init() {
if (!location.pathname.includes('/comments/')) {
console.log('>>> Not a post page, ignoring...');
return;
}
console.log('>>> Post page detected, looking for video element...');
function injectVideo() {
$('#overlayScrollContainer [data-click-id="media"]').css({ 'background': 'transparent' })
$('#overlayScrollContainer [data-click-id="media"]').html(`
<video controls autoplay height="100%" width="100%" style="max-height: 512px; max-width: calc(100% - 50px);">
<source src="https://v.redd.it/${videoId}/DASH_480.mp4" type="video/mp4">
</video>
`)
}
const sleep = m => new Promise(r => setTimeout(r, m))
let videoId;
let isReady = false
while (!isReady) {
await sleep(250);
console.log('Looking for video element...');
isReady = !!$('#overlayScrollContainer [data-click-id="media"] video').length;
if (isReady) {
console.log('Found video element!');
videoId = $('#overlayScrollContainer [data-click-id="media"] video > source').attr('src').split('/')[3];
injectVideo();
}
}
}
let oldHref = document.location.href;
window.onload = function () {
const bodyList = document.querySelector("body")
const observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (oldHref != document.location.href) {
console.log('>>> State change detected');
oldHref = document.location.href;
init();
}
});
});
const config = {
childList: true,
subtree: true
};
observer.observe(bodyList, config);
};
init();
})();