Anderson Madeira
Anderson Madeira

Reputation: 430

Zombie + NodeJS + Express - Errors while working with tabs

I'm building a small application using Zombie.js, but tabs dont seem to work properly. My current code follows:

// libs used
var Zombie = require("zombie");
var assert = require("assert");
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');

// server port and host configs
var server_port = 7777;
var server_ip_address = '127.0.0.1'

var app = express();
var httpServer = http.Server(app);

app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use( bodyParser.urlencoded() ); // to support URL-encoded bodies

// o navegador
var browser = null,
    result = "",
    SITE_URL = 'http://en.wikipedia.org/wiki/Programming_language';

app.get('/', function (response, request) {
  console.log('GET request received.');
  console.log('Opening new tab: ' + SITE_URL);
  // opens a tab
  browser.open(SITE_URL);
  // gets page content
  result = browser.html('#content');
  console.log('Tab open: '+browser.tabs.index);
  console.log('Content: '+result);
  request.send(result);
});

httpServer.listen(server_port, server_ip_address, function() {
  console.log('Listening on ' + server_ip_address + ':' + server_port);
  // creates the browser.
  browser = new Zombie();
});

But browser.html('#content'); returns nothing. It seems to me that those tabs are open, but when I try to extract data from the current open tab, by using the browser object, it never works. Am I doing the right way? Whats the 'right way' of working with tabs in Zombie 2.0.8 ? I just cant find any information/tutorial and the oficial docs arent clear enough for me.

EDIT:

As pointed by P.scheit, open(url) was not enough. Heres the main resulting code:

app.get('/', function (request, response) {
  console.log('GET request received.');
  if (request.query.url && request.query.selector) {
    console.log('Opening new tab: ' + request.query.url);
    browser.open(request.query.url);
    browser.visit(request.query.url, function () {
      result = browser.html(request.query.selector);
      console.log('Loaded tab content. Sending back to user...');
      response.send(result);
    });
    console.log('Tab open: '+browser.tabs.index);
  } else if (request.query.tabid && request.query.selector) {
    var tabid = request.query.tabid;
    if (tabid < browser.tabs.length) {
      browser.tabs.current = tabid;
      console.log('Retrieving content of tab '+tabid+", sending back to user...");
      result = browser.html(request.query.selector);
      response.send(result);
    } else {
      console.log('Tab not found!');
      response.send('Tab not found!');
    }
  } else {
    console.log('Supply either [(the tab id) AND (search selector)] or [(the url to visit) AND (search selector)]!');
    response.send('Supply either [(the tab id) AND (search selector)] or [(the url to visit) AND (search selector)]!');
  }
});

After running the server, open some tabs:

$ curl "http://127.0.0.1:7777/?url=http://en.wikipedia.org/wiki/Programming_language&selector=h1"
$ curl "http://127.0.0.1:7777/?url=http://stackoverflow.com/users/3739186/cyberpunk&selector=h1"
$ curl "http://127.0.0.1:7777/?url=http://www.google.com.br&selector=a"

Data from tab 0:

$ curl "http://127.0.0.1:7777/?tabid=0&selector=a"

Upvotes: 0

Views: 378

Answers (1)

pscheit
pscheit

Reputation: 2981

Just guessing wildly here: did you try using visit after opening the tab? I'm not quite sure if zombie is requesting the site when you pass the url to open.

Upvotes: 1

Related Questions