LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2011, 09:37 PM   #1
katea
LQ Newbie
 
Registered: Feb 2011
Posts: 21

Rep: Reputation: 1
javascript


hello,

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;
}
}
Thanks.

Last edited by katea; 02-09-2011 at 09:41 PM.
 
Old 02-09-2011, 11:40 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Surely the required selected index will be 2 ?
 
Old 02-10-2011, 01:29 AM   #3
katea
LQ Newbie
 
Registered: Feb 2011
Posts: 21

Original Poster
Rep: Reputation: 1
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."

Last edited by katea; 02-10-2011 at 01:34 AM.
 
Old 02-10-2011, 02:37 PM   #4
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Well define func1.

Here's a tip :
window.func1 isn't the declaration you think it is.

func1 = WHAT ?

What is the type of the variable ?
Where have you said what it is supposed to be ?

Maybe var func1 = window.function() .... ?

Last edited by smoker; 02-10-2011 at 02:42 PM.
 
Old 02-10-2011, 04:11 PM   #5
katea
LQ Newbie
 
Registered: Feb 2011
Posts: 21

Original Poster
Rep: Reputation: 1
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.

Please help me.
Thanks.
 
Old 02-11-2011, 02:09 PM   #6
katea
LQ Newbie
 
Registered: Feb 2011
Posts: 21

Original Poster
Rep: Reputation: 1
I've tried quite a lot of things but none worked. Is there someone here who knows a bit about greasemonkey?
 
Old 02-12-2011, 01:48 AM   #7
katea
LQ Newbie
 
Registered: Feb 2011
Posts: 21

Original Poster
Rep: Reputation: 1
Can I get help on this please ?
I'm really a beginner but I would very much appreciate this to work for my personal use.
 
Old 02-12-2011, 04:00 AM   #8
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
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);
 
Old 02-12-2011, 09:51 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Why do you pass a string to setTimeout? The following works for me under Greasemonkey:

Code:
window.func1 = function()
{
    alert("called func1");
}
setTimeout(window.func1,100);
 
Old 02-12-2011, 09:02 PM   #10
katea
LQ Newbie
 
Registered: Feb 2011
Posts: 21

Original Poster
Rep: Reputation: 1
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.

Thanks.
 
Old 02-13-2011, 07:00 PM   #11
katea
LQ Newbie
 
Registered: Feb 2011
Posts: 21

Original Poster
Rep: Reputation: 1
Can someone help me with that new situation please?
Thanks
 
Old 02-16-2011, 06:43 PM   #12
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
I don't understand the situation. Can you post an entire script and html page so I can reproduce the problem?
 
  


Reply

Tags
greasemonkey, javascript



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for Textbrowser with JavaScript Support or Elink Javascript Support rohezal Linux - Software 4 09-01-2009 01:02 PM
JavaScript dhanhar Linux - Newbie 8 09-28-2007 02:03 AM
Ubuntu Edgy/Firefox 2/Javascript - Firefox closes accessing websites with Javascript Interdictor Ubuntu 8 11-02-2006 11:58 AM
Need help on Javascript jun_tuko Programming 3 01-16-2006 03:45 AM
javascript in <td>? akaash Programming 0 03-18-2004 12:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration