简体   繁体   中英

Accessing values of JS Global variables in HTML with PhantomJS

So I have a piece of HTML that looks something like this:

<html>
  <body>
    <script>
      var foo = {
        bar: []
      };
    </script>
  </body>
</html>

And I am trying to use PhantomJS to extract the value of foo.bar. How would I do this? So far I know I would have is structured like this:

var webPage = require('webpage'); 
var page = webPage.create();
page.open(MY_URL, function(status) {
  var foo = page.evaluate(function(){
    //gets javascript from the HTML in the response
    // and extracts foo from there
  });
});

console.log(someVar);
phantom.exit();

Seems you should just be able to use

var foo = page.evaluate(function() {
  return window.foo
})
console.log('foo =', foo)

Here's how you would, open url from phantom virtual browser and get a javascript returned value from webpage with phantom

const phantom = require('phantom');


openUrl = (req, res, next) => {
    let url = 'your url goes here';
    const {content, returnedFromPage} = await loadJsSite(url);
    return res.json(returnedFromPage);
}

loadJsSite = async (url) => {
  return new Promise( async (resolve, reject) => {

    const instance = await phantom.create();
    const page = await instance.createPage();
    await page.on('onResourceRequested', function(requestData) {
      console.info('Requesting', requestData.url);
    });

    const status = await page.open(url);
    var returnedFromPage = await page.evaluate(function() {
            return document.title;
        });
    const content = await page.property('content');

    await instance.exit();

    return resolve({content: content, returnedFromPage: returnedFromPage});

  })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM