-
Notifications
You must be signed in to change notification settings - Fork 2
/
injected.js
171 lines (146 loc) · 5.65 KB
/
injected.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
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
'use strict';
function _mojitoSendMessageToPanel(payload) {
window.postMessage({ from: 'mojito', target: 'Panel', payload }, '*');
}
function _mojitoSendMessageToBackground(payload) {
window.postMessage({ from: 'mojito', target: 'Background', payload }, '*');
}
// detecting if Mojito is running in current page
function _mojitoDetect(forPanel, devtoolsOnly) {
if (window._mojitoDetectCnt == null) {
window._mojitoDetectCnt = 0;
window._mojitoMaxDetectCnt = 60;
}
window._mojitoDetectCnt++;
// detect up to 60 times (30 seconds)
if (window._mojitoDetectCnt > window._mojitoMaxDetectCnt) {
// notify inspector panel, No Mojito found
_mojitoSendMessageToPanel({ mojitoDetected: false });
return;
}
if (window.Mojito) {
if (forPanel) {
if (devtoolsOnly) {
Mojito.utils.domReady(() => _mojitoSendMessageToPanel({ mojitoDetected: true, devtoolsOnly: true, count: getActivatedCount() }));
}
else {
Mojito.utils.domReady(() => _mojitoSendMessageToPanel({ mojitoDetected: true, count: getActivatedCount() }))
}
}
else {
// change extension icon to enable state
_mojitoSendMessageToBackground({ mojitoDetected: true, mojitoVersion: window.Mojito.buildInfo.mojitoVersion, containerName: window.Mojito.buildInfo.container, pageHostname: location.hostname });
setTimeout(() => {
_mojitoSendMessageToBackground({ mojitoDetected: true, count: getActivatedCount(), mojitoVersion: window.Mojito.buildInfo.mojitoVersion, containerName: window.Mojito.buildInfo.container, pageHostname: location.hostname });
}, 500);
}
}
else {
setTimeout(function () {
_mojitoDetect(forPanel, devtoolsOnly);
}, 500);
}
}
function getActivatedCount() {
let tests = Mojito.testObjects || {},
activatedCount = 0;
for (let key in tests) {
if (tests[key].activated) {
activatedCount++;
}
}
return activatedCount;
}
function _mojitoGetTestData() {
let tests = Mojito.testObjects || {},
test,
data = [],
item,
buildInfo = Mojito.buildInfo||{size: {experiments: {}}};
for (let key in tests) {
test = tests[key];
item = {};
item.id = key;
item.name = test.options.name;
item.state = test.options.state;
item.divertTo = test.options.divertTo;
item.excludedBySample = test.inTest() == '0';
item.activated = test.activated;
item.chosenRecipe = test.chosenRecipe;
item.sampleRate = test.options.sampleRate;
item.size = buildInfo.size.experiments[key]||'N/A';
item.trigger = test.options.trigger.toString();
item.recipes = [];
for (let recipeId in test.options.recipes) {
item.recipes.push({
id: recipeId,
name: test.options.recipes[recipeId]['name'],
sampleRate: test.options.recipes[recipeId]['simpleRate']
});
}
data.push(item);
}
// url parameters
let params = Mojito.utils.parseUrlParameters(window.location.search);
let buildData = JSON.parse(JSON.stringify(buildInfo));
buildData.pageHostname = location.hostname;
return { tests: data, params, buildInfo: buildData};
}
const TEST_STATES = { STAGING: 'staging', LIVE: 'live', DIVERT: 'divert' };
function _mojitoApplyChanges(changes) {
let changeItem,
params = Mojito.utils.parseUrlParameters(window.location.search),
test;
for (let i = 0; i < changes.length; i++) {
changeItem = changes[i];
test = Mojito.testObjects[changeItem.id];
if (changeItem.state == TEST_STATES.STAGING) {
if (changeItem.newRecipe == 'exit-preview') {
// delete cookie
_mojitoDeleteCookie(`_mojito_${changeItem.id}-staging`);
}
// remove old url param
if (changeItem.oldRecipe) {
delete params[`mojito_${changeItem.id}`];
}
// append new url param
if (changeItem.newRecipe != 'exit-preview') {
params[`mojito_${changeItem.id}`] = changeItem.newRecipe;
}
}
else if (changeItem.state == TEST_STATES.LIVE) {
// excluded by sample
if (changeItem.oldRecipe == null) {
test.setInTest(1);
test.setRecipe(changeItem.newRecipe);
}
// remove old url param
delete params[`mojito_${changeItem.id}`];
// append new url param
params[`mojito_${changeItem.id}`] = changeItem.newRecipe;
}
else {
// divert
// delete old cookie
_mojitoDeleteCookie(`_mojito_${changeItem.id}`);
// remove old url param
delete params[`mojito_${changeItem.id}`];
// append new url param if non-divertTo recipe chosen
if (changeItem.newRecipe != test.options.divertTo) {
params[`mojito_${changeItem.id}`] = changeItem.newRecipe;
}
}
}
// re-assemble url params
let newParams = [];
for (let key in params) {
if (key != '') {
newParams.push(key + '=' + params[key]);
}
}
location.replace(location.protocol + '//' + location.hostname + location.pathname + (newParams.length ? ('?' + newParams.join('&')) : '') + location.hash);
}
function _mojitoDeleteCookie(name) {
document.cookie = name + '=;expires=Sat, 31 Jan 1970 16:00:00 GMT;path=/';
}
window.setTimeout(_mojitoDetect);