使用 Greasemonkey 取消 Firefox 中的页面前进/后退热键

发布于 2024-09-03 03:38:24 字数 929 浏览 5 评论 0原文

首先是背景:

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2024-09-10 03:38:24

alert(kCode); 之后添加 return false;
大多数时候应该这样做。

可能还需要补充一下:

evt.preventDefault();
and/or
evt.stopPropagation();

这两个功能适用于大多数 FF 版本,但我不确定 Mac 是否适用。
另外,通常,您的函数将在编辑器脚本之后触发。但是,如果它之前触发,它可能会禁用光标跳转。

Add return false; after alert(kCode);
That should do it most of the time.

May also have to add:

evt.preventDefault();
and/or
evt.stopPropagation();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文