Created
March 31, 2016 18:08
-
-
Save shu8/3835fea17bf70c7f8c9f8063300c11f1 to your computer and use it in GitHub Desktop.
A userscript that stops auto-playing GIFs in chat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Stop GIFs auto-playing | |
// @namespace http://stackexchange.com/users/4337810/ | |
// @version 1.0 | |
// @description A userscript that stops auto-playing GIFs in chat | |
// @author ᔕᖺᘎᕊ (http://stackexchange.com/users/4337810/) | |
// @match *://chat.stackoverfow.com/* | |
// @match *://chat.meta.stackexchange.com/* | |
// @match *://chat.stackexchange.com/* | |
// @require https://rawgit.com/krasimir/gifffer/master/build/gifffer.min.js | |
// @grant none | |
// ==/UserScript== | |
function replaceSrcAttributesAndStartGifffer() { | |
//Thanks to Krasimir's gifffer library: https://github.com/krasimir/gifffer, http://stackoverflow.com/a/24314098/ :) | |
setTimeout(function() { | |
$('img[src*=".gif"]').each(function() { | |
src = $(this).attr('src'); | |
$(this).removeAttr('src').attr('data-gifffer', src); | |
$(this).parents('a').click(function(e) { | |
e.preventDefault(); | |
}); | |
}); | |
Gifffer(); | |
}, 1000); | |
}; | |
var observer = new MutationObserver(function (mutations) { //for NEW messages | |
mutations.forEach(function (mutation) { | |
var length = mutation.addedNodes.length; | |
for (var i = 0; i < length; i++) { | |
var $addedNode = $(mutation.addedNodes[i]); | |
if (!$addedNode.hasClass('message')) { | |
return; | |
} | |
var $lastanchor = $addedNode.find('img').last(); | |
if (!$lastanchor) { | |
return; | |
} | |
replaceSrcAttributesAndStartGifffer(); | |
} | |
}); | |
}); | |
$(function() { | |
replaceSrcAttributesAndStartGifffer(); //for EXISTING messages | |
observer.observe(document.getElementById('chat'), { | |
childList: true, | |
subtree: true | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment