Imaglr hotkeys: Carousel & Like/Repost

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==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();
  }
})();