Disabling forum shortcuts

sqwishy

Chieftain
Joined
Jul 27, 2014
Messages
45
Hi, this forum runs some JavaScript that navigates the page on some keyup events like the number "1". On windows, it's common that I give my web browser focus by pushing `windows key + 1`. When I'm on this forum, some JavaScript will see the end of that shortcut as a keyup event and decide to switch from whatever thread I want to be viewing and go to the home page of this website. It would be super cool to have a nice way to disable that or for the shortcut to be way less promiscuous.
 
Uuhh.... to be honest, I have no idea where these controls are in the forum software, or if we can do anything about it. I didn't even know we had that :blush:
EDIT: Ah, I see, the numbers go through the nav bar...mmhhh....
EDIT2: I cannot see that anything explicitely is assigned to this though :think:.
 
That's okay. It seems I can use this uBlock Origin filter to disable the forum shortcut in my browser, forums.civfanatics.com##[data-xf-key]:remove-attr(data-xf-key)
 
This had been annoying me as well. After a year and a half of zapping the whole control bar with uBlockOrigin, I figured out I could block it with a TamperMonkey script with the following contents:

Code:
// ==UserScript==
// @name         Disable XF-Key Shortcuts in Header
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Prevents the 1, 2, 3, 4, 5, etc. keys from taking actions in the XenForo headers.  Thus allowing browser shortcuts bound to those keys to work.
// @author       Quintillus
// @match        https://*.civfanatics.com/*
// @icon         https://forums.civfanatics.com/favicon.ico
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    //Working approach - set the element's outer HTML to itself.  Disables all JS events as they aren't re-registered
    //Credit: https://stackoverflow.com/a/32809957
    document.getElementsByClassName("p-nav")[0].outerHTML = document.getElementsByClassName("p-nav")[0].outerHTML
})();

Does that uBlock Origin filter work in practice? I'd initially tried removing the data-xf-key attributes, like this:

Code:
    var nodeList = document.querySelectorAll('[data-xf-key]');
    for (var i = 0; i < nodeList.length; i++) {
        nodeList[i].removeAttribute("data-xf-key");
    }

But just removing those attributes didn't change anything, as the event had already been registered in JavaScript, and you can't remove an event without the reference to it that you get when it's created (which XenForo discards). Hence why I had to remove the entire element and re-add it; that effectively removes the event registration for the numerical keys.

---------

As a side note, this is a built-in XenForo 2 "feature". I found a thread on their forum where people were suggesting making it an optional setting, and ways to disable it, but XenForo didn't implement making it optional because that wasn't popular enough. And it appears only forum owners can make suggestions on that forum (though the public can read it). So these sorts of per-Xen-Foro-site scripts are probably the best way to block this behavior, which can be a really annoying default depending on your browser/OS preferences!
 
Oops, didn't see your reply until just now. That userscript is kinda clever. I'm surprised removing the data-xf-key doesn't work for you. It doesn't prevent the event from happening, but, it should make it not do anything. From what I remember, the event handler was attached to the body or the entire page (not each item with a shortcut) and it would run on every keyup event and look for elements with data-xf-key set that match the key that was pressed. It doesn't attach events for each element with data-xf-key; there's just one handler that looks for elements with that attribute. If you remove that attribute, the handler still runs but doesn't find anything so it doesn't navigate.

edit: Looking at it again, it seems to set up a map between shortcut keys and elements when the page loads. So the event handler uses that map instead of checking the data-xf-key attribute each time. So changing the attribute after a page load doesn't seem to do anything because the mapping was already initialized. My guess is that uBlock Origin is filtering that attribute out before the script in the page runs.
 
Last edited:
Oops, didn't see your reply until just now. That userscript is kinda clever. I'm surprised removing the data-xf-key doesn't work for you. It doesn't prevent the event from happening, but, it should make it not do anything. From what I remember, the event handler was attached to the body or the entire page (not each item with a shortcut) and it would run on every keyup event and look for elements with data-xf-key set that match the key that was pressed. It doesn't attach events for each element with data-xf-key; there's just one handler that looks for elements with that attribute. If you remove that attribute, the handler still runs but doesn't find anything so it doesn't navigate.

edit: Looking at it again, it seems to set up a map between shortcut keys and elements when the page loads. So the event handler uses that map instead of checking the data-xf-key attribute each time. So changing the attribute after a page load doesn't seem to do anything because the mapping was already initialized. My guess is that uBlock Origin is filtering that attribute out before the script in the page runs.
I am 94.5% sure that all makes sense, with the remaining 5.5% of doubt being due to lack of expertise in how uBlock Origin works. But your theory would explain why my "remove-the-data-xf-key" attempt in Tampermonkey didn't work, and I know that uBlock Origin is famous for blocking things before they're loaded, rather than modifying things after they're loaded as Greasemonkey and Tampermonkey do, which would be consistent with why it worked for you.

As my level of knowledge with both tools has increased over the past couple years, I've found them to be valuable tools for improving the usability of websites. And I don't just mean blocking ads, but general usability as well. Making margins smaller on sites with too narrow of margins, expanding embedded maps views on websites, changing relative dates ("three weeks ago") to absolute ones (July 23rd at 5:17 PM), and taking a 1-to-5 star rating that was calculated rather poorly (round 4.001 up to 5) and converting it into a numeral value (8.0/10, or 8.7 out of 10) are a few of the things I've used them for.
 
Top Bottom