RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Control Firefox with C#

Scriptiz

Sorceror
Control Firefox with C#

Hello,

I'm trying to "control" firefox via a C# program. Is it possible?

Actually I can just start the Process firefox.exe but then I don't know how to change the Url of this process, how to get the Url, get the html source code of the actually visited page in this firefox process, ...

Can you help me? Is it a way to do that? I hope :)


ps: I'm french so excuse my poor English.
ps2: I went for 5days so I'll read your answers in only 5days :)
 

daat99

Moderator
Staff member
Scriptiz;747059 said:
Hello,

I'm trying to "control" firefox via a C# program. Is it possible?

Actually I can just start the Process firefox.exe but then I don't know how to change the Url of this process, how to get the Url, get the html source code of the actually visited page in this firefox process, ...

Can you help me? Is it a way to do that? I hope :)


ps: I'm french so excuse my poor English.
ps2: I went for 5days so I'll read your answers in only 5days :)

1. To open Firefox in a specific URL you can just pass the url as a parameter to Firefox (run this in Start->Run: "firefox.exe http://www.runuo.com/forums/c/89428-control-firefox-c.html#post747059")

2. It's very likely that there is a lot better and easier way then this! but...
To get the source code you can "create" your own c# web explorer.
There should be a c# library that can "open" a url and "download" the source code so it can be displayed on the screen (look at ConnectUO or many other c# applications that integrate a small web browser into them).
 

Scriptiz

Sorceror
I knew those solutions, but the problem is that I ave to invoke a javascript in my page, and to find the parameters of this javascript function.

Actually with the webBrowser control available, I can navigate to the page I want, get the parameters of the javascript function by some functions wich analyse the html-code. But then when i change the Url of my webBrowser in : "javascript: the_function_searched("param1", "other_param");" the browser don't execute the javascript function.

In firefox, if I look manually in the code for this function, I can change the url in this way to invoke the javascript function. But in IE, or my webBrowser control wich is based on IE, the manually way doesn't work neither...

I find a way to solve this problem by the webBrowser wich got à Document instance wich permit to call the InvokeScript([script], [array of params]) but it doesn't work too...

Another way I think, is to user the firefox.exe to open new url, but then how can I get the source code of my page, because in this page, I'm identified by a cookie, session variables, and a get variable wich contain a hash code. Thus at each identification on the website by a different navigator, the hash is changed, and I can't get farther. So the only way is to stay in the same navigator, but my webBrowser don't permit me to invoke scripts... so I really have to find a way to totally control the "moves" of firefox or to invoke the javascripts functions in this webBrowser controle.

I'm searching for a solution many hours, an I got headaches now :(

Thanks for your help.
 

Jeff

Lord
Unless firefox's API is open(which i believe it is not) you cant really do this with C#, and if it is even remotely possible, i can guarentee its not a simple solution.
 

arul

Sorceror
Mozilla uses its own window class implementation, though I believe that classic get-handle-send-message approach should work.

In C# this of course requires P/Invoke of a few user32.dll functions.
 

Scriptiz

Sorceror
Thanks arul for this answer, but how do I do that?

Do you have a sample or a piece of code wich will be more easy to understand :confused:

Thanks a lot.
 

ursow

Wanderer
Scriptiz;754922 said:
Thanks arul for this answer, but how do I do that?

Do you have a sample or a piece of code wich will be more easy to understand :confused:

Thanks a lot.

sorry for gravedigging but i hope this helps somebody
i've been trying to solve this, using user32.dll to manipulate clicks and if u have some extention in firefox u can configure it to edit you javascript
here is what it does: with an alredy open window, it "alternates" to its window and clicks or types what i want.
here is the code:

public partial class Form1 : Form
{
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);

// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

//deals with mouse
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
// Get a handle to the application. The window classand window name were obtained using the Spy++ tool.
IntPtr IEHandle = FindWindow("IEFrame", "Plain Sample Menu - Windows Internet Explorer");

// Verify if is a running process.
if (IEHandle == IntPtr.Zero)
{
MessageBox.Show("Nao esta rodando.");
return;
}

SetForegroundWindow(IEHandle);
Thread.Sleep(2000);
Cursor.Position = new Point(41, 190);
Thread.Sleep(2000);
DoMouseClick();
}

public void DoMouseClick()
{
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
}

any doubts add me on msn, [email protected]
 

Matt_H

Wanderer
You can programatically control Internet Explorer quite easily.. People have written c# wrappers to do so. A pretty decent (it is IE after all) one is WatIN, it's used for automated web testing and isn't that bad..

As for Firefox, i'm pretty sure it's API is closed for this kind of thing...
 
Top