How to evaluate javascript inside c #? (you need to get all the links for the web page, including those created by java-script)

Reference Information. I have to load web pages with my resources for offline viewing, but as part of this I have to β€œrewrite” the URL of the links to the HTML web page for them to work. These are better than standard link types, but I understand that there are some links that are dynamically created by javascript.

Question: What approach (or even an existing library) could I use to transcribe web pages with dynamically generated links (with javascript) to a web page with normal non-dynamic links. (since then I can do the rewriting of the URL I need to do)

Notes:

  • It is almost as if I need to have a Javascript interpreter library through which I pass the HTML page of the page and then spills out the generated Java code. Then I can rewrite the links as I wish (the result will not use the dynamic javascript approach).
  • Context is a C # WinForms (3.5) application.

thank

PS. Some examples:

<script type="text/javascript">
        <!--
            document.write("<a href=\"/home.asp\" onMouseOver=\"MM_swapImage('tab_home','','/_includes/images/tab_home_.gif',1)\" onMouseOut=\"MM_swapImgRestore()\"><img src=\"/includes/images/tab_home.gif\" alt=\"Home\" name=\"tab_home\" width=\"45\" height=\"18\" border=\"0\" id=\"tab_home\"><\/a>");

            if (window.document.location.pathname.indexOf("mysite.asp") != "-1") {
                document.write("<a href=\"/mysite.asp\" onMouseOver=\"MM_swapImage('tab_my_site','','/_includes/images/tab_my_site_.gif',1)\" onMouseOut=\"MM_swapImgRestore()\"><img src=\"/_includes/images/tab_my_site_.gif\" alt=\"My Site\" name=\"tab_my_site\" width=\"76\" height=\"18\" border=\"0\" id=\"tab_my_site\"><\/a>");
            }
            else {
                document.write("<a href=\"/mysite.asp\" onMouseOver=\"MM_swapImage('tab_my_site','','/_includes/images/tab_my_site_.gif',1)\" onMouseOut=\"MM_swapImgRestore()\"><img src=\"/_includes/images/tab_my_site.gif\" alt=\"My Site\" name=\"tab_my_site\" width=\"76\" height=\"18\" border=\"0\" id=\"tab_my_site\"><\/a>");
            }

and

<script type="text/javascript">
  var fo = new FlashObject("/homepage/ia/flash/hero/banner.swf?q=1", "hero", "642", "250", "8", "#ffffff");
  fo.addParam("wmode", "transparent");
  fo.addParam("allowScriptAccess", "always");
  fo.addParam("base", "/homepage/ia/flash/hero/");
  fo.write("flashContent");
</script>

and

<td width="1%">  
  <a href="javascript:checksubmit(this);" 
      onmouseover="MM_swapImage('but_srch_go','','/_includes/images/but_srch_go_.gif',1)"      
      onmouseout="MM_swapImgRestore()">        
      <img src="http://localhost:3000/sites/http://qheps.health.qld.gov.au/_includes/images/but_srch_go.gif" alt="Go" name="but_srch_go" width="57" height="40" border="0">   
   </a>
</td>
+3
source share
1 answer

If you are not using the WebBrowser control, you can use the JScriptEvaluate method in JScript.NET, but most likely you will need to evaluate more than just a simple expression. Managing WebBrowser is certainly an easier route.

WebBrowser, "eval" #.

/// <summary>
/// Handles the Navigated event of the browser control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="T:WebBrowserNavigatedEventArgs"/> instance containing the
/// event data.</param>
private void browser_Navigated( object sender, WebBrowserNavigatedEventArgs e )
{

    string codeToEval = "window.alert('blah')";

    if ( browser.Document != null ) {

        object window = browser.Document.Window.DomWindow;
        if ( window != null ) {

            Type windowType = window.GetType();
            BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance;
            string[] args = { codeToEval, "JScript" };

            windowType.InvokeMember( "[DispID=1165]", flags, null, window, args );

        }   // if

    }   // if

}

. HTML- , URL-, , HTML , "" .

+2

Source: https://habr.com/ru/post/1732712/


All Articles