Skip to content

Instantly share code, notes, and snippets.

@DanBrink91
Created October 21, 2014 19:04
Show Gist options
  • Save DanBrink91/7c130e71506942335332 to your computer and use it in GitHub Desktop.
Save DanBrink91/7c130e71506942335332 to your computer and use it in GitHub Desktop.
Get highlighted text
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
var keysDown = {};
window.addEventListener("keyup", function(e){
keysDown[e.keyCode] = false;
});
window.addEventListener("keydown", function(e){
keysDown[e.keyCode] = true;
// ctrl + `
if(keysDown[192] && keysDown[17]){
var text = getSelectionText();
// do something with selected text
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment