-
Notifications
You must be signed in to change notification settings - Fork 2
/
GitHub projects - focus on clicked card.user.js
61 lines (51 loc) · 1.95 KB
/
GitHub projects - focus on clicked card.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
// ==UserScript==
// @name GitHub projects - focus on clicked card
// @namespace https://www.wildfireinternet.co.uk/
// @version 0.1
// @description focuses out the rest of the page when a cards sidebar info is showing
// @author Andrew
// @match https://github.com/*/*/projects/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// fade opacity of other content like a modal
var style = document.createElement('style');
style.innerHTML = '.hide-sm ~ .project-columns { opacity: .5; }';
style.innerHTML += ' .js-project-card-details-pane { width: 560px !important; }';
document.body.appendChild(style);
// helper to simulate click
function clickEl(el) {
var event = document.createEvent('HTMLEvents');
event.initEvent('click', true, false);
el.dispatchEvent(event);
}
// delay helper, setTimeout but sweeter promise syntax ;)
function delay(seconds) {
let promise = new Promise(resolve => setTimeout(resolve, seconds));
return promise;
}
// helper to close card info
function closeCardInfo() {
var el = document.querySelector('.js-project-card-details .js-hide-project-card-details');
if (el && el.offsetParent) {
delay(500).then(() => clickEl(el));
}
}
// bind ESC to close card info
document.body.addEventListener('keydown', (event) => {
if (event.key == 'Escape' || event.key == 'Esc') {
closeCardInfo();
}
});
// also bind click to close card info
// ~~~~~~
// TODO: revisit this, i'm not happy that it's bound to EVERY click atm, not the most performant way of doing this!
// ~~~~~~
document.body.addEventListener('click', (event) => {
var els = document.querySelectorAll('.hide-sm ~ .project-columns');
if (els && els.length && els[0].contains(event.target)) {
closeCardInfo();
}
});
})();