Switch Slack channels.

Use ALT+PageUp/PageDown to navigate Slack channels.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Switch Slack channels.
// @namespace    http://kevinx.net/
// @version      0.2
// @description  Use ALT+PageUp/PageDown to navigate Slack channels.
// @author       Kevin DeLoach
// @match        http://*.slack.com/*
// @match        https://*.slack.com/*
// @run-at       document-end
// @grant        none
// ==/UserScript==

$(function() {
    var $channels = $('#channel-list'),
        $target = null;

    function elAt(i) {
        return $($channels.children().get(i));
    }

    function selectedOrActive() {
        var $el = $channels.find('li.channel.underline');
        if ($el.size() > 0) {
            return $($el.get(0));
        }
        return $($channels.find('li.channel.active').get(0));
    }
    
    function move(dy) {
        var $active = selectedOrActive(),
            i = $channels.children().index($active),
            j = i + dy;
        j = Math.max(0, Math.min($channels.children().size() - 1, j));
        switchChannel(elAt(j));
    }

    function switchChannel($el) {
        $channels.find('li.channel.underline').removeClass('underline');
        $el.addClass('underline');
        $target = $el;
    }

    $(document.body).on('keyup', function(e) {
        if (e.shiftKey) {
            return;
        }
        if (e.altKey && e.keyCode === 33) {
            move(-1);
        } else if (e.altKey && e.keyCode === 34) {
            move(1);
        } else if ($target) {
            $target.find('a').trigger('click');
            $target = null;
        }
    });
});