I just want to add a simple feature to my Reddit userscript that enables me to press E on a post and it clicks the upvote button, I managed to do it with this code in jQuery but it also works when I'm typing in any input field, I don't know how to make it stop.
CODE:
$(document).ready(()=>{
$(document).keypress((e)=>{
if(e.key === 'e'){
//$('.voteButton[data-click-id="upvote"]')[0].click();
console.log('clicked!');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" />
NOTE: The question which this is supposed to be a duplicate of does help me but does not fix my problem directly, adding if($(e.target).is('input,select,button,textarea,div[role="textbox"]')) return;
is what fixed it, apparently Reddit uses wrappers for text fields so I had to add div[role="textbox"]
which is the "textbox" used, the thing is I will have to manually add custom selection for all of these wrapped elements.
You could try if the event element is a input then leave from function:
$(document).ready(()=>{
$(document).keypress((e)=>{
if($(e.target).is('input')) return
if(e.key === 'e'){
//$('.voteButton[data-click-id="upvote"]')[0].click();
console.log('clicked!');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input>