content.js 654 B

1234567891011121314151617181920212223
  1. // 监听键盘按下事件
  2. let isCtrlPressed = false;
  3. let isShiftPressed = false;
  4. document.addEventListener('keydown', (event) => {
  5. if (event.ctrlKey) isCtrlPressed = true;
  6. if (event.shiftKey) isShiftPressed = true;
  7. });
  8. document.addEventListener('keyup', (event) => {
  9. if (!event.ctrlKey) isCtrlPressed = false;
  10. if (!event.shiftKey) isShiftPressed = false;
  11. });
  12. // 发送按键状态给 background.js
  13. chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  14. if (message.type === 'getKeyboardState') {
  15. sendResponse({
  16. ctrl: isCtrlPressed,
  17. shift: isShiftPressed
  18. });
  19. }
  20. });