View Generated Source (After AJAX/JavaScript) in C#

后端 未结 3 2143
一向
一向 2020-12-01 02:23

Is there a way to view the generated source of a web page (the code after all AJAX calls and JavaScript DOM manipulations have taken place) from a C# application without ope

相关标签:
3条回答
  • 2020-12-01 02:34

    Theoretically yes, but, at present, no.

    I don't think there is currently a product or OSS project that does this. Such a product would need to have it's own javascript interpreter and be able to accurately emulate the run-time environment and quirks of every browser it supports.

    Given that you need something that accurately emulates the server + browser environment in order to produce the final page code, in the long run, I think that using a browser instance is the best way to accurately generate the page in its final state. This is especially true, when you consider that, after the page load completes, the page sources can still change over time in the browser from AJAX/javascript.

    0 讨论(0)
  • 2020-12-01 02:36

    Best way is using PhantomJs. That's Great. (sample of that is Article).

    My solution is look like this:

    var page = require('webpage').create();
    
    page.open("https://sample.com", function(){
        page.evaluate(function(){
            var i = 0,
            oJson = jsonData,
            sKey;
            localStorage.clear();
    
            for (; sKey = Object.keys(oJson)[i]; i++) {
                localStorage.setItem(sKey,oJson[sKey])
            }
        });
    
        page.open("https://sample.com", function(){
            setTimeout(function(){
             page.render("screenshoot.png") 
                // Where you want to save it    
               console.log(page.content); //page source
                // You can access its content using jQuery
                var fbcomments = page.evaluate(function(){
                    return $("body").contents().find(".content") 
                }) 
                phantom.exit();
            },10000)
        });     
    });
    
    0 讨论(0)
  • 2020-12-01 02:57

    it is possibly using an instance of a browser (in you case: the ie control). you can easily use in your app and open a page. the control will then load it and process any javascript. once this is done you can access the controls dom object and get the "interpreted" code.

    0 讨论(0)
提交回复
热议问题