forked from JeanOUINA/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransitioningBackgrounds.plugin.js
240 lines (195 loc) · 9.71 KB
/
TransitioningBackgrounds.plugin.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//META{"name":"TransitioningBackgrounds","website":"https://metalloriff.github.io/toms-discord-stuff/","source":"https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/TransitioningBackgrounds.plugin.js"}*//
class TransitioningBackgrounds {
getName() { return "TransitioningBackgrounds"; }
getDescription() { return "Allows you to set a list of backgrounds that will be transitioned between with several transition animations, in order, or at random."; }
getVersion() { return "1.1.3"; }
getAuthor() { return "Metalloriff"; }
getChanges() {
return {
"1.1.3": `
Entirely rewrote the plugin. Should be more stable, smoother, and most importantly, actually works at all.
`
};
}
get settingFields() {
return {
images : {
label : "Image URLs",
description : "Images must be a direct link. (ending in .png, .jpg, .jpeg, .gif, etc)",
type : "string",
array : true
},
transitionMethod : {
label : "Transition animation",
type : "radio",
choices : {
"none" : { label : "None" },
"fade" : { label : "Fade" },
"slide-left" : { label : "Slide Left" },
"slide-right" : { label : "Slide Right" },
"slide-up" : { label : "Slide Up" },
"slide-down" : { label : "Slide Down" },
"shrink" : { label : "Shrink" },
"rotate-x" : { label : "Rotation Horizontally" },
"rotate-y" : { label : "Rotation Vertically" },
"zoom-fade" : { label : "Zoom and Fade" }
}
},
changeDelay : {
label : "Image lifetime",
description : "The amount of time in seconds that images will last before beginning the transition to the next image.",
type : "float",
min : 1, max : 10000
},
transitionTime : {
label : "Transition animation length",
description : "The amount of time in seconds that the transition between image to image lasts.",
type : "float",
min : 0, max : 10000
},
randomize : {
label : "Pick images at random",
description : "Whether or not you want images to be picked at random or in the order you put them in.",
type : "boolean"
},
onlyRunWhileFocused : {
label : "Only run while Discord is in focus",
description : "If enabled, the images won't change if you are out of window, to save bandwidth for bad internet.",
type : "boolean"
},
forceTransparency : {
label : "Force transparency",
description : "Attempts to force transparency for themes that don't have a background, or the default theme. A transparent theme is recommended.",
type : "boolean"
},
backgroundBrightness : {
label : "Background brightness",
type : "float",
min : 0, max : 1
}
};
}
load() {}
start() {
let libLoadedEvent = () => {
try{ this.onLibLoaded(); }
catch(err) { console.error(this.getName(), "fatal error, plugin could not be started!", err); try { this.stop(); } catch(err) { console.error(this.getName() + ".stop()", err); } }
};
let lib = document.getElementById("NeatoBurritoLibrary");
if(lib == undefined) {
lib = document.createElement("script");
lib.setAttribute("id", "NeatoBurritoLibrary");
lib.setAttribute("type", "text/javascript");
lib.setAttribute("src", "https://rawgit.com/Metalloriff/BetterDiscordPlugins/master/Lib/NeatoBurritoLibrary.js?forceNew=" + performance.now());
document.head.appendChild(lib);
}
if(typeof window.NeatoLib !== "undefined") libLoadedEvent();
else lib.addEventListener("load", libLoadedEvent);
}
getSettingsPanel() {
setTimeout(() => {
NeatoLib.Settings.create(this);
NeatoLib.Settings.pushChangelogElements(this);
}, 0);
return "";
}
saveSettings() {
this.update();
NeatoLib.Settings.save(this);
}
onLibLoaded() {
this.settings = NeatoLib.Settings.load(this, {
displayUpdateNotes : true,
images : [],
transitionMethod : "zoom-fade",
changeDelay : 30,
transitionTime : 3,
randomize : false,
onlyRunWhileFocused : false,
forceTransparency : false,
backgroundBrightness : 0.5
});
NeatoLib.Updates.check(this);
if(this.settings.displayUpdateNotes) NeatoLib.Changelog.compareVersions(this.getName(), this.getChanges());
this.focused = true;
window.addEventListener("focus", this.focusEvent = () => { this.focused = true; });
window.addEventListener("blur", this.blurEvent = () => { this.focused = false; });
this.update();
NeatoLib.Events.onPluginLoaded(this);
}
update() {
let css = `
.transitioning-background {
background-size: cover;
width: 100%;
height: 100%;
position: absolute;
transition: all ${this.settings.transitionTime}s ease-in-out;
filter: brightness(${this.settings.backgroundBrightness});
}
.tb-fade { opacity: 0; }
.tb-slide-left { transform: translateX(-100%); }
.tb-slide-right { transform: translateX(100%); }
.tb-slide-up { transform: translateY(-100%); }
.tb-slide-down { transform: translateY(100%); }
.tb-shrink { transform: scale(0); }
.tb-rotate-x { transform: rotateX(180deg); }
.tb-rotate-y { transform: rotateY(180deg); }
.tb-zoom-fade { transform: scale(5); opacity: 0; }
`;
if (this.settings.forceTransparency) css = css + `
.container-2lgZY8, .theme-dark .layer-3QrUeG, .theme-dark .layers-3iHuyZ, .theme-dark .chat-3bRxxu, .theme-dark .chat-3bRxxu form, .theme-dark .content-yTz4x3, .theme-dark .chat-3bRxxu, .theme-dark .chat-3bRxxu form, .theme-dark .content-yTz4x3, .theme-dark .messagesWrapper-3lZDfY, #app-mount .channels-Ie2l6A, .theme-dark .members-1998pB, #app-mount .container-PNkimc, .theme-dark .wrapper-1Rf91z, .theme-dark .title-3qD0b-, .theme-dark .typing-2GQL18, .theme-dark .container-3gCOGc, .theme-dark .headerBar-UHpsPw, .theme-dark .activityFeed-28jde9, .theme-dark .gameLibrary-TTDw4Y, .theme-dark .container-2Thooq, .theme-dark .ui-standard-sidebar-view, #app-mount .container-1UB9sr, .hasDropdown-2invhV .header-2o-2hj {
background: transparent !important;
}
`;
if (this.css) this.css.destroy();
this.css = NeatoLib.injectCSS(css);
clearInterval(this.loop);
clearTimeout(this.removeTimeout);
let existing = document.getElementsByClassName("transitioning-background");
for (let i = existing.length - 1; i > -1; i--)
existing[i].remove();
if (!this.settings.images.length){
NeatoLib.showToast("You have not added any images. Please add some in the TransitioningBackgrounds settings.", "error");
return;
}
this.index = 0;
this.loop = setInterval(() => {
this.transition();
}, this.settings.changeDelay * 1000);
this.transition();
}
transition() {
if (this.settings.images.length == 0 || (this.settings.onlyRunWhileFocused && !this.focused)) return;
let app = document.getElementsByClassName(NeatoLib.getClass("app"))[0],
existing = document.getElementsByClassName("transitioning-background"),
next = this.index + 1 >= this.settings.images.length ? 0 : this.index + 1,
last = this.index;
if (this.settings.randomize) {
next = this.settings.images.length * Math.random() << 0;
while (next == last)
next = this.settings.images.length * Math.random() << 0;
}
if (existing.length > 1) {
existing[1].className += " tb-" + this.settings.transitionMethod;
this.removeTimeout = setTimeout(() => {
existing[1].remove();
app.insertAdjacentHTML("afterbegin", `<div class="transitioning-background" style="background-image:url(${this.settings.images[next]})"></div>`);
}, this.settings.transitionTime * 1000)
} else {
app.insertAdjacentHTML("afterbegin", `<div class="transitioning-background" style="background-image:url(${this.settings.images[this.index]})"></div>`);
app.insertAdjacentHTML("afterbegin", `<div class="transitioning-background" style="background-image:url(${this.settings.images[next]})"></div>`);
}
this.index = next;
}
stop() {
window.removeEventListener("focus", this.focusEvent);
window.removeEventListener("blur", this.blurEvent);
clearInterval(this.loop);
clearTimeout(this.removeTimeout);
let existing = document.getElementsByClassName("transitioning-background");
for (let i = existing.length - 1; i > 0; i--)
existing[i].remove();
this.css.destroy();
}
}