Skip to content

Commit

Permalink
Merge pull request #112 from 0x6362/nit/update-deprecated-keycode-code
Browse files Browse the repository at this point in the history
Nit: Update code using deprecated keyCode API
  • Loading branch information
artemave authored Nov 13, 2024
2 parents 6e72233 + 640c441 commit 93f81c3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
15 changes: 7 additions & 8 deletions contentscript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import $ from 'jquery'
import {
renderError,
modifierKeys,
escape_html,
formatTranslation
} from './lib/transover_utils'
Expand Down Expand Up @@ -200,7 +199,7 @@ function processEvent(e) {
return res
}

function getExactTextNode(nodes, e) {
function getExactTextNode(_, e) {
$(text_nodes).wrap('<transblock />')
let hit_text_node = document.elementFromPoint(e.clientX, e.clientY)

Expand Down Expand Up @@ -388,7 +387,7 @@ function processEvent(e) {
}
}

function withOptionsSatisfied(e, do_stuff) {
function withOptionsSatisfied(_, do_stuff) {
if (!options) return

//respect 'translate only when alt pressed' option
Expand Down Expand Up @@ -464,7 +463,7 @@ function speak({ text, lang }) {
}

$(document).keydown(e => {
if (e.keyCode === 27) {
if (e.key === 'Escape') {
audio.pause()
audio.removeAttribute('src')
audio.load()
Expand All @@ -481,7 +480,7 @@ function speak({ text, lang }) {
$(document).keydown(function(e) {
if (!options) return

if (modifierKeys[e.keyCode] == options.popup_show_trigger) {
if (e.key == options.popup_show_trigger) {
show_popup_key_pressed = true

const selection = window.getSelection().toString()
Expand All @@ -505,7 +504,7 @@ $(document).keydown(function(e) {
}

// text-to-speech on ctrl press
if (!e.originalEvent.repeat && modifierKeys[e.keyCode] == options.tts_key && options.tts && $('transover-popup').length > 0) {
if (!e.originalEvent.repeat && e.key == options.tts_key && options.tts && $('transover-popup').length > 0) {
chrome.runtime.sendMessage({
handler: 'trackEvent',
event: {
Expand Down Expand Up @@ -538,11 +537,11 @@ $(document).keydown(function(e) {
}

// Hide tat popup on escape
if (e.keyCode == 27) {
if (e.key == 'Escape') {
removePopup('transover-type-and-translate-popup')
}
}).keyup(function(e) {
if (options && modifierKeys[e.keyCode] == options.popup_show_trigger) {
if (options && e.key == options.popup_show_trigger) {
show_popup_key_pressed = false
}
})
Expand Down
6 changes: 3 additions & 3 deletions lib/options_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async function fill_reverse_lang() {
async function populate_popup_show_trigger() {
const saved_popup_show_trigger = await Options.popup_show_trigger()

;[...new Set(Object.values(modifierKeys))].forEach(function(key) {
modifierKeys.forEach(function(key) {
$('#word_key_only_key, #selection_key_only_key').each(function() {
$(this).append($('<option>', {value: key}).text(key).prop('selected', saved_popup_show_trigger == key))
})
Expand Down Expand Up @@ -223,7 +223,7 @@ async function load() {
$('#fontSize').val(await Options.fontSize())

$('#tts').attr('checked', await Options.tts() ? true : false)
;[...new Set(Object.values(modifierKeys))].forEach(async function(key) {
modifierKeys.forEach(async function(key) {
$('#tts_key').append($('<option>', {value: key}).text(key).prop('selected', await Options.tts_key() == key))
})

Expand All @@ -232,7 +232,7 @@ async function load() {

$('#save_button').click(function() { save_options() })
$(document).on('keydown', function(e) {
if (e.keyCode == 13) {
if (e.key === 'Enter') {
save_options()
}
})
Expand Down
4 changes: 2 additions & 2 deletions lib/tat_popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ class TatPopupTransover extends HTMLElement {
}

this.q('main').onkeydown = (e) => {
if (e.keyCode == 13) {
if (e.key == 'Enter') {
sendTranslate()
}
// let 'escape' be handled in the host context (by content script)
if (e.keyCode == 27) {
if (e.key == 'Escape') {
return
}
e.stopPropagation()
Expand Down
11 changes: 8 additions & 3 deletions lib/transover_utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { languages } from './languages'

export const modifierKeys = {
16: 'shift', 17: 'ctrl', 18: 'alt', 224: 'meta', 91: 'command', 93: 'command', 13: 'Return'
}
export const modifierKeys = new Set([
'Alt',
'Control',
'Enter',
'Escape',
'Meta',
'Shift'])


export function renderError (message) {
return `
Expand Down

0 comments on commit 93f81c3

Please sign in to comment.