Skip to content

Instantly share code, notes, and snippets.

@restlessdesign
Created October 15, 2015 17:57
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 restlessdesign/2cca383b4c4baef3267b to your computer and use it in GitHub Desktop.
Save restlessdesign/2cca383b4c4baef3267b to your computer and use it in GitHub Desktop.
Proactively hide sensitive items before OS X keyboard shortcuts for taking screenshots are completed
/**
* secret.js
*
* Hides senstive DOM elements on the page before a screenshot can
* be taken on OS X using the Cmd + Shift + 3 or Cmd + Shift + 4
* keyboard shortcuts under *some* circumstances. Obviously this
* will only work if the browser has focus.
*
* A note on Windows:
* The last time I checked, the PrtScn key on Windows fired as a
* system event and triggered a screenshot before the key event
* reaches the browser.
*/
const CMD_KEY_LEFT = 91;
const CMD_KEY_RIGHT = 93;
const SHIFT_KEY = 16;
const sensitive_items = [
document.getElementById('secret'),
document.getElementById('another_secret')
];
var active_keys = [];
function disableSensitiveItems() {
sensitive_items.forEach(function(el) {
el.style.display = 'none';
});
}
function enableSensitiveItems() {
sensitive_items.forEach(function(el) {
el.style.display = '';
});
}
function _checkKeyCombo() {
function isPressed(key) {
return active_keys.indexOf(key) !== -1;
}
if (isPressed(SHIFT_KEY) && (isPressed(CMD_KEY_LEFT) || isPressed(CMD_KEY_RIGHT))) {
disableSensitiveItems();
}
}
function _handleKeyDown(e) {
active_keys.push(e.keyCode);
_checkKeyCombo();
}
function _handleKeyUp() {
active_keys = [];
enableSensitiveItems();
}
window.addEventListener('keydown', _handleKeyDown);
window.addEventListener('keyup', _handleKeyUp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment