Created
November 15, 2021 14:35
-
-
Save bearr/ec992faa6a8d874951a773be006b898f to your computer and use it in GitHub Desktop.
wrap selected text with quotes - IDE style
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
const search = document.querySelector([ | |
'input[name="q"]', // google, github, ... | |
'input[name="query"]:not([type="hidden"])' // craigslist | |
].join(',')) | |
if (search instanceof HTMLInputElement) { | |
search.addEventListener('keydown', function (event) { | |
let { | |
selectionStart: start, | |
selectionEnd: end | |
} = this | |
if (event.code !== 'Quote' || start === end) return | |
event.preventDefault() | |
if (' ' === this.value[end - 1]) end -= 1 | |
if (' ' === this.value[start]) start += 1 | |
this.setRangeText(`"${this.value.slice(start, end)}"`, start, end) | |
this.setSelectionRange(start + 1, end + 1) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment