2024-06-04 04:05:40 +02:00
|
|
|
/* All selectors that should act as interactables / keyboard buttons by default */
|
2024-06-06 02:48:06 +02:00
|
|
|
const interactableSelectors = [
|
|
|
|
'.custom_interactable',
|
|
|
|
'.interactable',
|
|
|
|
'.menu_button',
|
|
|
|
'.right_menu_button',
|
|
|
|
'.drawer-icon',
|
|
|
|
'.inline-drawer-icon',
|
|
|
|
'.paginationjs-pages li a',
|
|
|
|
'.group_select',
|
|
|
|
'.character_select',
|
|
|
|
'.bogus_folder_select',
|
|
|
|
'.avatar-container',
|
|
|
|
'.tag .tag_remove',
|
|
|
|
'.bg_example',
|
|
|
|
'.bg_example .bg_button',
|
|
|
|
'#options a',
|
|
|
|
'#extensionsMenu div:has(.extensionsMenuExtensionButton)',
|
|
|
|
'.mes_buttons .mes_button',
|
|
|
|
'.extraMesButtons>div:not(.mes_button)',
|
|
|
|
'.stscript_btn'
|
|
|
|
];
|
2024-06-03 02:52:54 +02:00
|
|
|
|
2024-06-04 04:05:40 +02:00
|
|
|
export const INTERACTABLE_CONTROL_CLASS = 'interactable';
|
|
|
|
export const CUSTOM_INTERACTABLE_CONTROL_CLASS = 'custom_interactable';
|
2024-06-03 02:52:54 +02:00
|
|
|
|
2024-06-04 04:05:40 +02:00
|
|
|
export const NOT_FOCUSABLE_CONTROL_CLASS = 'not_focusable';
|
|
|
|
export const DISABLED_CONTROL_CLASS = 'disabled';
|
2024-06-03 02:52:54 +02:00
|
|
|
|
|
|
|
/**
|
2024-06-04 04:05:40 +02:00
|
|
|
* An observer that will check if any new interactables are added to the body
|
2024-06-03 02:52:54 +02:00
|
|
|
* @type {MutationObserver}
|
|
|
|
*/
|
|
|
|
const observer = new MutationObserver(mutations => {
|
|
|
|
mutations.forEach(mutation => {
|
|
|
|
if (mutation.type === 'childList') {
|
|
|
|
mutation.addedNodes.forEach(node => {
|
|
|
|
if (node.nodeType === Node.ELEMENT_NODE && node instanceof Element) {
|
2024-06-04 04:05:40 +02:00
|
|
|
// Check if the node itself is an interactable
|
|
|
|
if (isKeyboardInteractable(node)) {
|
|
|
|
makeKeyboardInteractable(node);
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
2024-06-04 04:05:40 +02:00
|
|
|
// Check for any descendants that might be an interactable
|
|
|
|
const interactables = getAllInteractables(node);
|
|
|
|
makeKeyboardInteractable(...interactables);
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (mutation.type === 'attributes') {
|
|
|
|
const target = mutation.target;
|
|
|
|
if (mutation.attributeName === 'class' && target instanceof Element) {
|
2024-06-04 04:05:40 +02:00
|
|
|
if (isKeyboardInteractable(target)) {
|
|
|
|
makeKeyboardInteractable(target);
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2024-06-04 04:05:40 +02:00
|
|
|
* Registers an interactable class (for example for an extension) and makes it keyboard interactable.
|
2024-06-03 02:52:54 +02:00
|
|
|
* Optionally apply the 'not_focusable' and 'disabled' classes if needed.
|
|
|
|
*
|
2024-06-04 04:05:40 +02:00
|
|
|
* @param {string} interactableSelector - The CSS selector for the interactable (Supports class combinations, chained via dots like <c>tag.actionable</c>, and sub selectors)
|
|
|
|
* @param {object} [options={}] - Optional settings for the interactable
|
|
|
|
* @param {boolean} [options.disabledByDefault=false] - Whether interactables of this class should be disabled by default
|
|
|
|
* @param {boolean} [options.notFocusableByDefault=false] - Whether interactables of this class should not be focusable by default
|
2024-06-03 02:52:54 +02:00
|
|
|
*/
|
2024-06-04 04:05:40 +02:00
|
|
|
export function registerInteractableType(interactableSelector, { disabledByDefault = false, notFocusableByDefault = false } = {}) {
|
|
|
|
interactableSelectors.push(interactableSelector);
|
2024-06-03 02:52:54 +02:00
|
|
|
|
2024-06-04 04:05:40 +02:00
|
|
|
const interactables = document.querySelectorAll(interactableSelector);
|
2024-06-03 02:52:54 +02:00
|
|
|
|
|
|
|
if (disabledByDefault || notFocusableByDefault) {
|
2024-06-04 04:05:40 +02:00
|
|
|
interactables.forEach(interactable => {
|
|
|
|
if (disabledByDefault) interactable.classList.add(DISABLED_CONTROL_CLASS);
|
|
|
|
if (notFocusableByDefault) interactable.classList.add(NOT_FOCUSABLE_CONTROL_CLASS);
|
2024-06-03 02:52:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-04 04:05:40 +02:00
|
|
|
makeKeyboardInteractable(...interactables);
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-04 04:05:40 +02:00
|
|
|
* Checks if the given control is a keyboard-enabled interactable.
|
2024-06-03 02:52:54 +02:00
|
|
|
*
|
2024-06-04 04:05:40 +02:00
|
|
|
* @param {Element} control - The control element to check
|
|
|
|
* @returns {boolean} Returns true if the control is a keyboard interactable, false otherwise
|
2024-06-03 02:52:54 +02:00
|
|
|
*/
|
2024-06-04 04:05:40 +02:00
|
|
|
export function isKeyboardInteractable(control) {
|
|
|
|
// Check if this control matches any of the selectors
|
|
|
|
return interactableSelectors.some(selector => control.matches(selector));
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-04 04:05:40 +02:00
|
|
|
* Makes all the given controls keyboard interactable and sets their state.
|
|
|
|
* If the control doesn't have any of the classes, it will be set to a custom-enabled keyboard interactable.
|
2024-06-03 02:52:54 +02:00
|
|
|
*
|
2024-06-04 04:05:40 +02:00
|
|
|
* @param {Element[]} interactables - The controls to make interactable and set their state
|
2024-06-03 02:52:54 +02:00
|
|
|
*/
|
2024-06-04 04:05:40 +02:00
|
|
|
export function makeKeyboardInteractable(...interactables) {
|
|
|
|
interactables.forEach(interactable => {
|
|
|
|
// If this control doesn't have any of the classes, lets say the caller knows this and wants this to be a custom-enabled keyboard control.
|
|
|
|
if (!isKeyboardInteractable(interactable)) {
|
|
|
|
interactable.classList.add(CUSTOM_INTERACTABLE_CONTROL_CLASS);
|
2024-06-03 06:19:41 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 04:05:40 +02:00
|
|
|
// Just for CSS styling and future reference, every keyboard interactable control should have a common class
|
|
|
|
if (!interactable.classList.contains(INTERACTABLE_CONTROL_CLASS)) {
|
|
|
|
interactable.classList.add(INTERACTABLE_CONTROL_CLASS);
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
2024-06-08 07:13:52 +02:00
|
|
|
/**
|
|
|
|
* Check if the element or any parent element has 'disabled' or 'not_focusable' class
|
|
|
|
* @param {Element} el
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
const hasDisabledOrNotFocusableAncestor = (el) => {
|
|
|
|
while (el) {
|
|
|
|
if (el.classList.contains(NOT_FOCUSABLE_CONTROL_CLASS) || el.classList.contains(DISABLED_CONTROL_CLASS)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
el = el.parentElement;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2024-06-03 02:52:54 +02:00
|
|
|
// Set/remove the tabindex accordingly to the classes. Remembering if it had a custom value.
|
2024-06-08 07:13:52 +02:00
|
|
|
if (!hasDisabledOrNotFocusableAncestor(interactable)) {
|
2024-06-04 04:05:40 +02:00
|
|
|
if (!interactable.hasAttribute('tabindex')) {
|
|
|
|
const tabIndex = interactable.getAttribute('data-original-tabindex') ?? '0';
|
|
|
|
interactable.setAttribute('tabindex', tabIndex);
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-04 04:05:40 +02:00
|
|
|
interactable.setAttribute('data-original-tabindex', interactable.getAttribute('tabindex'));
|
|
|
|
interactable.removeAttribute('tabindex');
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-08 07:13:52 +02:00
|
|
|
|
2024-06-03 02:52:54 +02:00
|
|
|
/**
|
2024-06-04 04:05:40 +02:00
|
|
|
* Initializes the focusability of controls on the given element or the document
|
2024-06-03 02:52:54 +02:00
|
|
|
*
|
2024-06-04 04:05:40 +02:00
|
|
|
* @param {Element|Document} [element=document] - The element on which to initialize the interactable state. Defaults to the document.
|
2024-06-03 02:52:54 +02:00
|
|
|
*/
|
2024-06-04 04:05:40 +02:00
|
|
|
function initializeInteractables(element = document) {
|
|
|
|
const interactables = getAllInteractables(element);
|
|
|
|
makeKeyboardInteractable(...interactables);
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-04 04:05:40 +02:00
|
|
|
* Queries all interactables within the given element based on the given selectors and returns them as an array
|
2024-06-03 02:52:54 +02:00
|
|
|
*
|
2024-06-04 04:05:40 +02:00
|
|
|
* @param {Element|Document} element - The element within which to query the interactables
|
|
|
|
* @returns {HTMLElement[]} An array containing all the interactables that match the given selectors
|
2024-06-03 02:52:54 +02:00
|
|
|
*/
|
2024-06-04 04:05:40 +02:00
|
|
|
function getAllInteractables(element) {
|
|
|
|
// Query each selector individually and combine all to a big array to return
|
|
|
|
return [].concat(...interactableSelectors.map(selector => Array.from(element.querySelectorAll(`${selector}`))));
|
2024-06-03 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-04 04:05:40 +02:00
|
|
|
* Handles keydown events on the document to trigger click on Enter key press for interactables
|
2024-06-03 02:52:54 +02:00
|
|
|
*
|
|
|
|
* @param {KeyboardEvent} event - The keyboard event
|
|
|
|
*/
|
|
|
|
function handleGlobalKeyDown(event) {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
if (!(event.target instanceof HTMLElement))
|
|
|
|
return;
|
|
|
|
|
2024-06-04 04:05:40 +02:00
|
|
|
// Only count enter on this interactable if no modifier key is pressed
|
2024-06-03 02:52:54 +02:00
|
|
|
if (event.altKey || event.ctrlKey || event.shiftKey)
|
|
|
|
return;
|
|
|
|
|
2024-06-04 04:05:40 +02:00
|
|
|
// Traverse up the DOM tree to find the actual interactable element
|
2024-06-03 02:52:54 +02:00
|
|
|
let target = event.target;
|
2024-06-04 04:05:40 +02:00
|
|
|
while (target && !isKeyboardInteractable(target)) {
|
2024-06-03 02:52:54 +02:00
|
|
|
target = target.parentElement;
|
|
|
|
}
|
|
|
|
|
2024-06-04 04:05:40 +02:00
|
|
|
// Trigger click if a valid interactable is found and it's not disabled
|
|
|
|
if (target && !target.classList.contains(DISABLED_CONTROL_CLASS)) {
|
|
|
|
console.debug('Triggering click on keyboard-focused interactable control via Enter', target);
|
2024-06-03 02:52:54 +02:00
|
|
|
target.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-04 04:05:40 +02:00
|
|
|
* Initializes several keyboard functionalities for ST
|
2024-06-03 02:52:54 +02:00
|
|
|
*/
|
|
|
|
export function initKeyboard() {
|
|
|
|
// Start observing the body for added elements and attribute changes
|
|
|
|
observer.observe(document.body, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ['class']
|
|
|
|
});
|
|
|
|
|
2024-06-04 04:05:40 +02:00
|
|
|
// Initialize interactable state for already existing controls
|
|
|
|
initializeInteractables();
|
2024-06-03 02:52:54 +02:00
|
|
|
|
|
|
|
// Add a global keydown listener
|
|
|
|
document.addEventListener('keydown', handleGlobalKeyDown);
|
|
|
|
}
|