Execute JavaScript

This is a discussion on Execute JavaScript within the C# Programming forums, part of the General Programming Boards category; Hello, I need to write an application which should be able to download the content of a .html page. This ...

  1. #1
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244

    Lightbulb Execute JavaScript

    Hello,

    I need to write an application which should be able to download the content of a .html page. This .html page contains a lot of JS code, which uses some document.write and other stuff.

    called in a browser, the page displays an image. i need the url of that image, but that url is printed out by javascript.
    i need to execute that javascript in order to retrieve that url.
    so how can i "render" a html page (execute JS) ?

    thanks.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You can use WebBrowser.
    Typically you don't need to display it at all, it can run on the background execute all the JS code and then you can try to find the url.

    This is not that straightforward and there are some tricks and things you need to solve on the long run. Try searching for examples on the WebBrowser class and you can go from there.

  3. #3
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    i tried that and somehow, the WebBrowser object does not seem to contain any data.
    example:
    Code:
    			WebBrowser web = new WebBrowser();
    			web.Navigate("http://www.google.com/");
    			Thread.Sleep(5000);
    			// breakpoint here
    it has null's and ""s everywhere.
    what am i doing wrong?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,554
    Link with an interpreter ?
    SpiderMonkey (JavaScript-C) Engine
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    I just checked out these JS interpreters. they seem to be able to evaluate functions like "x*2" but cannot execute a whole HTML page?

    any suggestions? what interpreter to use?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,554
    Parse the HTML, ripping out the JS and feed it to your JS engine.
    It's all that any browser would do for you anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Can you provide an example URL so I can better understand what you mean by javascript printing the link to the picture? I would consider porting the javascript code to C# as the way forward.

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Devils Child View Post
    i tried that and somehow, the WebBrowser object does not seem to contain any data.
    example:
    Code:
    			WebBrowser web = new WebBrowser();
    			web.Navigate("http://www.google.com/");
    			Thread.Sleep(5000);
    			// breakpoint here
    it has null's and ""s everywhere.
    what am i doing wrong?
    The reason is that with Thread.Sleep() you will simply block the current thread, so it is not that reliable.

    You have to do something like this:
    Code:
    private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
                completedLoading = true;
    }
    
    public void LoadPage(string url, bool wait = true)
    {
                completedLoading = false;
                browser.Navigate(new Uri(url));
                delaying = true;
                delayThread = new Thread(new ParameterizedThreadStart(delayDel));
                delayThread.Start(SecDelaying);
                while (delaying)
                    Application.DoEvents();
                while (!completedLoading)
                    Application.DoEvents();
    }
    
    public static void Delay(object sec)
    {
                Thread.Sleep((int)sec * 1000);
                delaying = false;
    }
    The first part, the most important, while ensure that the Document is completed, thus the page loaded. BUT, sometimes it loads more than once. That is why I have an additional delay. Note that the Delay function is executed from another Thread (delayThread) so it doesn't stop current thread.

    Just a note, I had problems finding links even using the method above. For example I was trying to get links from search engine pages. For most of the part I could, but still somethings were missing, so it might not always be possible to do what you want.

  9. #9
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    that webbrowser example of you seems to be the way to go, but it uses 100 % CPU and somehow does not load anyway?

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The page should load. That is what DocumentCompleted event is there for. I am assuming you included also
    Code:
    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted );
    somewhere in your code before.

    The thing is that there are some pages that load more than once. Like maybe the initial pages is loaded, Javascript is played afterwards and another page is loaded again or anyhow something like that. So even if the initial Document is loaded, still the page might change until the user gets control. That is why I have a delay. Apart from that the DocumentCompleted is a way to ensure the loading of the page.

    Yes, when you have a while loop you will use 100% of your CPU. You can change this if you want by any method you want, but in my case there was no reason to optimize.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. JavaScript book recommendations
    By neandrake in forum Tech Board
    Replies: 2
    Last Post: 04-05-2009, 12:27 PM
  2. Replies: 2
    Last Post: 03-10-2009, 08:36 PM
  3. Execute external file
    By Malefaust in forum C Programming
    Replies: 3
    Last Post: 07-03-2006, 04:20 PM
  4. Execute Command from C fcn in DLL
    By karcheee in forum Windows Programming
    Replies: 2
    Last Post: 07-27-2005, 03:42 PM
  5. Execute prog as other user
    By groorj in forum C Programming
    Replies: 2
    Last Post: 05-04-2005, 12:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21