Instagram: Arrow Key Navigation for Multi-Image Posts

Right/left keys will go to the next/previous image within a multi-image post, as well as between posts. While holding the shift key, right/left only jumps between posts (the current default behavior)

As of 2024-01-18. See the latest version.

// ==UserScript==
// @name         Instagram: Arrow Key Navigation for Multi-Image Posts
// @description  Right/left keys will go to the next/previous image within a multi-image post, as well as between posts. While holding the shift key, right/left only jumps between posts (the current default behavior)
// @match        https://www.instagram.com/*
// @version      0.2
// @namespace    greatest.deepsurf.us/users/12559
// @license      MIT
// ==/UserScript==

document.addEventListener('keydown', (event) => {
    event.stopPropagation();
    if (event.shiftKey && event.key === 'ArrowRight') {
        document.querySelector('svg[aria-label="Next"]').closest('button').click();
    } else if (event.key === 'ArrowRight') {
        if (document.querySelector('button[aria-label="Next"]')) {
            document.querySelector('button[aria-label="Next"]').click();
        } else {
            document.querySelector('svg[aria-label="Next"]').closest('button').click();
        }
    } else if (event.shiftKey && event.key === 'ArrowLeft') {
        document.querySelector('svg[aria-label="Go Back"]').closest('button').click();
    } else if (event.key === 'ArrowLeft') {
        if (document.querySelector('button[aria-label="Go Back"]')) {
            document.querySelector('button[aria-label="Go Back"]').click();
        } else {
            document.querySelector('svg[aria-label="Go Back"]').closest('button').click();
        }
    }
}, true);