Also inline video comments

This commit is contained in:
2026-08-28 14:00:47 +02:00
parent ddd97c12aa
commit ad2c20ea39
+58 -5
View File
@@ -1,8 +1,8 @@
// ==UserScript== // ==UserScript==
// @name Inline Reddit images and Comment Redirect // @name Inline Reddit images and Comment Redirect
// @namespace https://www.reddit.com // @namespace https://www.reddit.com
// @version 2025-09-24 // @version 2026-08-28
// @description Embed images posted as comments and redirect post links to comments // @description Embed images and videos posted as comments and redirect post links to comments
// @author luxick // @author luxick
// @updateURL https://git.luxick.de/luxick/scripts/js/raw/branch/master/reddit-image-inline.user.js // @updateURL https://git.luxick.de/luxick/scripts/js/raw/branch/master/reddit-image-inline.user.js
// @downloadURL https://git.luxick.de/luxick/scripts/js/raw/branch/master/reddit-image-inline.user.js // @downloadURL https://git.luxick.de/luxick/scripts/js/raw/branch/master/reddit-image-inline.user.js
@@ -15,8 +15,10 @@
(function() { (function() {
'use strict'; 'use strict';
// Handle image inlining // Collect the placeholder links Reddit renders for media comments,
let xpath = "//a[text()='<image>']"; // e.g. "<image>" or "<video>" as the link text.
function collectLinks(placeholder) {
let xpath = "//a[text()='" + placeholder + "']";
var element; var element;
var result = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); var result = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
let links = []; let links = [];
@@ -25,8 +27,27 @@
links.push(element); links.push(element);
} }
} }
return links;
}
for (var elem of links) { // Reddit's video comments link to an embedded player page rather than to a
// playable file, e.g. ".../video/<id>/player".
function isPlayerUrl(href) {
return /\/player\/?(\?|$)/i.test(href);
}
function buildPlayerFrame(href) {
let frame = document.createElement('iframe');
frame.src = href;
frame.width = 300;
frame.height = 300;
frame.allowFullscreen = true;
frame.style = "max-width: 300px; border: none";
return frame;
}
// Handle image inlining
for (var elem of collectLinks('<image>')) {
console.log("inlining image " + elem.href); console.log("inlining image " + elem.href);
let image = new Image(); let image = new Image();
image.src = elem.href; image.src = elem.href;
@@ -45,6 +66,38 @@
elem.remove(); elem.remove();
} }
// Handle video inlining
for (var videoElem of collectLinks('<video>')) {
console.log("inlining video " + videoElem.href);
let href = videoElem.href;
let embed;
if (isPlayerUrl(href)) {
embed = buildPlayerFrame(href);
} else {
let video = document.createElement('video');
video.src = href;
video.controls = true;
video.preload = 'metadata';
video.style = "max-width: 300px";
// Some links (v.redd.it DASH streams) a bare <video> cannot play;
// put the original link back so the video stays reachable.
video.addEventListener('error', function() {
let anchor = document.createElement('a');
anchor.href = href;
anchor.target = '_blank';
anchor.textContent = '<video>';
video.replaceWith(anchor);
});
embed = video;
}
videoElem.parentElement.appendChild(embed);
videoElem.remove();
}
// Only proceed with link rewriting if we're not already in a comments section // Only proceed with link rewriting if we're not already in a comments section
if (!window.location.pathname.includes('/comments/')) { if (!window.location.pathname.includes('/comments/')) {
// Handle post link redirection // Handle post link redirection