|
| 1 | +// ==UserScript== |
| 2 | +// @name GitHub projects - focus on clicked card |
| 3 | +// @namespace https://www.wildfireinternet.co.uk/ |
| 4 | +// @version 0.1 |
| 5 | +// @description focuses out the rest of the page when a cards sidebar info is showing |
| 6 | +// @author Andrew |
| 7 | +// @match https://github.com/*/*/projects/* |
| 8 | +// @grant none |
| 9 | +// ==/UserScript== |
| 10 | + |
| 11 | +(function() { |
| 12 | + 'use strict'; |
| 13 | + |
| 14 | + // fade opacity of other content like a modal |
| 15 | + var style = document.createElement('style'); |
| 16 | + style.innerHTML = '.hide-sm ~ .project-columns { opacity: .5; }'; |
| 17 | + style.innerHTML += ' .js-project-card-details-pane { width: 560px !important; }'; |
| 18 | + document.body.appendChild(style); |
| 19 | + |
| 20 | + // helper to simulate click |
| 21 | + function clickEl(el) { |
| 22 | + var event = document.createEvent('HTMLEvents'); |
| 23 | + event.initEvent('click', true, false); |
| 24 | + el.dispatchEvent(event); |
| 25 | + } |
| 26 | + |
| 27 | + // delay helper, setTimeout but sweeter promise syntax ;) |
| 28 | + function delay(seconds) { |
| 29 | + let promise = new Promise(resolve => setTimeout(resolve, seconds)); |
| 30 | + return promise; |
| 31 | + } |
| 32 | + |
| 33 | + // helper to close card info |
| 34 | + function closeCardInfo() { |
| 35 | + var el = document.querySelector('.js-project-card-details .js-hide-project-card-details'); |
| 36 | + if (el && el.offsetParent) { |
| 37 | + delay(500).then(() => clickEl(el)); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // bind ESC to close card info |
| 42 | + document.body.addEventListener('keydown', (event) => { |
| 43 | + if (event.key == 'Escape' || event.key == 'Esc') { |
| 44 | + closeCardInfo(); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + // also bind click to close card info |
| 49 | + // ~~~~~~ |
| 50 | + // TODO: revisit this, i'm not happy that it's bound to EVERY click atm, not the most performant way of doing this! |
| 51 | + // ~~~~~~ |
| 52 | + document.body.addEventListener('click', (event) => { |
| 53 | + var els = document.querySelectorAll('.hide-sm ~ .project-columns'); |
| 54 | + if (els && els.length && els[0].contains(event.target)) { |
| 55 | + closeCardInfo(); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | +})(); |
| 60 | + |
| 61 | + |
0 commit comments