Messenger Chat Tab Switching
Here’s the script itself. Unfortunately there’s a really long line in the middle which I tried to break up across lines, but it’s still not the prettiest thing. Currently I’m running it with Tampermonkey in Chrome so that I don’t need to load it manually every time I go to Messenger.
document.querySelector('div._5rpu').onkeydown = function(e) {
if (e.key=="Tab") {
try {
document.querySelector("li._1ht2")[((e.shiftKey)?'previous':'next')+'Sibling']
.children[0].children[0].click();
} catch (e) {}
e.preventDefault();
}
}
As with all browser scripting I started in the Chrome Elements Inspector. div._5rpu
turns out to be the actual input box element, so I added a key down listener onto it. Since shift is a modifier key, I only need to check whether the key
is Tab. Next I found the list of chats open in the sidebar, and used previousSibling
and nextSibling
(constructing the keyword depending on the shift modifier key), and found the actual a
tag to click on. The try-catch is there to handle edge cases because I’m lazy. Then prevent the default action for tab (which is to set focus to the next element). That’s really all there is to it. Real simple project, but pretty useful.