Skip to content

Instantly share code, notes, and snippets.

@dgaitsgo
Created September 20, 2020 19:46
Show Gist options
  • Select an option

  • Save dgaitsgo/8c5a1424c24c437850a7f411357a0681 to your computer and use it in GitHub Desktop.

Select an option

Save dgaitsgo/8c5a1424c24c437850a7f411357a0681 to your computer and use it in GitHub Desktop.
initImageHoverListeners = (image: HTMLImageElement): void => {
const imageButtonId = 'bai-image-btn-container'
const imageLink = this.getImageContainer(image)
const buttonContainer = this.getButtonContainer('image')
if (imageLink) {
const wrapper = imageLink.parentNode
wrapper?.addEventListener('mouseover', (e) => {
if (e.target === image) {
document.getElementById(imageButtonId)?.remove()
imageLink.style.position = 'relative'
imageLink.prepend(buttonContainer)
window.__loadBAIButton({
type: 'image',
clickCb: this.safeRequest(() => this.requestAnalyzeImage(image)),
})
}
})
wrapper?.addEventListener('mouseout', (e: ChromeEvent) => {
const nextTarget = e?.toElement
const id = nextTarget?.getAttribute('id')
const isButtonContainerChild = nextTarget?.closest('#' + imageButtonId)
if (id !== imageButtonId && !isButtonContainerChild) {
if (e.target === image) {
document.getElementById(imageButtonId)?.remove()
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment