-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (97 loc) · 2.96 KB
/
index.js
File metadata and controls
122 lines (97 loc) · 2.96 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
var fetch = require('node-fetch');
var JSDOM = require("jsdom").JSDOM;
var usertiming = require('usertiming');
function newJSDOM (html, options) {
return new JSDOM(html || '<!DOCTYPE html>', options);
}
function enhanceToString () {
"use strict"; // causes fn.call(null) to return [object Null] instead of [object global]
var oldToString = Object.prototype.toString;
Object.prototype.toString = function () {
var ts = oldToString.call(this);
if (ts === '[object Object]') {
if (this.nodeType) {
return this.toString();
}
return ts;
}
return ts;
};
}
function init (options) {
options = options || {};
var context = options.context || global;
if (options.url) {
options.url = options.url;
}
options.runScripts = 'dangerously';
options.resources = "usable";
this._dom = newJSDOM(options.html, options);
var win = this._dom.window;
// allow child windows
win.open = function (url) {
return newJSDOM(null, { url: url }).window;
};
win.navigator.sendBeacon = function () {};
win.document.hasFocus = function () { return true; };
// Adds LocalStorage
// This should only be needed until jsdom adds this feature
// https://github.com/tmpvar/jsdom/issues/1137
win.localStorage = win.sessionStorage = (function () {
return {
getItem: function (key) {
return this[key] || null;
},
setItem: function (key, value) {
this[key] = value;
},
removeItem: function (key) {
this[key] = null;
},
};
})();
// https://github.com/tmpvar/jsdom/issues/1510
win.performance = usertiming;
win.performance.navigation = {
redirectCount: 0,
type: 1
};
win.fetch = fetch;
win.screen.availTop = 0;
win.screen.availLeft = 0;
win.screen.width = win.innerWidth;
win.screen.height = win.innerHeight;
win.screen.colorDepth = 32;
win.screen.pixelDepth = 32;
context.window = win;
for (var x in win) {
if (typeof context[x] === 'undefined') {
try {
context[x] = win[x];
} catch (e) {
// not sure what to do if this fails
}
}
}
for (var y in win._core) {
if (typeof context[y] === 'undefined') {
context[y] = win[y];
}
}
context.Image = win.Image;
// needs to be overriden here specifically, but not sure why
// somehow allows external scripts to run in same context
context.document = win.document;
// Enhance toString output for DOM nodes
enhanceToString();
return context;
}
function reconfigure (options) {
this._dom.reconfigure(options);
}
function NodeAsBrowser () {
this._dom = null;
this.init = init;
this.reconfigure = reconfigure;
}
module.exports = new NodeAsBrowser();