1

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.)

quintopia
  • 21
  • 3
  • What do you mean by auto-highlight? Do you mean the different color of selected text? If so, is it enough to change the appearance or do you want to stop scripts from selecting? Or do you mean the spelling-check feature? Something else? Overriding the base `select()` function is not recommended. – Brock Adams Apr 04 '11 at 04:24
  • Seems to be the same as the question "Prevent selection in HTML" http://stackoverflow.com/questions/2326004/prevent-selection-in-html – RobG Apr 04 '11 at 05:20
  • @Brock I want to stop scripts from selecting. @RobG That looks to be about preventing the end user from selecting, which I do not want to do. I only want to prevent scripts from selecting. – quintopia Apr 04 '11 at 15:02
  • Actually, if it is possible to select text from CSS, I'd like to prevent that too, but I doubt I can do that from Greasemonkey. – quintopia Apr 04 '11 at 15:10

1 Answers1

0

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.

kwah
  • 1,149
  • 1
  • 13
  • 27