bootstrap.js
9.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*!
* Casper is a navigation utility for PhantomJS.
*
* Documentation: http://n1k0.github.com/casperjs/
* Repository: http://github.com/n1k0/casperjs
*
* Copyright (c) 2011-2012 Nicolas Perriault
*
* Part of source code is Copyright Joyent, Inc. and other Node contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
if (!phantom.casperLoaded) {
// see http://semver.org/
phantom.casperVersion = {
major: 0,
minor: 6,
patch: 0,
ident: 'alpha',
toString: function() {
var version = [this.major, this.minor, this.patch].join('.');
if (this.ident) {
version = [version, this.ident].join('-');
}
return version;
}
};
// Patching fs
// TODO: watch for these methods being implemented in official fs module
var fs = (function(fs) {
if (!fs.hasOwnProperty('basename')) {
fs.basename = function(path) {
return path.replace(/.*\//, '');
};
}
if (!fs.hasOwnProperty('dirname')) {
fs.dirname = function(path) {
return path.replace(/\/[^\/]*\/?$/, '');
};
}
if (!fs.hasOwnProperty('pathJoin')) {
fs.pathJoin = function() {
return Array.prototype.join.call(arguments, this.separator);
};
}
return fs;
})(require('fs'));
// casper root path
if (!phantom.casperPath) {
try {
phantom.casperPath = phantom.args.map(function(i) {
var match = i.match(/^--casper-path=(.*)/);
if (match) {
return fs.absolute(match[1]);
}
}).filter(function(path) {
return fs.isDirectory(path);
}).pop();
} catch (e) {}
}
if (!phantom.casperPath) {
console.error("Couldn't find not compute phantom.casperPath, exiting.");
phantom.exit(1);
}
// Embedded, up-to-date, validatable & controlable CoffeeScript
phantom.injectJs(fs.pathJoin(phantom.casperPath, 'modules', 'vendors', 'coffee-script.js'));
// Adding built-in capabilities to phantom object
phantom.sourceIds = {};
phantom.getErrorMessage = function(e) {
return (e.fileName || this.sourceIds[e.sourceId]) + ':' + e.line + ' ' + e;
};
phantom.getScriptCode = function(file, onError) {
var scriptCode = fs.read(file);
if (/\.coffee$/i.test(file)) {
try {
scriptCode = CoffeeScript.compile(scriptCode);
} catch (e) {
this.processScriptError(e, file, onError);
}
}
// trick to locate source file location on error
scriptCode += ";var __fe__ = new Error('__sourceId__')";
scriptCode += ";__fe__.fileName = '" + file + "'";
scriptCode += ";throw __fe__;";
return scriptCode;
};
phantom.processScriptError = function(error, file, callback) {
if (!this.sourceIds.hasOwnProperty(error.sourceId)) {
this.sourceIds[error.sourceId] = file;
}
if (error.message === "__sourceId__") {
return;
}
if (typeof callback === "function") {
callback(error, file);
} else {
console.error(this.getErrorMessage(error));
this.exit(1);
}
};
// Patching require()
// Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/
// TODO: remove when PhantomJS has full module support
require = (function(require, requireDir) {
var phantomBuiltins = ['fs', 'webpage', 'webserver'];
var phantomRequire = phantom.__orig__require = require;
var requireCache = {};
return function(path) {
var i, dir, paths = [],
fileGuesses = [],
file,
module = {
exports: {}
};
if (phantomBuiltins.indexOf(path) !== -1) {
return phantomRequire(path);
} else {
if (path[0] === '.') {
paths.push(fs.absolute(fs.pathJoin(requireDir, path)));
} else if (path[0] === '/') {
paths.push(path);
} else {
dir = fs.absolute(requireDir);
while (dir !== '') {
// nodejs compatibility
paths.push(fs.pathJoin(dir, 'node_modules', path));
dir = fs.dirname(dir);
}
paths.push(fs.pathJoin(requireDir, 'lib', path));
paths.push(fs.pathJoin(requireDir, 'modules', path));
}
paths.forEach(function(testPath) {
fileGuesses.push.apply(fileGuesses, [
testPath,
testPath + '.js',
testPath + '.coffee',
fs.pathJoin(testPath, 'index.js'),
fs.pathJoin(testPath, 'index.coffee'),
fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.js'),
fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.coffee')
]);
});
file = null;
for (i = 0; i < fileGuesses.length && !file; ++i) {
if (fs.isFile(fileGuesses[i])) {
file = fileGuesses[i];
}
}
if (!file) {
throw new Error("CasperJS couldn't find module " + path);
}
if (file in requireCache) {
return requireCache[file].exports;
}
try {
var scriptCode = phantom.getScriptCode(file);
new Function('module', 'exports', scriptCode)(module, module.exports);
} catch (e) {
phantom.processScriptError(e, file);
}
requireCache[file] = module;
return module.exports;
}
};
})(require, phantom.casperPath);
// Adding stack traces to Error
// Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/
// TODO: remove when phantomjs has js engine upgrade
if (!new Error().hasOwnProperty('stack')) {
Object.defineProperty(Error.prototype, 'stack', {
set: function(string) {
this._stack = string;
},
get: function() {
var location;
if (this._stack) {
return this._stack;
} else if (this.fileName || this.sourceId) {
location = phantom.getErrorMessage(this);
} else {
location = "unknown";
}
var from = this.caller || "anonymous";
return this.toString() + '\n ' + from + ' at ' + location;
},
configurable: true,
enumerable: true
});
}
// BC < 0.6
phantom.Casper = require('casper').Casper;
// casper cli args
phantom.casperArgs = require('cli').parse(phantom.args);
// loaded status
phantom.casperLoaded = true;
}
if (!!phantom.casperArgs.options.version) {
console.log(phantom.casperVersion.toString());
phantom.exit(0);
} else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) {
var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.');
console.log('CasperJS version ' + phantom.casperVersion.toString() + ' at ' + phantom.casperPath);
console.log('Using PhantomJS version ' + phantomVersion);
console.log('Usage: casperjs script.(js|coffee) [options...]');
console.log('Read the docs http://n1k0.github.com/casperjs/');
phantom.exit(0);
}
phantom.casperScript = phantom.casperArgs.get(0);
if (!fs.isFile(phantom.casperScript)) {
console.error('Unable to open file: ' + phantom.casperScript);
phantom.exit(1);
}
// filter out the called script name from casper args
phantom.casperArgs.args = phantom.casperArgs.args.filter(function(arg) {
return arg !== phantom.casperScript;
});
// passed casperjs script execution
try {
new Function(phantom.getScriptCode(phantom.casperScript))();
} catch (e) {
phantom.processScriptError(e, phantom.casperScript);
}