Skip to content

Instantly share code, notes, and snippets.

@bschlenk
Last active March 4, 2018 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bschlenk/a9b19645d6611705b26eb93e3c0288ac to your computer and use it in GitHub Desktop.
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
// ==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 <bschlenk@umich.edu>
// @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