-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfancy-rails-web-console.user.js
72 lines (58 loc) · 2.82 KB
/
fancy-rails-web-console.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
// ==UserScript==
// @name Fancy Rails Web Console
// @namespace http://tampermonkey.net/
// @version 1.5
// @description Improves the Rails Web Console
// @author Five Element Ninja (https://mastodon.social/@FiveElementNinja)
// @match http://localhost:3000/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=undefined.localhost
// @downloadURL https://github.com/FiveElementNinja/Fancy-Rails-Web-Console/raw/main/fancy-rails-web-console.user.js
// @updateURL https://github.com/FiveElementNinja/Fancy-Rails-Web-Console/raw/main/fancy-rails-web-console.user.js
// @grant none
// ==/UserScript==
// This userscript makes the Rails Web Console (https://github.com/rails/web-console) collapsible
// and collapses it automatically so you don't have to close it every time.
(function() {
'use strict';
// stylsheet
const consoleStyles = "<style>#console-btn { background-color: #333; color: #FFF; position: fixed; height: 16px; bottom: 0; right: 0; font-size: 10px; padding: 4px 2px 4px 4px; border-top-left-radius: 50%; line-height: 10px; cursor: pointer; }</style>";
// element refs
// TODO: can probably move these to setup() now, but too lazy atm
let consoleElem;
let consoleBtn;
// shows the console and hides the button
function showConsole() {
consoleElem.classList.remove('hidden');
consoleBtn.classList.add('hidden');
}
// hides the console and shows the button
function hideConsole(e) {
// these two are a hacky way to override the default event listener that removes the console from the DOM on close
e.stopImmediatePropagation();
e.stopPropagation();
consoleElem.classList.add('hidden');
consoleBtn.classList.remove('hidden');
}
// sets up all elements and listeners
function setup() {
const body = document.getElementsByTagName('body')?.[0];
const consoleBtnElem = '<div id="console-btn">>></div>';
consoleElem = document.getElementById('console');
const consoleCloseBtn = consoleElem?.querySelectorAll('.close-button')[0];
// add stylesheet and button to DOM
body?.insertAdjacentHTML('beforeend', consoleStyles);
body?.insertAdjacentHTML('beforeend', consoleBtnElem);
// hide console
consoleElem?.classList?.add('hidden');
// add hideConsole click event
consoleCloseBtn?.addEventListener('click', (e) => hideConsole(e), true);
// get reference to consoleBtn
consoleBtn = document.getElementById('console-btn');
// attach click event to the show console button
consoleBtn?.addEventListener('click', showConsole);
}
// detect page load and navigation and run setup()
document.addEventListener('turbo:load', function() {
setup();
});
})();