Last active
March 4, 2018 04:03
-
-
Save bschlenk/a9b19645d6611705b26eb93e3c0288ac to your computer and use it in GitHub Desktop.
Hide any element on the page by meta+alt clicking it. Return the page to normal by shift+meta+alt clicking. https://gist.github.com/bschlenk/a9b19645d6611705b26eb93e3c0288ac/raw/hideit.user.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name HideIt | |
// @namespace http://tampermonkey.net/ | |
// @version 1.1.2 | |
// @description Hide any element on the page, with the ability to restore all. | |
// @author Brian Schlenker <[email protected]> | |
// @match *://*/* | |
// @noframes | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const DISPLAY_ATTR = 'data-display'; | |
const HIDDEN_ELEMENTS = []; | |
function getModifierState(e) { | |
if (Object.prototype.hasOwnProperty.call(e, 'getModifierState')) { | |
const alt = e.getModifierState('Alt'); | |
const meta = e.getModifierState('Meta'); | |
const shift = e.getModifierState('Shift'); | |
return { alt, meta, shift }; | |
} | |
const { | |
altKey: alt, | |
metaKey: meta, | |
shiftKey: shift, | |
} = e; | |
return { alt, meta, shift }; | |
} | |
function hideElement(e) { | |
e.setAttribute(DISPLAY_ATTR, e.style.display); | |
e.style.display = 'none'; | |
HIDDEN_ELEMENTS.push(e); | |
} | |
function showElement(e) { | |
const display = e.getAttribute(DISPLAY_ATTR); | |
e.style.display = display; | |
e.removeAttribute(DISPLAY_ATTR); | |
} | |
document.addEventListener('click', (e) => { | |
const { alt, meta, shift } = getModifierState(e); | |
if (alt && meta) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
if (shift) { | |
HIDDEN_ELEMENTS.forEach(showElement); | |
HIDDEN_ELEMENTS.length = 0; | |
} else { | |
hideElement(e.target); | |
} | |
} | |
}, true); // use capture to prevent hide-it click triggering other events | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment