Vanilla JavaScript:禁用整个网站中的所有关键组合 [英] Vanilla JavaScript: Disable all key combos whatsoever in an entire website
问题描述
有时当您使用网站时,它们包含一个或多个键盘组合或(键盘快捷键),如 Alt + Shift + S 或 CTRL + Shift + D 有些人认为,该网站(或该网站中的一个或多个网页)是独一无二的。
Sometimes when you use websites, they contain one or more keyboard combos or ("keyboard shortcuts") like Alt+Shift+S or CTRL+Shift+D that are, by some view, unique to that website (or one or more webpages in that website).
一个很好的例子就是你得到的不同的关键组合在 的>编辑页面(站在鼠标上的选项通常会显示描述其键组合的链接描述)。
A good broad example would be the different key combos you get when you are in the edit-page of a random user-page in Wikipedia (standing with the mouse on an option will usually display the link's description describing its key combo).
有时网站的一组关键组合可能会与给定操作系统的这些组合发生冲突(如果使用了组合,甚至会导致编辑错误一个罕见的错误。)
Sometimes a website's set of key combos can clash with these of a given operating system (or even cause editorial mistakes if a combo was used in a rare mistake).
是否存在暴力(和vanillic)JavaScript方式禁用
Is there a "violent" (and vanillic) JavaScript way to disable all preexisting keycombos whatsoever in an entire website? Not just one or two preexisting (specific) key combos but all of them, in one go?
对于$ c,可能是某种 $ c>循环可以做到吗?
Maybe some kind of for
loop that will do that?
我可以在Greasemonkey或Tampermonkey等浏览器脚本执行器中运行此代码。
I could run this code in some browser script-executor like Greasemonkey or Tampermonkey.
I ask this question after yet finding "satisfying" answers in this question and this question that I personally asked, and contains all sorts of codes I've tried so far.
一般来说,我认为没有人应该这样做(而且不会强迫别人这样做) Web Acc网络应用程序的可行性;我描述的问题是罕见的,处理我的个人问题。
Generally I don't think anyone should do this (and surly not forcing it on others) as it decreases Web Accessibility of web applications in general; the problem I described is rare and deals with a personal issue I have.
推荐答案
我想我会尝试通过此处描述的方法。
window.addEventListener("keydown", function (e) { e.stopPropagation(); }, true);
window.addEventListener("keyup", function (e) { e.stopPropagation(); }, true);
window.addEventListener("keypress", function (e) { e.stopPropagation(); }, true);
这会阻止所有听众注意任何按键操作。如果这太宽泛,你可以添加一些逻辑,只有在持有修饰键的情况下才会停止,例如
This would stop all listeners watching for any key presses. If that's too broad, you could add some logic to only stopPropagation if modifier keys are being held, e.g.
if (e.ctrlKey) {
e.stopPropagation();
}
修改:
以上代码适用于我用于测试的页面,但不适用于维基百科谈话页面,因为维基页面实际上并没有使用事件监听器,而是 accesskey 属性。在这种情况下,删除该属性将删除键盘快捷键:
The above code works on the page I used for testing but not on your wikipedia talk page because the wiki page isn't actually using event listeners, but rather the accesskey attribute. In this case, removing the attribute will remove the keyboard shortcut:
document.querySelectorAll("[accesskey]").forEach(function(el) {
el.removeAttribute('accesskey');
});
这篇关于Vanilla JavaScript:禁用整个网站中的所有关键组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!