使用 Greasemonkey 取消 Firefox 中的页面前进/后退热键
首先是背景:
在 Mac OS X 10.5.8 上的 Firefox 3.6.3 中,当在标准中输入文本时,Command+LeftArrow 和 Command+RightArrow 的热键组合将光标分别跳转到当前行的开头/结尾。然而,当使用CKEditor、FCKEditor和YUI Editor时,Firefox似乎并没有完全识别出它是一个文本区域。相反,它会返回到这些热键的默认功能,即在浏览器历史记录中后退/前进。发生这种情况后,当您返回页面时,编辑器中的文本也会被清除,从而很容易丢失正在处理的任何内容。
我正在尝试编写一个greasemonkey 脚本,我可以用它来捕获事件并防止页面向前/向后跳转被执行。到目前为止,我已经能够在 GreaseMonkey 中将以下内容用作 .user.js 脚本来查看事件:
document.addEventListener('keypress', function (evt) {
// grab the meta key
var isCmd = evt.metaKey;
// check to see if it is pressed
if(isCmd)
{
// if so, grab the key code;
var kCode = evt.keyCode;
if(kCode == 37 || kCode == 39)
{
alert(kCode);
}
}
}, 错误的 );
安装/启用后,按命令+左|右箭头键会弹出带有相应代码的警报,但一旦对话框关闭,浏览器就会执行页面向前/向后移动。我尝试使用 evt.keyCode = 0 设置新代码,但这不起作用。
那么,问题是,是否可以更新 Greasemonkey 脚本以防止后退/前进页面移动?
(注意:我也愿意接受其他解决方案。不一定是 Greasemonkey,这只是我尝试过的方向。真正的目标是能够禁用前进/后退热键功能。)
First the background:
In Firefox 3.6.3 on Mac OS X 10.5.8 when entering text into a standard the hotkey combination of Command+LeftArrow and Command+RightArrow jump the cursor to the start/end of the current line, respectively. However, when using CKEditor, FCKEditor and YUI Editor, Firefox does not seem to completely recognize that it's a text area. Instead, it drops back to the default function for those hotkeys which is to move back/forward in the browser history. After this occurs, the text in the editor is also cleared when you return to the page making it very easy to loose whatever is being worked on.
I'm attempting to write a greasemonkey script that I can use to capture the events and prevent the page forward/back jumps from being executed. So far, I've been able to see the events with the following used as a .user.js script in GreaseMonkey:
document.addEventListener('keypress', function (evt) {
// grab the meta key
var isCmd = evt.metaKey;
// check to see if it is pressed
if(isCmd)
{
// if so, grab the key code;
var kCode = evt.keyCode;
if(kCode == 37 || kCode == 39)
{
alert(kCode);
}
}
},
false
);
When installed/enabled, pressing command+left|right arrow key pops an alert with the respective code, but as soon as the dialog box is closed, the browser executes the page forward/back move. I tried setting a new code with evt.keyCode = 0, but that didn't work.
So, the question is, can this Greasemonkey script be updated so that it prevents the back/forward page moves?
(NOTE: I'm open to other solutions as well. Doesn't have to be Greasemonkey, that's just the direction I've tried. The real goal is to be able to disable the forward/back hotkey functionality.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
alert(kCode);
之后添加return false;
大多数时候应该这样做。
可能还需要补充一下:
这两个功能适用于大多数 FF 版本,但我不确定 Mac 是否适用。
另外,通常,您的函数将在编辑器脚本之后触发。但是,如果它之前触发,它可能会禁用光标跳转。
Add
return false;
afteralert(kCode);
That should do it most of the time.
May also have to add:
These two functions work on most FF editions, but I'm not sure about the Mac.
Also, usually, your func will fire after the editor script. But, if it fires before, it could disable that cursor jump.