Skip to content

Instantly share code, notes, and snippets.

@AldanaQuintana
Last active November 14, 2015 04:48
Show Gist options
  • Save AldanaQuintana/7782933424fbbd6a70d6 to your computer and use it in GitHub Desktop.
Save AldanaQuintana/7782933424fbbd6a70d6 to your computer and use it in GitHub Desktop.
Chrome extension to have ctrl + d sublime shortcut everywhere
javascript:(function(){
var d_code = 68;
var space_code = 32;
var enter_code = 10;
function validatePosition(text, position){
return (
text[position] &&
text[position].charCodeAt(0) != space_code &&
text[position].charCodeAt(0) != enter_code &&
text[position] != "," &&
text[position] != "." &&
text[position] != "@"
)
}
function selectWord(el) {
var text, start, end;
el.focus();
if(el.getAttribute("contenteditable")) {
var range = window.getSelection().getRangeAt(0).cloneRange()
text = el.textContent
start = range.startOffset;
end = range.endOffset;
} else {
text = el.value
start = el.selectionStart;
end = el.selectionEnd;
}
while(validatePosition(text, end)){
end = end + 1;
}
if((!validatePosition(text, start)) && validatePosition(text, start - 1)){
start = start - 2;
}
while(validatePosition(text, start)){
start = start - 1;
}
if (el.getAttribute("contenteditable")) {
var txtNode = el.childNodes[0];
var range = document.createRange();
var sel = window.getSelection();
range.setStart(txtNode, start + 1);
range.setEnd(txtNode, end);
sel.removeAllRanges();
sel.addRange(range);
} else {
el.setSelectionRange(start + 1, end);
}
}
document.onkeydown = function(e){
if(e.ctrlKey && e.keyCode == d_code){
selectWord(e.target);
window.getSelection();
e.preventDefault();
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment