简体   繁体   English

如何在PhantomJS中包含多个JavaScript库?

[英]How to include multiple JavaScript libs in PhantomJS?

I'm using PhantomJS to generate a series of images based on the state of an HTML canvas element which I am manipulating via JS. 我正在使用PhantomJS根据我通过JS处理的HTML canvas元素的状态生成一系列图像。 This canvas manipulation depends on 3 separate JS libraries and some inline scripts which I invoke during window.onload . 这种画布操作取决于3个单独的JS库以及一些在window.onload期间调用的内联脚本。

The PhantomJS docs cover how to include a single JS library, but it doesn't cover how to include several. PhantomJS文档涵盖了如何包含单个JS库,但未涵盖如何包含多个JS库。

Can anyone provide me with the correct syntax to include several JS libs, and then run some scripts during window.onload ? 谁能为我提供包含几个JS库的正确语法,然后在window.onload期间运行一些脚本?

There is a callback for page.includeJs() . 有一个用于page.includeJs()的回调。 When the first one is done, then you can load the next one. 当第一个完成后,您可以加载下一个。 This is usually done recursively. 通常这是递归完成的。

function multipleIncludeJs(page, jsArray, done) {
    if (jsArray.length === 0) {
        done();
        return;
    }

    var url = jsArray.shift();
    page.includeJs(url, function(){
        multipleIncludeJs(page, jsArray, done);
    });
}

and use it like this: 并像这样使用它:

multipleIncludeJs(page, ["http://code.jquery...", "http://getbootstrap..."], function(){
    console.log("loaded");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM