phantomJS网页超时 [英] phantomJS webpage timeout

查看:273
本文介绍了phantomJS网页超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个脚本来创建我们应用程序的网络快照. 它运行完美,一切都很好,直到遇到URL破损的图像为止:

I have set up a script to create webshots of our app. It runs perfectly and all is fine Until I encounter an image with a broken url :

 "<img src='http://testserver.our.intranet/fetch/image/373e8fd2339696e2feeb680b765d626e' />"

6秒钟后,我设法使用下面的脚本将脚本破坏了,直到它永远循环为止.

I have managed to break the script after 6 seconds using the below, Before it was just looping forever.

但是,是否可以忽略网络请求(AKA将图像从DOM中取出),然后继续创建没有该图像的拇指(或注入的图像缺少图像!)

But, is it possible to ignore the network request (AKA take the image out of DOM) and then proceed to create the thumb without the image, (or with an injected image missing image !)

var page = require('webpage').create(),
    system = require('system'),
    address, output, size;

if (system.args.length < 3 || system.args.length > 5) {
    phantom.exit(1);
} else {
    address = system.args[1];
    output  = system.args[2];
    page.viewportSize = { width: 640, height: 640 };
    page.zoomFactor = 0.75;
    page.clipRect = { top: 10, left: 0, width: 640, height: 490 };
    try{
        page.open(address, function (status) {
            if (status !== 'success') {
                console.log('Unable to load the address!');
                phantom.exit();
            } else {
                window.setTimeout(function () {
                    page.render(output);
                    phantom.exit();
                }, 200);
            }
        });    
    } finally{
        setTimeout(function() {
            console.log("Max execution time " + Math.round(6000) + " seconds exceeded");
            phantom.exit(1);
        }, 6000);
    }
}

推荐答案

PhantomJS 1.9引入了一个新设置resourceTimeout,该设置控制请求在取消之前可以花费多长时间.除此之外,如果请求超时,则会触发一个onResourceTimeout事件.

PhantomJS 1.9 has introduced a new setting, resourceTimeout, that controls how long a request can take before it gets cancelled. Along with that, there's a onResourceTimeout event that is triggered if/when a request times out.

下面是说明以上所有内容的代码段:

Here's a code snippet illustrating all of the above:

var page = require('webpage').create();  
page.settings.resourceTimeout = 5000; // 5 seconds
page.onResourceTimeout = function(e) {
  console.log(e.errorCode);   // it'll probably be 408 
  console.log(e.errorString); // it'll probably be 'Network timeout on resource'
  console.log(e.url);         // the url whose request timed out
  phantom.exit(1);
};

page.open('http://...', function (status) {
...
}

不幸的是,这些选项目前文献记载不多.我必须经历GitHub 讨论

Unfortunately those options are poorly documented right now. I had to go through GitHub discussions and the PhantomJS source code in order to find them out.

这篇关于phantomJS网页超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆