Am 01.07.2011 18:18, schrieb Rudy:
Dear Chris,

thank you so far.

You're always welcome.

If you could help me with the code it would be great.
I can't seem to figure out when it triggers, with what website or does
it always do it?

The code I gave you is just the actual code. A userscript also contains a specific section at the top, which contains the information that goes with a userscript. When you decide to create a script, you'll get a form to enter that information, and it'll be inserted into the header correctly. See the next section for more detailed information.

What I now use is a bookmark, which is the following code:

javascript:document.getElementsByName('submit')[0].click();

So what you basically say is that this should be in the clickOkButton
section and then the eventlistener?

Absolutely :)

clickOKButton = function () {
document.getElementsByName('submit')[0].click();
}
window.addEventListener("load", setTimeout(clickOKButton, 2000), false);

should do the trick.

Is it possible to only activate the script when a webpage includes
try=confirm?

Yes it is. Userscripts are enabled for exact URLs or URL patterns. Best you start with navigating to the site you want to create a script for. Then, from the "Monkeymenu", select the entry "New Script". The above mentioned form will appear and ask for a script name, a namespace (that is something to group scripts together, you can set it once in the very first form, Greasemonkey will then continue to use it for every new script), and it'll also ask for the URLs to in- and exclude.

Basically, you just add each URL plaintext that you want the script to be enabled on. A * stands for "anything" (or even nothing) within the URL. To activate it a script on google.com and all country-derivations, you can either add all the URLs, or simply use "http://www.google.*"; as rule. For more detailed information see the Greasemonkey Wiki at wiki.greasespot.net.

Your rule would be something like *try=confirm* then, I guess? But pay attention to that the wildcards also mean the script is enabled for "...nexttry=confirmation..." and so on. Automatically click on a submit-button might be dangerous, so if you can narrow the set of matching URLs with more attributes (for example the domain name), I'd recommend to do it.

I know.. a lot of questions but I couldn't really find it. Tried
google but all but the answers popped up.
I have no idea how much work it is, but if you could, can you set out
the code and explain why you use certain coding?

Of course. I wasn't quite sure how detailed you wanted it. Sometimes some people just want hints which keywords they should Google for because they only understand and learn if they find out themselves how things work. Okay, let's start from the beginning.

We first set up a function to perform the action. I assume you've got at least that little experience already to know that a function is basically like a group of commands, that can be accessed with the function name, plus maybe parameters and / or a return value. I needn't explain that to the full extend, I think.

The code for the function is already available, and basically does nothing then selecting all elements with the name attribute value "submit", pick the first of it (in JS, any index starts with 0) and simulate a click on it. You may be used to define a function like "function ClickOKButton () {". This doesn't work for Greasemonkey if you use eventListeners or timeouts, but explaining why it does not goes very deeply into the complicated areas of programming and object modelling and stuff. I think that's too complex to explain, so basically just do as I posted and it'll work. ;)

The really interesting point is the last line. Let's start with basics, "event listeners" in common.

As the name suggests, event listeners "listen" to events. Opposed to, say, the onClick="..." in an input tag, you can't see that anywhere in the code except where the listener is defined. Basically, nearly every tag can have one or more listeners (to cut it short). But what are those "events" anyway that the listeners detect? Events can be any interaction with the page by the mouse or the keyboard, for example a click or a double-click, mouse movements, and there's one for keypress, one for keydown and keyup, and so on. But there are also some events that are "fired" (this is the term for the browser broadcasting "hey, event so-and-so occurred") from within the page, like when the page is readily loaded or when an error occurs.

Your script needs the content to be loaded beforehand, which is why we use the "Load" event, that is fired as soon as the content completely ready and visible for the user from the primary entered code (there might be some scripts that later change the code, but that doesn't count for the "load" event). Just assume "the load event is fired as soon as the browser is ready with processing the code on the page". Event listeners are added and removed from the elements by the function "addEventListener" and "removeEventListener". While we needn't really remove the load listener as that event is fired only once anyway, I'll focus on the "add" here.

The function expects three parameters: the name of the event, which is "load" in this case; next the function to call when the event was fired and "heard" (to stay in terms of listening); and last a boolean. Again, the boolean has to be set but is only relevant for advanced scripters. Usually, both false and true will have the same result for the "load" event anyway. If you're really interested in the stuff, I recommend to keep in mind the keywords "capturing" and "bubbling", and to read http://www.quirksmode.org/js/events_order.html to see what the boolean is for.

Okay, now that we've got that clear, (y)our script is almost ready. We've got the function with the code in it, and decided to call it when the window says "I'm ready with loading". Now, it'd call the function directly. You said you want to wait two seconds, so we delay the execution of our ClickOKButton function with setTimeout, set to a delay of 2000 milliseconds, the desired two seconds.

By the way, I assume those two seconds were meant to be the "wait until the page is loaded to be sure the button the script tries to click on will already exist". Now that we use our event listener, we can maybe omit the timeout, because the event isn't fired unless the button is available for the code. If the page takes 5 seconds to load, the script will be executed after 5 seconds, if the page takes 0.5 seconds to load, ... I think you get the point.

However, as explained above, the script might unintentionally be executed for pages that don't have any submit button at all, maybe. As it's a single command function, it doesn't matter if the execution of that command fails and the function is thereby aborted at that command. But if there were more commands behind the click, you should wrap the click command in an if conditional to catch the case that "document.getElementsByName("submit")[0]" does not match any element, therefore is undefined and therefore does not have any sub-function ".click()".

if (document.getElementsByName("submit")[0]) { document.getElementsByName("submit")[0].click() };

I don't like it that way though, because that'll make the code search all matching elements twice. Imagine you've got thousands of name="submit" elements, you'd create the list two times. That's why I put the result of the getElements... into a variable and work with that variable from then on:

var Button = document.getElementsByName("submit")[0];
if (Button) { Button.click() };

Don't be afraid of using many variables within a function. They are only kept temporarily for that single call until the function is done. Then, they are deleted again.

Thank you!

P.S. It is for a friend of mine who has RSI. This will help him to
skip the submit sections.

I hope I could help you. I know this is a mailing list, so I know that many people with more knowledge than I have might read this. Maybe, I didn't explain anything 100% correctly, and maybe I used a term do describe something although that term usually describes something different. I hope nobody minds that, I am no native speaker, so I have to translate my knowledge (luckily I don't have to use Google Translate for it, that wouldn't be understandable at all for such topics I guess).

Let me know if you've got further questions. Feel free to ask, also directly instead of via mailing list, if you want to. It's always a balancing act to decide between "annoys readers with another mailing-list-mail" and "it might be helpful for someone who later reads the thread and has the same problem". I do not mind how you decide to answer.

Good luck with your first script.

Chris


--
You received this message because you are subscribed to the Google Groups 
"greasemonkey-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/greasemonkey-users?hl=en.

Reply via email to