Skip to content

Instantly share code, notes, and snippets.

@gvoxc
Created March 9, 2024 02:41
Show Gist options
  • Select an option

  • Save gvoxc/ad1e967efa128092848b6f7b4844c371 to your computer and use it in GitHub Desktop.

Select an option

Save gvoxc/ad1e967efa128092848b6f7b4844c371 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name trans
// @match *://*/*
// ==/UserScript==
let ctrlPressed = false
// 监听键盘按下事件
document.addEventListener('keydown', function(event) {
// 按下 Ctrl,F 时, keyup 未检测到 Ctrl,F
// 解: 其会出现 2 个连续 keydown 事件 (e.key17, e.ctrl&e.key70), 当检测到第二个事件时无效化第一个
//console.log(event.keyCode)
if(event.ctrlKey && event.keyCode === 70) {
ctrlPressed = false
}
if (event.keyCode === 17) { // Ctrl 键被按下
ctrlPressed = true
setTimeout(function() {
if (!ctrlPressed) return
ctrlPressed = false
console.log('超时已释放')
}, 3000);
//console.log(ctrlPressed)
}
})
// 监听键盘释放事件
document.addEventListener('keyup', function(event) {
//console.log(event.keyCode)
if (event.keyCode === 17) {
ctrlPressed = false
//console.log(ctrlPressed)
}
})
// 监听鼠标释放事件
document.addEventListener('mouseup', function(event) {
let selection = window.getSelection().toString()
if (!ctrlPressed)
return
if (selection !== '') {
window.open("quicker:runaction:trans_cr?" + selection)
} else { // 点击超链接时
ctrlPressed = false
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment