我的脚本是:
var page = require('webpage').create();
var system = require('system');
var fs = require('fs');
var file = fs.open(system.args[1], 'r');
page.content = file.read();
page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js', function() {
page.injectJs('https://www.google.com/jsapi', function() {
// result = page.evaluate(function(){
// });
// commented out until the rest is working
system.stdout.write(page.content);
});
});
page.close()
phantom.exit();
此打开的文件是:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
google.load('visualization','1', {packages: ['corechart'], callback: draw_chart});
function draw_chart() {
var data_table = new google.visualization.DataTable();data_table.addColumn({"type":"string","label":"ID"});data_table.addColumn({"type":"number","label":"Effective"});data_table.addColumn({"type":"number","label":"Usable"});data_table.addColumn({"type":"string","label":"Findable"});data_table.addColumn({"type":"number","label":"Persuasive"});data_table.addRow([{v: "SG"}, {v: 0.264}, {v: 0.48100000000000004}, {v: "Below Average Findability"}, {v: 11.1}]);data_table.addRow([{v: "ID"}, {v: 0.353}, {v: 0.596}, {v: "Below Average Findability"}, {v: 5.6}]);data_table.addRow([{v: "WP"}, {v: 0.39799999999999996}, {v: 0.34299999999999997}, {v: "Average Findability"}, {v: 16.7}]);data_table.addRow([{v: "AS"}, {v: 0.52}, {v: 0.504}, {v: "Above Average Findability"}, {v: 37.5}]);data_table.addRow([{v: "CB"}, {v: 0.272}, {v: 0.242}, {v: "Below Average Findability"}, {v: 21.2}]);data_table.addRow([{v: "AN"}, {v: 0.419}, {v: 0.507}, {v: "Average Findability"}, {v: 16.1}]);data_table.addRow([{v: "SR"}, {v: 0.342}, {v: 0.35200000000000004}, {v: "Average Findability"}, {v: 15.2}]);data_table.addRow([{v: "AM"}, {v: 0.353}, {v: 0.25}, {v: "Average Findability"}, {v: 25.0}]);data_table.addRow([{v: "BT"}, {v: 0.355}, {v: 0.135}, {v: "Average Findability"}, {v: 13.9}]);data_table.addRow([{v: "NB"}, {v: 0.29100000000000004}, {v: 0.11800000000000001}, {v: "Average Findability"}, {v: 5.6}]);data_table.addRow([{v: "CF"}, {v: 0.268}, {v: 0.293}, {v: "Average Findability"}, {v: 19.4}]);data_table.addRow([{v: "ML"}, {v: 0.3}, {v: 0.429}, {v: "Above Average Findability"}, {v: 6.5}]);
var chart = new google.visualization.BubbleChart(document.getElementById('chart'));
chart.draw(data_table, {fontName: "Open Sans", fontSize: 12, width: 750, height: 750, hAxis: {title: "Effective", format: "00.0%", textStyle: {bold: "true", italic: "false"}, titleTextStyle: {fontSize: 18, bold: "true", italic: "false"}, viewWindow: {min: 0.0, max: 0.7}}, vAxis: {title: "Usable", format: "00.0%", textStyle: {bold: "true", italic: "false"}, titleTextStyle: {fontSize: 18, bold: "true", italic: "false"}, viewWindow: {min: -0.1, max: 0.9}}, bubble: {textStyle: {bold: "true", auraColor: "none"}}, colors: ["red","yellow","green"], legend: {position: "none"}, sizeAxis: {minSize: 15, maxSize: 40}});
};
</script><div id="chart">Chart did not generate</div>
</body>
</html>
为什么我会收到此错误?
"ReferenceError: Can't find variable: google\n\n about:blank:9\n"
(我试图注入JS而不是将其包含在HTML <head>
中,因为我正在与page.evaluate()
仅评估第一个如果我这样做,我在HTML中有三个<script>
。)
更新
如果我使用includeJS()
代替injectJS()
,我会收到相同的错误。
更新2
如果我将其改为此,它似乎有效:
var page = require('webpage').create();
var system = require('system');
var fs = require('fs');
var file = fs.open(system.args[1], 'r');
page.content = file.read();
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js');
page.includeJs('https://www.google.com/jsapi');
// result = page.evaluate(function(){
// });
system.stdout.write(page.content);
page.close()
phantom.exit();
但是当我把它改成这个时,我再次得到同样的错误:
var page = require('webpage').create();
var system = require('system');
var fs = require('fs');
var file = fs.open(system.args[1], 'r');
page.content = file.read();
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js');
page.includeJs('https://www.google.com/jsapi');
result = page.evaluate(function(){
});
system.stdout.write(result);
page.close()
phantom.exit();