Skip to content

Instantly share code, notes, and snippets.

@IntermittentlyRupert
Last active November 17, 2017 20:26
Show Gist options
  • Save IntermittentlyRupert/b1f46fab2c545cdf099edb114d2216a9 to your computer and use it in GitHub Desktop.
Save IntermittentlyRupert/b1f46fab2c545cdf099edb114d2216a9 to your computer and use it in GitHub Desktop.
A very basic user script to inline images in Twitch chat.
// ==UserScript==
// @name TwitchInlineImages
// @namespace https://github.com/kaotik4266
// @version 0.1
// @description A very basic user script to inline images in Twitch chat.
// @author kaotik4266
// @include /^https?:\/\/(www\.)?twitch\.tv\/.*$/
// @grant none
// ==/UserScript==
(function() {
'use strict';
const imageSuffixes = ["gif", "jpg", "png", "jpeg"];
function isImageLink(link) {
const tokens = link.href.split(".");
const extension = tokens[tokens.length - 1].split("?")[0].split("#")[0];
return imageSuffixes.includes(extension);
}
function alreadyHasImageChild(link) {
return link.childNodes[0].tagName === "IMG";
}
function inlineTheImage(link) {
link.innerHTML = "<img src=\"" + link.href + "\" width=300 />";
}
function run() {
Array.from(document.querySelectorAll(".chat-line__message--link"))
.filter(link => isImageLink(link))
.filter(link => !alreadyHasImageChild(link))
.forEach(link => inlineTheImage(link));
setTimeout(run, 100);
}
run();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment