XKit-Rewritten icon indicating copy to clipboard operation
XKit-Rewritten copied to clipboard

New tweak: disable L-to-like

Open marcustyphoon opened this issue 3 years ago • 1 comments

User-facing changes

  • Adds a tweak that disables the L-to-like keyboard shortcut.

Technical explanation

The event listener that handles page-wide keyboard shortcuts are attached directly to the document element (document.addEventListener("keydown", this.onKeyDown)), so this prevents the L key event from reaching it even when no element is selected (document.activeElement is the body element).

I could inject a function that calls disableHotkeys('KeyL') and enableHotkeys('KeyL') on the big app component in the Redpop internals instead, which is sort of cuter but way more complicated and prone to breakage for no reason.

Issues this closes

n/a

marcustyphoon avatar Sep 08 '22 22:09 marcustyphoon

The fun way:

import { inject } from '../../util/inject.js';

const controlHotkeys = (funcName, ...args) => {
  const baseContainerElement = document.querySelector('#base-container');
  const reactKey = Object.keys(baseContainerElement).find(key => key.startsWith('__reactFiber'));
  let fiber = baseContainerElement[reactKey];

  while (fiber !== null) {
    if (fiber.stateNode?.[funcName] !== undefined) {
      fiber.stateNode[funcName](...args);
      return;
    } else {
      fiber = fiber.return;
    }
  }
};

const disableHotkeys = (keys) => inject(controlHotkeys, ['disableHotkeys', keys]);
const enableHotkeys = (keys) => inject(controlHotkeys, ['enableHotkeys', keys]);

export const main = async () => disableHotkeys(['KeyL']);
export const clean = async () => enableHotkeys(['KeyL']);

marcustyphoon avatar Sep 09 '22 09:09 marcustyphoon

I don't plan to review this. It would be fun to have completely configurably keyboard shortcuts, but just the shortcut for liking is too niche on its own to justify its place in Tweaks.

AprilSylph avatar Oct 01 '22 16:10 AprilSylph