Skip to content

Instantly share code, notes, and snippets.

@rlemon
Last active December 18, 2015 17:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlemon/5822576 to your computer and use it in GitHub Desktop.
Save rlemon/5822576 to your computer and use it in GitHub Desktop.
Script to stop auto play of gifs in the chat.
// ==UserScript==
// @name GIF Mask
// @author Robert Lemon
// @version 0.0.7.1
// @namespace
// @description Replaces gifs with a single frame canvas (click to watch, click to hide)
// @include http://chat.stackexchange.com/rooms/*
// @include http://chat.stackoverflow.com/rooms/*
// ==/UserScript==
function replaceGif() {
var gif = this;
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
width = canvas.width = (gif.width > 300 ? 300 : gif.width ),
height = canvas.height = (gif.height > 166 ? 166 : gif.height );
context.drawImage(gif, 0, 0);
context.font = "20px arial";
context.fillStyle = "white";
context.fillText("Click to play", width / 2 - 52, height / 2 + 8);
function tGif() {
gif.parentNode.insertBefore(canvas, gif);
gif.parentNode.removeChild(gif);
return false;
}
function tCanvas() {
canvas.parentNode.insertBefore(gif, canvas);
canvas.parentNode.removeChild(canvas);
return false;
}
canvas.onclick = tCanvas;
gif.onclick = tGif;
gif.setAttribute('data-replacegif', true);
tGif();
}
function scanChat() {
var images = chat.getElementsByTagName('img');
[].forEach.call(images, function(img) {
var sp = img.src.split('?')[0],
ext = sp.substr(-3);
if( (ext.toLowerCase() === 'gif') && (img.getAttribute('data-replacegif') !== true) ) {
img.onload = replaceGif.bind(img);
}
});
}
var chat = document.getElementById('chat');
chat.addEventListener('DOMSubtreeModified', scanChat, false);
scanChat();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment