ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to make a very small script with greasemonkey to do some automatic things when a page loads.
The first thing I need to do is to click on a link. I've done that.
Then I need to select the 3rd element of a drop down list that appears a few moments (depending on the connection) after the link is pressed, without reloading the page (ajax or something like that). This I can't get to work, can you help me?
The select has no id or class, but I can get its xpath value. There is only one select on the page.
I've tried both setTimeOut and the DOMNodeInserted event with no effect.
Code:
// ==UserScript==
// @name test
// @namespace "www.google.com"
// @include www.mywebpage.com
// ==/UserScript==
// click:
location.assign( "javascript:" + "f(x)" + ";void(0)" );
// the rest is supposed to wait until the select appears, and then select its second element, but this doesn't work:
setTimeout("func1()",100);
window.func1 = function()
{
if (document.getElementsByTagName("select") == null){
setTimeout("func1()",100);
}
else{
var drop = document.getElementsByTagName("select");
(drop[0]).selectedIndex=0;
}
}
Yes you're right
but unfortunately it's just a typo, which means that it's not where the error comes from.
I'm just a beginner so I don't really even know how to debug js (I'm just using notepad), but if I go to the error java console I can see "'func1' is not defined."
var func1 = window.function() { ... } doesn't work either.
It still says func1 is not defined.
Please don't forget that this is not a regular js, it's greasemonkey, which adds some differences. I just picked this window.func1 = function()
from the net (the book on greasemonkey). But since it's not working, I'm opened to any suggestions.
Functions must be declared before they are used. Your script as above appears to call a function, func1() before any function is declared or described.
Code:
// the rest is supposed to wait until the select appears, and then select its second element, but this doesn't work:
setTimeout("func1()",100);
okay, thank you, it works now that I've not passed the string .
Now my next problem: actually it just selected the correct element from the drop down menu, but nothing happened.
The element is declared like this: <select onChange="f(this.options[selectedIndex].value)">
How could I fire that event ? I can't call f with
location.assign( "javascript:" + "f(x)" + ";void(0)" );
because 'this' is not the same.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.