Created
October 15, 2015 17:57
-
-
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
This file contains 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
/** | |
* 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