switch between last two discord channels using caps
// ==UserScript==
// @name Discord channel switcher
// @namespace http://tampermonkey.net/
// @version 1.0
// @description switch between last two discord channels using caps
// @author You
// @match https://discord.com/channels/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
let channelHistory = [];
function getCurrentChannelPath() {
return window.location.pathname;
}
function navigateToChannel(path) {
if (window.location.pathname !== path) {
window.history.pushState({}, '', path);
window.dispatchEvent(new PopStateEvent('popstate'));
}
}
function updateChannelHistory() {
const currentPath = getCurrentChannelPath();
if (channelHistory.length === 0 || channelHistory[channelHistory.length - 1] !== currentPath) {
channelHistory.push(currentPath);
if (channelHistory.length > 2) {
channelHistory.shift();
}
}
}
updateChannelHistory();
window.addEventListener('popstate', updateChannelHistory);
window.addEventListener('pushstate', updateChannelHistory);
window.addEventListener('replacestate', updateChannelHistory);
const originalPushState = history.pushState;
history.pushState = function () {
originalPushState.apply(this, arguments);
window.dispatchEvent(new Event('pushstate'));
};
const originalReplaceState = history.replaceState;
history.replaceState = function () {
originalReplaceState.apply(this, arguments);
window.dispatchEvent(new Event('replacestate'));
};
window.addEventListener('keydown', function (e) {
if (e.code === 'CapsLock') {
e.preventDefault();
e.stopPropagation();
if (channelHistory.length === 2) {
const currentPath = getCurrentChannelPath();
const targetPath = channelHistory.find(path => path !== currentPath);
if (targetPath) {
navigateToChannel(targetPath);
}
}
}
}, true);
})();