First of all, I'm not a noob on javascript (I know the basics), but I need your help.
What i need, is to make the script to find a link, and click it. In example...
There is a page, with 10 links, everytime you load the page, the links changes (it starts like normal, but has diferent endings), so i need the script to find the link that i need, and click it.
It would be great if you help me
Easiest way would be to assign IDs to your links that will remain unchanged and the script would then use getElementById("idoflink") and click on that. Hope this helps.
In which case it would be nice if we could see the code we have to work on. I mean, if it does assign IDs already, that can be used... or if it assigns names to the links, etc.
Going through all the childnodes proved to be rather annoying, but in the end:
Code:
<script type="text/javascript">
function selectlink() {
p = document.body.childNodes;
small = p[5].childNodes;
links = small[0].childNodes;
clicky = links[3].href;
window.location=clicky;
}
</script>
This essentially points the browser to the link, called x40 in the code you provided me. There probably is a better way to go about it (with a loop that runs through the entire child nodes list there and looks for a specific link that is called... "Alksnio" for an example, but... this should do the trick, provided the link you need to click has the same position every time the web page is generated) Give it a whirl, see how it works for you. Maybe someone else will give a better solution in the mean-time.
<script type="text/javascript">
function selectlink() {
p = document.getElementsByTagName("a")
clicky = p[1].href;
window.location = clicky;
}
</script>
All in all, choosing which link to go to is in the line "clicky = p[1].href;". Just alter the number there to point to the specific link. This would work... but once again depends on the link being in the same position all the time. As to making it actually do what it's supposed to... I guess assign an onclick event to a button that will trigger the function like this:
<script type="text/javascript">
window.onload = function selectlink() {
p = document.getElementsByTagName("a")
clicky = p[1].href;
window.location = clicky;
}
</script>
This would force it to follow the link when the page loads. Once again, it relies on the link you want followed being in the same location all the time, and ofcourse, altering the number in "clicky = p[1].href;" will designate the specific link. Hope this helps.
One major problem that I see in your code (marked in red):
Code:
// ==UserScript==
// @name name
// @namespace namespace
// @include http://ms.miestas.biz/miskas.php?w=&nick=plytas&pass=2a6848e1cb92e0985dabc65cbcd1b52f&id=med
// ==/UserScript==
window.onload = function selectlink() {
p = document.getElementsByTagName("Alksnio")
clicky = p[2].href;
window.location = clicky;
}
There is a reason for the "a" tag - it points to anchor elements, whereas there is no "Alksnio" tag in HTML, so you're essentially... selecting nothing. And from that point on... the entire script would break and even get a pretty error in FireBug.