Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get horseman work synchronous #318

Open
heboyuan opened this issue Oct 31, 2017 · 0 comments
Open

how to get horseman work synchronous #318

heboyuan opened this issue Oct 31, 2017 · 0 comments

Comments

@heboyuan
Copy link

heboyuan commented Oct 31, 2017

I'm trying to fetch things from another website with horseman, and then use it in my own web. However, my own res.send always triggers before horseman has collected data.

var Horseman = require('node-horseman');
var express = require("express");

var app = express();

var ProfName = 'Stahl';
var URL = "http://www.bruinwalk.com/search/?q=" + ProfName;


function getProf(callback){
    var result = {com:"default", rat:"default"};
    var horseman = new Horseman();
    horseman.open(URL)
        .evaluate(function(selector){
            var els = $(selector);
            $.each(els, function(i, el) {
                var event = document.createEvent('MouseEvent');
                event.initEvent('click', true, true);
                el.dispatchEvent(event);
            });
        }, 'h1:eq(3)')
        .delay(1000)
        .evaluate(function(selector){
            var els = $(selector);
            $.each(els, function(i, el) {
                var event = document.createEvent('MouseEvent');
                event.initEvent('click', true, true);
                el.dispatchEvent(event);
            });
        }, "div:contains('COM SCI 31')")
        .delay(1000)
        .screenshot('test1.png')
        .text('.metric')
        .then(function(text){
            result.com = text;   
        })
        .text('.expand-area>p')
        .then(function(text){
            result.rat = text;   
        })
        .close();
    callback(result.rat,result.com);
}

app.get("/",function(req,res){
    getProf(function(comments, rating){
        console.log(comments);
        console.log(rating);
        res.send('success');
    });
});

this gives me

default
default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant