Instagram: Arrow Keys for Multi-Image Posts

Makes right/left keys navigate next/previous images in multi-image posts, as well as to adjacent posts. Holding shift, right/left jumps directly between posts. Esc key closes posts.

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         Instagram: Arrow Keys for Multi-Image Posts
// @description  Makes right/left keys navigate next/previous images in multi-image posts, as well as to adjacent posts. Holding shift, right/left jumps directly between posts. Esc key closes posts.
// @match        https://www.instagram.com/*
// @version      0.7
// @author       mica
// @namespace    greatest.deepsurf.us/users/12559
// @license      MIT
// ==/UserScript==


const nextImg = () => document.querySelector('article button[aria-label="Next"]');
const prevImg = () => document.querySelector('article button[aria-label="Go back"]');
const nextPgImg = () => document.querySelector('div[role="button"] button[aria-label="Next"]');
const prevPgImg = () => document.querySelector('div[role="button"] button[aria-label="Go back"]');
const nextPost = () => document.querySelector('svg[aria-label="Next"]');
const prevPost = () => document.querySelector('svg[aria-label="Go back"]');
const closePost = () => document.querySelector('svg[aria-label="Close"]');
const openFirst = () => document.querySelector('[role="tablist"]');

document.addEventListener('keydown', event => {
    if (!location.pathname.match(/^\/$|^\/reels\/|^\/direct\/|^\/accounts\/|^\/your_activity\/|^\/.*\/saved\//g)) {
        event.stopPropagation();
        switch (true) {
            case (event.shiftKey && event.key == 'ArrowRight'):
                nextPost().parentElement.click();
                break;
            case (event.key == 'ArrowRight'):
                if (nextImg()) {
                    nextImg().click();
                } else if (nextPost()) {
                    nextPost().parentElement.click();
                } else if (openFirst()) {
                    openFirst().parentNode.nextSibling.querySelector('a').click();
                } else {
                    nextPgImg().click();
                }
                break;
            case (event.shiftKey && event.key == 'ArrowLeft'):
                prevPost().parentElement.click();
                break;
            case (event.key == 'ArrowLeft'):
                if (prevImg()) {
                    prevImg().click();
                } else if (prevPost()) {
                    prevPost().parentElement.click();
                } else {
                    prevPgImg().click();
                }
                break;
            case (event.key == 'Escape'):
                closePost().parentElement.click();
                break;
        }
    }
});