-
-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy pathanalyzer.js
129 lines (115 loc) · 3.43 KB
/
analyzer.js
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
'use strict';
const merge = require('lodash.merge');
const forEach = require('lodash.foreach');
const path = require('path');
const browsertime = require('browsertime');
const set = require('lodash.set');
const get = require('lodash.get');
const coach = require('webcoach');
const browserScripts = browsertime.browserScripts;
const defaultBrowsertimeOptions = {
statistics: true
};
const iphone6UserAgent =
'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 ' +
'(KHTML, like Gecko) Version/6.0 Mobile/10B329 Safari/8536.25';
async function parseUserScripts(scripts) {
if (!Array.isArray(scripts)) scripts = [scripts];
const allUserScripts = {};
for (let script of scripts) {
const myScript = await browserScripts.findAndParseScripts(
path.resolve(script),
'custom'
);
merge(allUserScripts, myScript);
}
return allUserScripts;
}
async function addCoachScripts(scripts) {
const coachAdvice = await coach.getDomAdvice();
scripts.coach = {
coachAdvice: coachAdvice
};
return scripts;
}
function addExtraScripts(scriptsByCategory, pluginScripts) {
// For all different script in the array
for (let scripts of pluginScripts) {
// and then for all scripts in that category
forEach(scripts.scripts, function(script, name) {
set(scriptsByCategory, scripts.category + '.' + name, script);
});
}
return scriptsByCategory;
}
function setupAsynScripts(asyncScripts) {
const allAsyncScripts = {};
// For all different script in the array
for (let scripts of asyncScripts) {
// and then for all scripts in that category
forEach(scripts.scripts, function(script, name) {
set(allAsyncScripts, scripts.category + '.' + name, script);
});
}
return allAsyncScripts;
}
module.exports = {
async analyzeUrl(
url,
scriptOrMultiple,
pluginScripts,
pluginAsyncScripts,
options
) {
const btOptions = merge({}, defaultBrowsertimeOptions, options);
// set mobile options
if (options.mobile) {
btOptions.viewPort = '360x640';
if (btOptions.browser === 'chrome') {
const emulation = get(
btOptions,
'chrome.mobileEmulation.deviceName',
'iPhone 6'
);
btOptions.chrome.mobileEmulation = {
deviceName: emulation
};
} else {
btOptions.userAgent = iphone6UserAgent;
}
}
const scriptCategories = await browserScripts.allScriptCategories;
let scriptsByCategory = await browserScripts.getScriptsForCategories(
scriptCategories
);
if (btOptions.script) {
const userScripts = await parseUserScripts(btOptions.script);
scriptsByCategory = merge(scriptsByCategory, userScripts);
}
if (btOptions.coach) {
scriptsByCategory = addCoachScripts(scriptsByCategory);
}
scriptsByCategory = await addExtraScripts(scriptsByCategory, pluginScripts);
const engine = new browsertime.Engine(btOptions);
const asyncScript =
pluginAsyncScripts.length > 0
? await setupAsynScripts(pluginAsyncScripts)
: undefined;
try {
await engine.start();
if (scriptOrMultiple) {
const res = await engine.runMultiple(
url,
scriptsByCategory,
asyncScript
);
return res;
} else {
const res = await engine.run(url, scriptsByCategory, asyncScript);
return res;
}
} finally {
await engine.stop();
}
}
};