This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbit_toggle_controls.user.js
276 lines (261 loc) · 10.9 KB
/
rabbit_toggle_controls.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// ==UserScript==
// @name Rabbit toggle controls
// @namespace https://github.com/ZashIn/rabbit-extensions
// @version 2.5
// @description Toggles rabb.it controls on fullscreen change and with # (default key), controls bar is shown on hover by default.
// @author Zash
// @updateURL https://github.com/ZashIn/rabbit-extensions/raw/master/rabbit_toggle_controls.user.js
// @downloadURL https://github.com/ZashIn/rabbit-extensions/raw/master/rabbit_toggle_controls.user.js
// @supportURL https://github.com/ZashIn/rabbit-extensions/issues
// @match *://www.rabb.it/*
// @exclude *://www.rabb.it/
// @grant GM_addStyle
// @run-at document-idle
// ==/UserScript==
/*
See settings below:
- fullscreen: options
- enter = disable controls - exceptions
- leave = enable controls - exceptions
- hotkey: toggle = all on / off, but exceptions (keep on / off)
*/
(function() {
'use strict';
// Options enum per setting, use for e.g. as: opt.toggle
var opt = Object.freeze({
unchanged:1 // Does not change the element
,enabled:2 // Enables the element permanent.
,disabled:3 // Disables the element permanent.
,toggle:4 // Toggles the element on/off
});
// Settings and controls definitions //
var settings = {};
{
let fs =
settings.fullscreen = {
enabled: true // true = toggles (enables/disables) on fullscreen change
// Use the opt enum to toggle / disable (permanent) / enable (permanent) / not change the following elements
,friends: opt.unchanged // Toggle friends
,controls: opt.toggle // Bottom controls: toggle
,chat: opt.toggle
};
settings.hotkey = {
enabled: true // true = hotkey to toggle controls
,hotkeyTest: e => (
e.key === '#'
&& e.altKey === false
&& e.shiftKey === false
&& e.ctrlKey === false
)
// See fs = fullscreen above. Change here for different settings using the hotkey.
,friends: fs.friends
,controls: fs.controls
,chat: fs.chat
};
settings.hoverControls = true; // Hover over controls bar to show it (when disabled / toggled).
settings.addReactionHideButton = true; // Adds a button to hide (toggle) the reactions (bottom right).
}
/**
* Control class
* @class
* @prop {string} selector - The selector of the control element.
* @method {function} enable
* @method {function} disable
* @method {function} toggle
* @property {boolean} disabled - whether the control is disabled. (dynamic: use getter)
* @prop {Element} element - The reference to the DOM element.
*/
var Control = class {
constructor(selector, definitions) {
this.selector = selector;
// Copies properties with getter
for (let p of Object.keys(definitions)) {
Object.defineProperty(this, p, Object.getOwnPropertyDescriptor(definitions, p));
}
}
/**
* Lazy property on first call.
* @return {Element} The reference to the DOM element.
*/
get element() { var v = document.querySelector(this.selector); Object.defineProperty(this, 'element', {value: v}); return v; }
};
var controlsHover, controlsHoverOut;
var hoverBar = document.createElement('div');
hoverBar.style = 'background-color: transparent; height: 5px; width: 100%; position: absolute; bottom: 0;';
var controls = {
controls: {
friends: new Control('.trayButton.toggleBubbles', {
enable() { if (this.disabled) this.toggle(); }
,disable() { if (!this.disabled) this.toggle(); }
,toggle() { this.element.click() }
,get disabled() { return !this.element.textContent.contains('show'); }
})
,controls: (settings.hoverControls ?
// Show on hover
new Control('.tray.screencast', {
enable() {
if (controlsHover) {
controlsHover();
[this.element, hoverBar].forEach(e => e.removeEventListener('mouseover', controlsHover));
}
if (controlsHoverOut) this.element.removeEventListener('mouseout', controlsHoverOut);
this.disabled = false;
}
,disable() { this.showOnlyOnHover(); this.disabled = true; }
,toggle() { this.disabled ? this.enable() : this.disable(); }
,disabled: false
,showOnlyOnHover() {
var el = this.element;
if (controlsHover) [el, hoverBar].forEach(e => e.removeEventListener('mouseover', controlsHover));
if (controlsHoverOut) el.removeEventListener('mouseout', controlsHoverOut);
el.style.position = 'relative';
// Add hoverBar at bottom of fullscreen.
if (!hoverBar.parentElement) el.parentElement.appendChild(hoverBar);
controlsHover = function(e) {
//el.style.backgroundColor = '';
el.style.display = '';
};
[el, hoverBar].forEach(e => e.addEventListener('mouseover', controlsHover));
el.addEventListener('mouseout', controlsHoverOut = function(e) {
//el.style.backgroundColor = 'transparent';
el.style.display = 'none';
});
controlsHoverOut();
}
})
// Simply hide
: new Control('.tray.screencast', {
enable() { this.element.display = ''; }
,disable() { this.element.display = 'none'; }
,toggle() { this.element.display = this.disabled ? '' : 'none'; }
,get disabled() { return this.element.display == 'none'; }
})
)
,chat: (() => {
var delayed = function(e, func) {
if (e === null) {
setTimeout(func, 500);
} else {
func();
}
};
var control = new Control('.toggle.right', {
enable() { delayed(this.element, () => this.disabled && this.element.click()); }
,disable() { delayed(this.element, () => !this.disabled && this.element.click()); }
,toggle() { delayed(this.element, () => this.element.click()); }
,get disabled() { return !this.element.classList.contains('open'); }
});
Object.defineProperty(control, 'element', {get() { return document.querySelector(this.selector); }});
return control;
})()
/*,: new control('', {
,enable() { }
,disable() { }
,toggle() { }
,get disabled() { return ; }
})*/
}
,disable(setting) {
for (const [name, c] of Object.entries(this.controls)) {
if (setting[name] === opt.enabled) {
// Ensure enabled elements are always enabled.
c.enable();
} else if ([opt.disabled, opt.toggle].includes(setting[name])) {
c.disable();
}
}
}
,enable(setting) {
for (const [name, c] of Object.entries(this.controls)) {
if ([opt.enabled, opt.toggle].includes(setting[name])) { c.enable(); }
}
}
,toggle(setting) {
for (const [name, c] of Object.entries(this.controls)) {
switch (setting[name]) {
case opt.toggle:
c.toggle();
break;
case opt.enabled:
c.enable();
break;
case opt.disabled:
c.disable();
}
}
}
};
// Button to hide reactions. Returns false if button could not be added.
var reactionHideButton = null;
var addReactionHideButton = !settings.addReactionHideButton ? null :
function() {
var reactionButtons = document.querySelector('.reactionsMenu');
if (!reactionButtons) return false;
reactionButtons.style.position = 'relative';
if (!reactionHideButton) {
reactionHideButton = document.createElement('div');
reactionHideButton.id = 'reactionHideButton';
reactionHideButton.className = 'reactionOption';
reactionHideButton.textContent = 'Hide >';
reactionHideButton.title = 'Toggle reaction overlay';
var reactionsOverlay = null;
reactionHideButton.onclick = function() {
reactionsOverlay = reactionsOverlay || document.querySelector('.reactionsOverlay');
reactionsOverlay.hidden ^= true;
this.textContent = reactionsOverlay.hidden ? 'Show >' : 'Hide >';
}
var style = document.getElementById('RabbitToggleControlsStyle');
if (!style) {
style = GM_addStyle(
`
#reactionHideButton {
color: #CCC;
white-space: nowrap;
line-height: 33px;
vertical-align: bottom;
position: absolute;
bottom: 0;
right: calc(100% + 10px);
}
#reactionHideButton:hover {
color: white;
}
`
);
style.id = 'RabbitToggleControlsStyle';
}
}
reactionButtons.appendChild(reactionHideButton);
return true;
};
// Events //
var fullscreenElement = 'fullscreenElement mozFullScreenElement webkitFullscreenElement'.split(' ').find(p => document[p] !== undefined)
, fullscreenchange = 'fullscreenchange mozfullscreenchange webkitfullscreenchange'.split(' ').find(p => document['on' + p] !== undefined);
// Remove previous event listener, for testing.
/*
var f,f2;
document.removeEventListener('keyup', f);
document.removeEventListener(fullscreenchange, f2);
/**/
if (settings.fullscreen.enabled) {
document.addEventListener(fullscreenchange, /*f2 =*/ function() {
if (document[fullscreenElement] === null) {
controls.enable(settings.fullscreen);
} else {
controls.disable(settings.fullscreen);
}
addReactionHideButton && setTimeout(addReactionHideButton, 1000);
});
}
if (settings.hotkey.enabled) {
document.addEventListener('keyup', /*f =*/ function(e) {
if (settings.hotkey.hotkeyTest(e)) {
controls.toggle(settings.hotkey);
}
}, false);
}
// Add reaction hide button. if the container is not found, retry it.
if (addReactionHideButton && !addReactionHideButton()) {
var timer = setInterval(() => addReactionHideButton() && clearInterval(timer), 1000);
}
})();