Imaglr hotkeys: Carousel & Like/Repost

Add keyboard shortcuts: ←/→ for carousel prev/next, L for Like, R for Repost.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Imaglr hotkeys: Carousel & Like/Repost
// @version      1.0
// @description  Add keyboard shortcuts: ←/→ for carousel prev/next, L for Like, R for Repost.
// @match        https://imaglr.com/post/*
// @grant        none
// @namespace https://greasyfork.org/users/897843
// ==/UserScript==

(function () {
  'use strict';

  // Utility: return true if the element is an editable field where we should not intercept keys
  function isTypingTarget(el) {
    if (!el) return false;
    const tag = el.tagName && el.tagName.toLowerCase();
    if (tag === 'input' || tag === 'textarea') return true;
    if (el.isContentEditable) return true;
    return false;
  }

  // Utility: find a button by selector and click it if found
  function clickButton(selector) {
    try {
      const btn = document.querySelector(selector);
      if (btn) {
        // If element is disabled, do nothing
        if (btn.disabled) return false;
        // Dispatch both mouse events and call click() to cover different handlers
        btn.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, cancelable: true }));
        btn.dispatchEvent(new MouseEvent('mouseup', { bubbles: true, cancelable: true }));
        btn.click();
        return true;
      }
    } catch (e) {
      // ignore any errors
    }
    return false;
  }

  // Key handler
  function onKeyDown(e) {
    // Ignore if modifier keys are pressed
    if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;

    // Ignore if focus is in typing field
    const active = document.activeElement;
    if (isTypingTarget(active)) return;

    // Map keys
    // ArrowRight (key) or 39 (keyCode)
    // ArrowLeft or 37
    // 'L' and 'R' keys: note e.key gives 'l' or 'L' depending on layout; normalize to uppercase
    const key = e.key;

    switch (key) {
      case 'ArrowRight':
        if (clickButton('button.carousel-control.next')) {
          e.preventDefault();
        }
        break;
      case 'ArrowLeft':
        if (clickButton('button.carousel-control.prev')) {
          e.preventDefault();
        }
        break;
      default:
        // normalize letter keys to uppercase
        if (typeof key === 'string' && key.length === 1) {
          const K = key.toUpperCase();
          if (K === 'L') {
            if (clickButton('button[title="Like"]')) {
              e.preventDefault();
            }
          } else if (K === 'R') {
            if (clickButton('button[title="Repost"]')) {
              e.preventDefault();
            }
          }
        }
        break;
    }
  }

  // Attach listener when document is ready enough
  function attach() {
    window.addEventListener('keydown', onKeyDown, true);
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', attach);
  } else {
    attach();
  }
})();