我尝试使用 PhantomJS 制作 www.fallswoodsmith.com 的屏幕截图。我的代码是:
var page = require('webpage').create();
page.viewportSize = { width: 1024, height: 768 };
page.clipRect = {top: 0, left: 0, width: 1024, height: 768};
page.open('http://www.fallswoodsmith.com', function () {
page.render('cache/www.fallswoodsmith.com123567266_1024_768.png', {format: 'png', quality: '10'});
phantom.exit();
});
这个页面只有 JS,所以没有 JS 就没有内容。出于某种原因,PhantomJS 没有执行这个 JS。我还尝试为 page.render() 和 phantom.exit() 设置 5 秒的超时,但这并没有改变什么。如果我在 page.render() 之前执行 console.log(page.content) 我会得到页面的完整 HTML - 只是没有 JS 所做的更改。
为什么PhantomJS不执行页面的JS?
更新 1: 我添加了以下调试内容:
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
};
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.onResourceTimeout = function(request) {
console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
};
我的控制台没有 console.log() 输出...
最佳答案
请允许我礼貌地问一下,这个网站是谁制作的?我强烈建议不要以 100% 依赖 JavaScript 的方式构建站点。关闭 JavaScript 并“加载”该网站 ( www.fallswoodsmith.com ) 没有结果。压缩。虚无。 zilch。 </rant>
运行上面的屏幕截图脚本,我得到以下输出:
TypeError: 'undefined' is not a function (evaluating 'joinURL.bind(null, staticServerUrl)')
http://static.parastorage.com/services/santa-versions/1.150.0/main-r.js:353 in wixRenderSite
要解决该问题,您可以填充 Function.prototype.bind (PhantomJS 1.x 中缺少 as per this issue )在创建网页对象之后但在加载 URL 之前(即 onInitialized )。
结果:
var page = require('webpage').create();
page.onInitialized = function () {
page.evaluate(function () {
var isFunction = function (obj) {
return typeof obj == 'function' || false;
};
var slice = Array.prototype.slice;
Function.prototype.bind = function bind(obj) {
var args = slice.call(arguments, 1);
var self = this;
var F = function () {};
var bounded = function() {
return self.apply(
this instanceof F ? this : (obj || {}),
args.concat(slice.call(arguments))
);
};
F.prototype = this.prototype || {};
bounded.prototype = new F();
return bounded;
};
});
};
page.open('http://www.fallswoodsmith.com', function () {
setTimeout(function screenshot() {
page.render('WORKS.png', {
format: 'png',
quality: '10',
});
phantom.exit();
}, 10 * 1000);
});
为什么要等 10 秒再截屏?好吧,由于该站点完全依赖于 JS,因此没有明显的事件(我能想到的)等待指示页面加载。你的旅费可能会改变。根据需要增加或减少超时。
注意:上面的输出文件名是WORKS.png .
上面的例子已经过测试并适用于 PhantomJS 1.9.7。该脚本似乎也适用于 PhantomJS 1.9.8,但 1.9.8 有 this issue (Unsafe JavaScript attempt to access frame in 1.9.8)虽然已修复,但它不是任何版本的一部分,并会导致以下错误输出:
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file://28011634.js. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file://28011634.js. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file://28011634.js. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file://28011634.js. Domains, protocols and ports must match.
默认情况下,呈现的图像将是整页屏幕截图。要修复视口(viewport)大小,您可以在脚本顶部重新添加以下内容:
page.viewportSize = {
width: 1024,
height: 768
};
page.clipRect = {
top: 0,
left: 0,
width: 1024,
height: 768
};
.bind 在 MDN 上找到的 polyfill , 似乎没有一点修改就无法工作,但是,结合 underscore.js 源代码和 this answer结果如上。
关于javascript - PhantomJS 没有执行 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28011634/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象