Skip to content

Instantly share code, notes, and snippets.

@ayaysir
Last active November 16, 2022 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayaysir/8c22f71859ef070029a28da520ba68bb to your computer and use it in GitHub Desktop.
Save ayaysir/8c22f71859ef070029a28da520ba68bb to your computer and use it in GitHub Desktop.
With this code, you'll get a SELECT menu in front of the Google search bar to select a prefix. // 1. install js-injection chrome plugin // 2. in google.com, add this code with select 'use jQuery 3' option
const KEY_LAST_SELECTED = "LAST_SELECTED"
const divText = `
<select id="q-prefix" style="float: left;">
<option>none</option>
<option selected>swift</option>
<option>js</option>
<option>jquery</option>
<option>java</option>
<option>musescore</option>
<option>logic pro x</option>
<option>davinci resolve</option>
</select>
`
$(".SDkEP").prepend(divText)
const gInput = $("input.gLFyf.gsfi")
const qPrefix = $("#q-prefix")
// change last selected option
const lastSelected = localStorage.getItem(KEY_LAST_SELECTED) ?? "js"
qPrefix.val(lastSelected)
gInput.on("keyup", function(e) {
const optionValue = qPrefix.val()
if (optionValue == "none") {
return
}
let isPrefixCorrect = gInput.val().startsWith(`${optionValue} `)
if ( !isPrefixCorrect ) {
gInput.val(`${optionValue} `)
gInput.focus()
}
})
qPrefix.on("change", function(e) {
const changedValue = qPrefix.val() === "none" ? gInput.val() : `${qPrefix.val()} `
gInput.val(changedValue)
gInput.focus()
// save selected value to localStorage
localStorage.setItem(KEY_LAST_SELECTED, qPrefix.val())
})
// 검색어 입력하면 나오는 X 버튼
$(".M2vV3.vOY7J").on("click", function(e) {
setTimeout(function() {
gInput.val(`${qPrefix.val()} `)
gInput.focus()
}, 10)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment