gpt4 book ai didi

javascript - Zombie + NodeJS + Express - 使用选项卡时出错

转载 作者:行者123 更新时间:2023-12-03 11:36:42 26 4
gpt4 key购买 nike

我正在使用 Zombie.js 构建一个小型应用程序,但选项卡似乎无法正常工作。我当前的代码如下:

// 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();
});

但是 browser.html('#content'); 什么也不返回。在我看来,这些选项卡是打开的,但是当我尝试使用浏览器对象从当前打开的选项卡中提取数据时,它永远不会工作。我的做法正确吗?在 Zombie 2.0.8 中使用选项卡的“正确方法”是什么?我只是找不到任何信息/教程,而且官方文档对我来说不够清晰。

编辑:

正如 P.scheit 所指出的,open(url) 还不够。这是主要结果代码:

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)]!');
}
});

运行服务器后,打开一些选项卡:

$ 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"

来自选项卡 0 的数据:

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

最佳答案

只是在这里疯狂猜测:您在打开选项卡后尝试使用访问吗?我不太确定当您传递要打开的网址时 zombie 是否正在请求该网站。

关于javascript - Zombie + NodeJS + Express - 使用选项卡时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26467248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com