Skip to content

Commit

Permalink
Fixed chatbar always auto-focuses on 1st load ↞ [auto-sync from `adam…
Browse files Browse the repository at this point in the history
…lui/userscripts`]
  • Loading branch information
adamlui authored and kudo-sync-bot committed May 5, 2024
1 parent 11f1dba commit 77c91c0
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions googlegpt/greasemonkey/googlegpt.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
// @description:zu Faka amaphawu ase-ChatGPT kuvaliwe i-Google Search
// @author KudoAI
// @namespace https://kudoai.com
// @version 2024.5.4.1
// @version 2024.5.5.1
// @license MIT
// @icon https://media.googlegpt.io/images/icons/googlegpt/black/icon48.png
// @icon64 https://media.googlegpt.io/images/icons/googlegpt/black/icon64.png
Expand Down Expand Up @@ -488,7 +488,7 @@
while (true) {
let replyLanguage = prompt(
( msgs.prompt_updateReplyLang || 'Update reply language' ) + ':', config.replyLanguage)
if (replyLanguage == null) break // user cancelled so do nothing
if (replyLanguage rqEventHandler) break // user cancelled so do nothing
else if (!/\d/.test(replyLanguage)) {
replyLanguage = ( // auto-case for menu/alert aesthetics
[2, 3].includes(replyLanguage.length) || replyLanguage.includes('-') ? replyLanguage.toUpperCase()
Expand Down Expand Up @@ -1082,15 +1082,15 @@
})
})}

function rqEventHandler(event) { // for attachment/removal in `getShowReply()` + `appShow().handleSubmit()`
function handleRQevent(event) { // for attachment/removal in `getShowReply()` + `appShow().handleSubmit()`
if ([' ', 'Enter'].includes(event.key) || event.type == 'click') {
event.preventDefault() // prevent scroll on space taps

// Remove divs/listeners
const relatedQueriesDiv = document.querySelector('.related-queries')
Array.from(relatedQueriesDiv.children).forEach(relatedQueryDiv => {
relatedQueryDiv.removeEventListener('click', rqEventHandler)
relatedQueryDiv.removeEventListener('keydown', rqEventHandler)
relatedQueryDiv.removeEventListener('click', handleRQevent)
relatedQueryDiv.removeEventListener('keydown', handleRQevent)
})
relatedQueriesDiv.remove()

Expand All @@ -1100,6 +1100,7 @@
chatbar.value = event.target.textContent
chatbar.dispatchEvent(new KeyboardEvent('keydown', {
key: 'Enter', bubbles: true, cancelable: true }))
appShow.submitSrc = 'relatedQuery' // to not auto-focus chatbar in appShow()
}}}

async function getShowReply(convo, callback) {
Expand Down Expand Up @@ -1160,8 +1161,8 @@
// Add fade + listeners
setTimeout(() => {
relatedQueryDiv.classList.add('active')
relatedQueryDiv.addEventListener('click', rqEventHandler)
relatedQueryDiv.addEventListener('keydown', rqEventHandler)
relatedQueryDiv.addEventListener('click', handleRQevent)
relatedQueryDiv.addEventListener('keydown', handleRQevent)
}, index * 100)
})

Expand Down Expand Up @@ -1441,8 +1442,9 @@
sendButton.addEventListener('mouseover', toggleTooltip)
sendButton.addEventListener('mouseout', toggleTooltip)

// Focus chatbar if user interacted
if (appShow.submitted) chatTextarea.focus()
// Focus chatbar if user typed in prev appShow()
if (appShow.submitSrc && appShow.submitSrc != 'relatedQuery') chatTextarea.focus()
appShow.submitSrc = 'none'

function handleEnter(event) {
if (event.key == 'Enter') {
Expand Down Expand Up @@ -1475,8 +1477,8 @@
try {
const relatedQueriesDiv = document.querySelector('.related-queries')
Array.from(relatedQueriesDiv.children).forEach(relatedQueryDiv => {
relatedQueryDiv.removeEventListener('click', rqEventHandler)
relatedQueryDiv.removeEventListener('keydown', rqEventHandler)
relatedQueryDiv.removeEventListener('click', handleRQevent)
relatedQueryDiv.removeEventListener('keydown', handleRQevent)
})
relatedQueriesDiv.remove()
} catch (err) {}
Expand All @@ -1488,9 +1490,6 @@
// Show loading status
replySection.classList.add('loading', 'no-user-select')
replySection.innerText = appAlerts.waitingResponse

// Flag for chatbar auto-focus on subsequent loads
appShow.submitted = true
}

// Autosize chatbar function
Expand Down

0 comments on commit 77c91c0

Please sign in to comment.