Compare commits

...
2 Commits
Author SHA1 Message Date
luxick ad2c20ea39 Also inline video comments 2026-08-28 14:00:47 +02:00
luxick ddd97c12aa Update URL 2026-08-28 13:57:38 +02:00
+66 -13
View File
@@ -1,11 +1,11 @@
// ==UserScript==
// @name Inline Reddit images and Comment Redirect
// @namespace https://www.reddit.com
// @version 2025-09-24
// @description Embed images posted as comments and redirect post links to comments
// @version 2026-08-28
// @description Embed images and videos posted as comments and redirect post links to comments
// @author luxick
// @updateURL https://git.luxick.de/luxick/scripts/raw/branch/master/reddit-image-inline.user.js
// @downloadURL https://git.luxick.de/luxick/scripts/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
// @match https://old.reddit.com/r/*
// @match https://www.reddit.com/r/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=reddit.com
@@ -15,18 +15,39 @@
(function() {
'use strict';
// Handle image inlining
let xpath = "//a[text()='<image>']";
var element;
var result = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
let links = [];
while(element = result.iterateNext()){
if (element.href) {
links.push(element);
// Collect the placeholder links Reddit renders for media comments,
// e.g. "<image>" or "<video>" as the link text.
function collectLinks(placeholder) {
let xpath = "//a[text()='" + placeholder + "']";
var element;
var result = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
let links = [];
while(element = result.iterateNext()){
if (element.href) {
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);
let image = new Image();
image.src = elem.href;
@@ -45,6 +66,38 @@
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
if (!window.location.pathname.includes('/comments/')) {
// Handle post link redirection