-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordAutoGif.user.js
67 lines (61 loc) · 2.58 KB
/
DiscordAutoGif.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// ==UserScript==
// @name DiscordAutoGif
// @namespace https://files.noodlebox.moe/
// @downloadURL https://files.noodlebox.moe/userscripts/DiscordAutoGif.user.js
// @version 1.0.1
// @description Automatically play GIF and GIFV embeds without hovering
// @author noodlebox
// @require https://code.jquery.com/jquery-3.1.1.min.js
// @match *://*.discordapp.com/channels/*
// @match *://*.discordapp.com/invite/*
// @match *://*.discordapp.com/login
// @run-at document-idle
// @grant none
// ==/UserScript==
(function ($) {
"use strict";
// Automatically play GIFs and "GIFV" Videos
$.fn.autoGif = function () {
// Handle GIF
this.find(".image:has(canvas)").each(function () {
var image = $(this);
var canvas = image.children("canvas").first();
// Replace GIF preview with actual image
var src = canvas.attr("src");
if(src !== undefined) {
image.replaceWith($("<img>", {
src: canvas.attr("src"),
width: canvas.attr("width"),
height: canvas.attr("height"),
}).addClass("image kawaii-autogif"));
}
});
// Handle GIFV
this.find(".embed-thumbnail-gifv:has(video)").each(function () {
var embed = $(this);
var video = embed.children("video").first();
// Remove the class, embed-thumbnail-gifv, to avoid the "GIF" overlay
embed.removeClass("embed-thumbnail-gifv").addClass("kawaii-autogif");
// Prevent the default behavior of pausing the video
embed.parent().on("mouseout.autoGif", function (event) {
event.stopPropagation();
});
video[0].play();
});
return this;
};
// Helper function for finding all elements matching selector affected by a mutation
var mutationFind = function (mutation, selector) {
var target = $(mutation.target), addedNodes = $(mutation.addedNodes);
var mutated = target.add(addedNodes).filter(selector);
var descendants = addedNodes.find(selector);
var ancestors = target.parents(selector);
return mutated.add(descendants).add(ancestors);
};
// Watch for new embeds in chat messages
new MutationObserver(function (mutations, observer) {
mutations.forEach(function (mutation) {
mutationFind(mutation, ".accessory").autoGif();
});
}).observe(document, { childList:true, subtree:true });
})(jQuery.noConflict(true));