Skip to content

Commit

Permalink
🐛 Bug: Fix the bug causing shortcut keys to execute repeatedly
Browse files Browse the repository at this point in the history
  • Loading branch information
yym68686 committed Nov 29, 2024
1 parent 2626fca commit 27f566f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
22 changes: 21 additions & 1 deletion content.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CerebrSidebar {
this.initialized = false;
this.pageKey = window.location.origin + window.location.pathname;
console.log('CerebrSidebar 实例创建');
this.lastToggleTime = null; // 添加上次执行时间存储
this.initializeSidebar();
}

Expand Down Expand Up @@ -171,13 +172,32 @@ class CerebrSidebar {
if (!this.initialized) return;

try {
const currentTime = new Date();
const timeDiff = this.lastToggleTime ? currentTime - this.lastToggleTime : 0;

// 如果时间间隔小于10ms则不执行
if (timeDiff > 0 && timeDiff < 10) {
console.log('切换操作被忽略 - 间隔太短:', timeDiff + 'ms');
return;
}

console.log('切换侧边栏可见性 -',
'当前时间:', currentTime.toLocaleTimeString(),
'上次执行:', this.lastToggleTime ? this.lastToggleTime.toLocaleTimeString() : '无',
'时间间隔:', timeDiff + 'ms',
'当前状态:', this.isVisible,
'侧边栏可见性已切换为:', !this.isVisible
);
this.lastToggleTime = currentTime;

this.isVisible = !this.isVisible;
this.sidebar.classList.toggle('visible', this.isVisible);
this.saveState();
console.log('切换侧边栏可见性,当前状态:', this.isVisible, '侧边栏可见性已切换为:', this.isVisible);


if (this.isVisible) {
const iframe = this.sidebar.querySelector('.cerebr-sidebar__iframe');
// console.log('发送聚焦输入框消息');
if (iframe) {
iframe.contentWindow.postMessage({ type: 'FOCUS_INPUT' }, '*');
}
Expand Down
27 changes: 25 additions & 2 deletions sidebar-message-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,39 @@ window.addEventListener('message', (event) => {
let userQuestions = [];
let currentIndex = -1;

function checkCustomShortcut() {
return new Promise((resolve) => {
chrome.commands.getAll((commands) => {
const toggleCommand = commands.find(command => command.name === 'toggle_sidebar');
if (toggleCommand && toggleCommand.shortcut) {
const lastLetter = toggleCommand.shortcut.charAt(toggleCommand.shortcut.length - 1).toLowerCase();
console.log('当前设置的快捷键:', toggleCommand.shortcut, '最后一个字符:', lastLetter);
resolve(lastLetter);
} else {
resolve(null);
}
});
});
}

// 等待 DOM 加载完成
document.addEventListener('DOMContentLoaded', () => {

const input = document.querySelector('#message-input');
const chatContainer = document.querySelector('#chat-container');

// 监听输入框的键盘事件
input.addEventListener('keydown', (event) => {
input.addEventListener('keydown', async (event) => {
// 检查是否是快捷键组合(Alt+A 或 MacCtrl+A)
console.log('检查快捷键组合', event.key.toLowerCase());
const lastLetter = await checkCustomShortcut();
// console.log('检查快捷键组合', event.key.toLowerCase(), lastLetter, event.ctrlKey, navigator.userAgent, /Mac|iPod|iPhone|iPad/.test(navigator.userAgent), event.key.toLowerCase() === lastLetter);

if ((event.altKey || (event.ctrlKey && /Mac|iPod|iPhone|iPad/.test(navigator.userAgent))) && event.key.toLowerCase() === lastLetter) {
// 主动触发隐藏操作
event.preventDefault();
window.parent.postMessage({ type: 'TOGGLE_SIDEBAR' }, '*');
return;
}

// 当按下向上键且输入框为空时
if (event.key === 'ArrowUp' && event.target.value.trim() === '') {
Expand Down

0 comments on commit 27f566f

Please sign in to comment.