locked
Microsoft Jscript using Cscript engine in Powershell: Can webpages be retrieved by Jscript as text strings only or also as html Document objects, or as XML objects RRS feed

  • Question

  • I am deveoping a Microsoft Jscript application which I run in Powershell, using the cscript engine.

    The application is grabbing web pages, which are to be analysed further to extract certain information.

    Untill now I use RegExps for this purpose. However I would like to be able to analyse the webpages using XPATH or HTML DOM methods, being more robust to page changes.

    However, it seems that I can retrieve the webpages as text only, neither as html document object, analysable with HTML DOM methods, nor as an XML document object analysable by XPATH methods.

    Here is part of my code:

    //prepare http request 

    var  httpReq = tryXMLHttpRequest();
      alert("Getting page 1...");
      httpReq.open("get", url, false);
      //httpReq.responseType = "document"; //fails
      httpReq.send();

      if (httpReq.status != 200)
      {
        alert("Failed with status: " + httpReq.status);
        return false;
      }

    I retrieve the result of the http request by:

    var document   = httpReq.responseText; // yields document as a string

    which yields a string object.

    Trying to get an HTML Document object by setting the http request response type:

    //see: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/HTML_in_XMLHttpRequest

     httpReq.responseType = "document"; //fails

    fails

    Trying get a XML Document object in the following way:

    var document = httpReq.responseXML

    also fails.

    For the preparation of the http request, I do the following try catch blocks,

    Various trial and error runs have made me clear that only the first 2 can return succesfuly.

    function newXMLHttpRequest()
    {
      try //1
      {
          alert('trying new ActiveXObject("Microsoft.XMLHTTP")......')
          return new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {alert('error trying new ActiveXObject("Microsoft.XMLHTTP")')}

      try //2
      {
        alert('trying new ActiveXObject("Msxml2.XMLHTTP").....')
        return new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {alert('error trying new ActiveXObject("Msxml2.XMLHTTP")')}

      try
      {
        alert('trying new XMLHttpRequest.....')
        return new XMLHttpRequest();
      }
      catch (e) {alert('error trying new XMLHttpRequest') }

      try
      {
        alert('trying ActiveXObject("MSXML2.XMLHTTP.3.0").....')
        return ActiveXObject("MSXML2.XMLHTTP.3.0")
      }
      catch(e) {alert('error trying ActiveXObject("MSXML2.XMLHTTP.3.0")')}

      return false;
    }

    For the http request I have also tried (to no avail as mentioned above) 

    httpReq.open("GET", url, false);

    httpReq.responseType = "document";

    httpReq.send();

    Ihave also tried DocumentFrament() to insert my html "soup" into,

    see: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment/DocumentFragment

    var fragment = new DocumentFragment()

    fails with : DocumentFragment is undefined.

    So I repeat my question:

    Microsoft Jscript using the Cscript engine in Powershell: Can webpages be retrieved by Jscript as text strings only or also as html Document objects, or as XML objects, and if so, can you please show me the code to do so.

    P.S.

    My programming experience is in Python, I am a total newbie to JavaScript and the capabilites/limitations of Ms Jscript and the Cscript engine w.r.t. Javascript.






    • Edited by oldie1945 Thursday, February 16, 2017 5:35 PM
    • Moved by Neda Zhang Friday, February 17, 2017 2:45 AM Moved from VB.NET
    Thursday, February 16, 2017 3:45 PM

All replies

  • You might want to post your question to the PowerShell forum or The Official Scripting Guys Forum!. This forum is for Visual Basic .NET.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, February 16, 2017 4:11 PM
  • This is a question about Jscript, not about Powershell scripting

    oldie1945

    Thursday, February 16, 2017 5:57 PM
  • This is a question about Jscript, not about Powershell scripting

    oldie1945

    Unfortunately this is not a scripting forum and the scripting language you are using is not VB. I don't think there is a JScript forum so you may want to try the scripting forum I mentioned.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, February 16, 2017 6:20 PM
  • Hi oldie1945,

    Thank you for posting here.

    Since this issue is related to JScript, so I would suggest that you could post the related issues to JScript Native forum for better support. And I will move this thread to this forum.

    Thanks for your understanding and support.

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 17, 2017 2:35 AM