某些网站拦截标准浏览器 UI 热键,这让我很恼火。
我使用 Firefox,当我按下正斜杠键 ('/') 时,我想搜索 当前选项卡中的文本。我不希望光标聚焦于 谷歌的搜索框位于页面顶部。这是一个典型的违规页面:
https://groups.google.com/forum/#!msg/vim_use/r3TdW9G9ms4/s-Jr3BpcnvUJ
我尝试过一些在其他网站上有效的油猴技术,例如 为 keyup、keydown 和 keypress 安装我自己的 addEventListener(),如图所示 here ,或替换prototype.addEventListener(),如here所示.
下面是一些示例代码,我尝试使用后一种技术来核对所有 div 元素上的所有关键事件监听器:
// ==UserScript==
// @description Stop google groups from highjacking the keyboard
// @include http://groups.google.com/*
// @include https://groups.google.com/*
// @run-at document-start
// @grant unsafeWindow
// ==/UserScript==
realHTMLDivElementAddEventListener = unsafeWindow.HTMLDivElement.prototype.addEventListener;
unsafeWindow.HTMLDivElement.prototype.addEventListener = function(a,b,c) {
if ( a == 'keydown' || a == 'keyup' || a == 'keypress' ) {
console.log("zapped: " + a);
console.log("zapped: this is id: " + this.id);
console.log("zapped: this is cl: " + this.className);
} else {
realHTMLDivElementAddEventListener(a,b,c);
}
}
这捕获了一些,但仍然安装了数百个事件监听器。 在“load”或“DOMContentLoaded”上解雇我的GM并没有帮助......
我实际上不确定它是一个需要切换的 DIV 元素(我还没有 当我点击“/”时能够诱使 Firebug 崩溃),但令我惊讶的是 上面的代码无法阻止所有向 DIV 添加按键事件监听器的尝试。
谷歌是否找到了摆脱 Greasemonkey 威力的方法?
最佳答案
// ==UserScript==
// @name Google Groups Key Redemption
// @namespace github.com/zanetu
// @version 0.1
// @description Prevents Google Groups from intercepting browser hotkeys such as forward slash key.
// @include /^https?\:\/\/groups\.google\.com\//
// @author zanetu
// @license GPL version 2 or any later version; http://www.gnu.org/licenses/gpl-2.0.txt
// @grant none
// @run-at document-start
// ==/UserScript==
var ael = window.addEventListener
window.addEventListener = function(type) {'keypress' != type && ael.apply(this, arguments)}
关于javascript - 使用greasemonkey从google组取回 '/' key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29732741/