如何从 Greasemonkey 脚本重写 DOM 方法?

发布于 2024-10-30 01:47:03 字数 425 浏览 0 评论 0原文

我想禁用所有网页通过greasemonkey 脚本(适用于FF3.6)自动突出显示文本输入表单字段内容的功能。这是我的第一次尝试:

// ==UserScript==
// @name           Prevent Auto-highlight
// @namespace      quintopia
// ==/UserScript==
HTMLInputElement.prototype.select = function() {
    //do nothing
}

但这不起作用。是因为它在页面的其余部分之后加载,所以在创建输入字段后不会应用到输入字段吗?我是否需要手动循环 DOM、找到每个输入字段并手动覆盖其选择才能使其正常工作? (我没有尝试过这个,因为我不想这样做,除非它确实是最好的方法。我问是为了学习做这类事情的“最佳”或“标准”方法。)

I want to disable the ability of all webpages to auto-highlight the contents of text input form fields via a greasemonkey script (for FF3.6). This is my first attempt:

// ==UserScript==
// @name           Prevent Auto-highlight
// @namespace      quintopia
// ==/UserScript==
HTMLInputElement.prototype.select = function() {
    //do nothing
}

But this doesn't work. Is it because it loads after the rest of the page and so doesn't get applied to the input fields after they are created? Do I need to manually loop over the DOM, find every input field, and override its select manually to get this to work? (I haven't tried this because I don't want to do it this way unless it truly is the best way. I ask in order to learn the "best" or "standard" way of doing this sort of thing.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

橘亓 2024-11-06 01:47:03

我认为您想要做的是在单击它时停止自动选择整个输入字段?

使用 if() 过滤器向整个页面添加单击事件(或 onFocus?),以测试单击的目标是否是输入字段(以节省循环并创建潜在的大量事件处理程序)你所寻求的。下一部分可以根据您的意愿简单或优雅。

简单的方法是从事件处理程序中触发 onTimeout() (延迟 100 毫秒?)并将光标移动到文本字段的末尾。这是一个竞争条件(取决于页面代码的运行速度比计时器允许的速度快),但这应该适用于大多数站点。

更复杂的方法是检测触发单击事件时鼠标在输入字段中的位置,并相应地放置光标,和/或改为特定于站点并覆盖/专门抵消您要使用的确切页面/站点上的代码就可以了。

I think what you're trying to do is stop the whole input field being selected automatically when you click on it?

Adding a click event (or onFocus?) to the whole page with an if() filter to test whether the target of the click is an input field (to save having to loop through and creating a potentially huge number of event handlers) would do what you seek. The next part can be as simple or elegant as you wish.

The simple method would be to have an onTimeout() trigger (100ms delay?) from the event handler and move the cursor to the end of the text field. This is a race condition (depending on the page's code running faster than the timer allows) but this should work across most sites.

More sophisticated would detect where in the input field the mouse was when the click event was triggered and place the cursor accordingly, and/or would instead become site-specific and override / specifically counteract the code on the exact page / site you want to use it on.

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