Skip to content

Instantly share code, notes, and snippets.

@bearr
Created November 15, 2021 14:35
Show Gist options
  • Save bearr/ec992faa6a8d874951a773be006b898f to your computer and use it in GitHub Desktop.
Save bearr/ec992faa6a8d874951a773be006b898f to your computer and use it in GitHub Desktop.
wrap selected text with quotes - IDE style
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