-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (72 loc) · 2.66 KB
/
index.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
'use strict';
const Mirror = require('mirror-mirror');
const _ = require('lodash');
module.exports = (conf) => {
const mirror = Mirror(conf || {});
return {
setup: (options) => {
let afterActions = [];
if(options.preRendering){
afterActions = options.preRendering;
}
afterActions.push((nigthmare) => {
return nigthmare.evaluate(() => {
window.oc = window.oc || {};
window.oc.cmd = window.oc.cmd || [];
window.oc.cmd.push((oc) => {
const $el = oc.$(window.ohSee.selector);
oc.renderNestedComponent($el, () => window.ohSee.rendered = true);
});
});
});
afterActions.push(nigthmare => nigthmare.wait(() => window.ohSee.rendered === true));
if(options.postRendering){
_.each(options.postRendering, option => afterActions.push(option));
}
const component = `oc-component[data-name="${options.componentName}"]`;
return mirror.setup({
concurrency: options.concurrency,
cookies: options.cookies,
debug: options.debug,
headers: options.headers,
retries: options.retries,
screenshotsPath: options.screenshotsPath,
timeout: options.timeout,
urls: options.urls,
viewports: options.viewports,
selector: component,
before: [
(nigthmare) => {
return nigthmare.evaluate((transformation, selector, componentName, tryAppendLang) => {
const html = document.querySelector('html');
window.ohSee = {
componentName,
rendered: false,
selector,
transformation,
tryAppendLang: tryAppendLang || false
};
if(html){
window.ohSee.lang = html.getAttribute('lang');
}
}, options.transformation, component, options.componentName, options.tryAppendLang);
}
],
transform: (selected) => {
let ohSee = window.ohSee,
currentHref = selected.getAttribute('href'),
oldUrl = ohSee.transformation.oldUrl,
newUrl = ohSee.transformation.newUrl,
newHref = currentHref.replace(oldUrl, newUrl);
if(ohSee.tryAppendLang && !!ohSee.lang){
const hasQs = newHref.indexOf('?') >= 0;
newHref += `${hasQs ? '&' : '?'}__ocAcceptLanguage=${ohSee.lang}`;
}
return `<oc-component href="${newHref}" data-name="${window.ohSee.componentName}"></oc-component>`;
},
after: afterActions
});
},
run: callback => mirror.run(callback)
};
};