gpt4 book ai didi

javascript - 通过命令行管道生成的 javascript

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

我们使用PhantomJS创建网页的屏幕截图。它是从命令行运行的,因为出于某种原因 php-phantomjsscreen-master不适用于我们的服务器。解决方案是使用 PHP shell_exec() 命令运行命令行脚本。

PhantomJS 使用一个 javascript 文件来告诉我们要做什么、要加载哪个页面以及在哪里写入文件。由于多个用户将使用此功能,我需要将用户 ID 或其他内容传递到 javascript 文件中,以便它可以加载用户特定页面并在名称中保存用户 ID 的屏幕截图。

一种选择是通过 PHP 生成 javascript,然后使用它。采用以下 JavaScript:

var id = 1234;
var page = require('webpage').create();
page.viewportSize = { width: 1024, height: 768 };
page.clipRect = { top: 0, left: 0, width: 1024, height: 768 };
page.open('http://test.local/screenshot.php?id='+id, function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('/var/www/test/tmp/screenshot-'+id+'.png');
}
phantom.exit();
});

shell命令是这样的:

/path/to/phantomjs /var/www/test/js/phantom.js

变量id的值为1234。该值不是固定的,而是根据用户而变化。 javascript 文件不是动态的,但可以使用 PHP 生成。我可以生成 javascript 文件,或者只生成脚本。

我可以通过命令行将此生成的脚本(或文件)传输到 PhantomJS 吗?

我还有哪些其他选择可以更好地实现此目的?

最佳答案

如果你想发送命令参数给pj,你可以这样做:

 var system = require('system');
var page = require('webpage').create();
page.open(system.args[1], function () {
page.paperSize = { format: 'A4', orientation: 'landscape'};
window.setTimeout(function(){
page.render(system.args[2]);
phantom.exit();
},300);
});

在 PHP 中:

$cmd = 'phantomjs '.$path. 'render.js '.$html_tpl.' '. $image;
exec($cmd);

关于javascript - 通过命令行管道生成的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33010221/

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