puppeteer/phantom_shim/Phantom.js

143 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-05-11 07:06:41 +00:00
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2017-08-02 19:06:47 +00:00
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const url = require('url');
2017-05-11 07:06:41 +00:00
const VERSION = [0, 0, 1];
module.exports.version = VERSION;
2017-05-11 07:06:41 +00:00
/**
* @param {!Object} context
* @param {string} scriptPath
*/
module.exports.create = function(context, scriptPath) {
const phantom = {
2017-06-21 20:51:06 +00:00
page: {
onConsoleMessage: null,
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @param {string} relative
* @param {string} base
* @return {string}
*/
2017-06-21 20:51:06 +00:00
resolveRelativeUrl: function(relative, base) {
return url.resolve(base, relative);
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @param {string} url
* @return {string}
*/
2017-06-21 20:51:06 +00:00
fullyDecodeUrl: function(url) {
return decodeURI(url);
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
libraryPath: path.dirname(scriptPath),
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
onError: null,
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @return {string}
*/
2017-06-21 20:51:06 +00:00
get outputEncoding() {
return 'UTF-8';
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @param {string} value
*/
2017-06-21 20:51:06 +00:00
set outputEncoding(value) {
throw new Error('Phantom.outputEncoding setter is not implemented');
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
get cookiesEnabled() {
return true;
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @param {boolean} value
*/
2017-06-21 20:51:06 +00:00
set cookiesEnabled(value) {
throw new Error('Phantom.cookiesEnabled setter is not implemented');
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @return {!{major: number, minor: number, patch: number}}
*/
2017-06-21 20:51:06 +00:00
get version() {
return {
major: VERSION[0],
minor: VERSION[1],
patch: VERSION[2],
2017-06-21 20:51:06 +00:00
};
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @param {number=} code
*/
2017-06-21 20:51:06 +00:00
exit: function(code) {
process.exit(code);
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
injectJs: function(filePath) {
filePath = path.resolve(phantom.libraryPath, filePath);
if (!fs.existsSync(filePath))
return false;
let code = fs.readFileSync(filePath, 'utf8');
2017-06-21 20:51:06 +00:00
if (code.startsWith('#!'))
code = code.substring(code.indexOf('\n'));
vm.runInContext(code, context, {
filename: filePath,
displayErrors: true
});
return true;
},
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @param {string} moduleSource
* @param {string} filename
*/
2017-06-21 20:51:06 +00:00
loadModule: function(moduleSource, filename) {
const code = [
2017-06-21 20:51:06 +00:00
'(function(require, exports, module) {\n',
moduleSource,
'\n}.call({},',
'require.cache[\'' + filename + '\']._getRequire(),',
'require.cache[\'' + filename + '\'].exports,',
'require.cache[\'' + filename + '\']',
'));'
].join('');
vm.runInContext(code, context, {
filename: filename,
displayErrors: true
});
},
};
return phantom;
};