-
Notifications
You must be signed in to change notification settings - Fork 0
/
windu.js
338 lines (338 loc) · 17.1 KB
/
windu.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
window.windu = {
randomKey: function (p) {
function randomCharacter() {
var char = '';
if (Math.floor(1 + Math.random() * 2) === 1) {
char = String.fromCharCode(Math.floor(65 + Math.random() * 23));
} else {
char = String.fromCharCode(Math.floor(97 + Math.random() * 23));
}
return char;
};
var ol = p || 24,
init = Math.floor((1 + Math.random()) * 0x10000) * Date.now(),
iS = init.toString(),
iCN = Math.floor(12 + Math.random() * 42);
for (i = 0; i < iCN; i++) {
var iL = iS.length,
iP = Math.floor(1 + Math.random() * iL);
iS = [iS.slice(0, iP), randomCharacter(), iS.slice(iP)].join('');
}
sub = Math.floor(1 + Math.random() * (iS.length - ol));
use = iS.substring(sub, sub + ol);
return use;
},
consoleLogOn: false,
consoleLog: function () {
if (arguments[0]) {
console.log.apply(this, [].slice.call(arguments, 1));
}
},
simplifyString: function (str, connector) {
var c = typeof connector === 'undefined' ? '-' : connector;
return str.replace(/[^\w]/gi, c).replace(/-{,}/g, c);
},
data: {},
updateData: function (status, json) {
json.status = status;
window.windu.data[json.name] = json;
},
create: function (jsonOrStr, doFunction) {
if (typeof jsonOrStr === 'string') {
if (typeof doFunction === 'function') {
var json = {
selector: jsonOrStr,
attach: doFunction
}
} else {
console.warn('If Windu shorthand is being used, than the second parameter must be both provided & a function.');
}
} else {
var json = jsonOrStr;
};
json.detectType = !!json.detectType ? this.simplifyString(json.detectType) : 'mutation';
json.name = !!json.name ? this.simplifyString(json.name) : 'zz_' + json.detectType + '_' + this.randomKey(10);
if (json.detectType === 'mutation') {
if (!!json.attach) {
var showIndex = false;
if (typeof json.attach === 'function') {
var newJson = {
selector: json.selector,
do: json.attach
};
if (!!json.maxTimes) {
newJson.maxTimes = json.maxTimes;
}
json.attach = [newJson];
} else if (typeof json.attach.length === 'undefined') {
json.attach = [json.attach]
} else {
showIndex = json.attach.length > 1;
};
json.attach.forEach(function (obj, index) {
obj.name = json.name + '_attach' + (showIndex ? '_' + index : '');
obj.alreadyAttachedSelector = 'windu_' + obj.name;
obj.excludeSelector = ':not(.' + obj.alreadyAttachedSelector + ')';
window.windu.mutations.attachToElem.setup(obj);
});
this.consoleLog(json.consoleLog, json.name + ': setup attachToElem mutation function');
}
if (!!json.when && !!json.do) {
json.name = json.name + '_whenDo';
if (!!json.pollFirst) {
var pollWhen = typeof json.pollFirst === 'function' ? json.pollFirst : json.when,
pollDo = function () {
json.pollFirst = false;
json.detectType = 'mutation';
window.windu.updateData('polling finished', json);
window.windu.mutations.whenDo.setup(json);
window.windu.consoleLog(json.consoleLog, json.name + ': ran polling function');
};
this.create({
name: json.name,
when: pollWhen,
do: pollDo,
detectType: 'poll'
});
this.consoleLog(json.consoleLog, json.name + ': setup whenDo mutation poll function first');
} else {
window.windu.mutations.whenDo.setup(json);
this.consoleLog(json.consoleLog, json.name + ': setup whenDo mutation function');
}
}
} else if (/poll|script|((u|U)(r|R)(l|L))/.test(json.detectType)) {
json.reInit = json.reInit || false;
if (!this.data[json.name]) {
var cE = json.checkEvery ? json.checkEvery : 500;
this.data[json.name] = {
count: 0,
do: json.do,
test: json.when,
checkEvery: cE,
stopAfter: json.stopAfter ? (json.stopAfter / cE) : (15000 / cE),
status: 'initiated',
ifTimeOut: json.ifTimeOut ? json.ifTimeOut : 'standard',
run: function () {
if (window.windu.data[json.name].test()) {
try {
window.windu.data[json.name].do();
window.windu.data[json.name].status = 'completed';
} catch (err) {
console.log(json.name + ' poll err:', err.message);
window.windu.data[json.name].status = 'error: ' + err.message;
};
clearInterval(window.windu.data[json.name].interval);
} else if (window.windu.data[json.name].count > window.windu.data[json.name].stopAfter) {
clearInterval(window.windu.data[json.name].interval);
window.windu.data[json.name].status = 'timed out';
var tO = window.windu.data[json.name].ifTimeOut;
if (typeof tO === 'function') {
window.windu.consoleLog(json.consoleLog, json.name + ' is running provided timed out function');
tO();
} else if (tO === 'goforit') {
window.windu.consoleLog(json.consoleLog, json.name + ' is going for it anyway!');
window.windu.data[json.name].do();
} else if (tO === 'standard') {
window.windu.consoleLog(json.consoleLog, json.name + ' timed out.');
} else {
window.windu.consoleLog(json.consoleLog, tO);
}
} else {
window.windu.data[json.name].count++;
}
}
};
if (json.after) {
if (!!this.data[json.after]) {
var fCache = this.data[json.after].do;
this.data[json.after].do = function () {
fCache();
this.data[json.name].interval = setInterval(this.data[json.name].run, this.data[json.name].checkEvery);
};
} else {
this.consoleLog(json.consoleLog, json.after + ' interval is not defined and could not be chained');
}
} else {
this.data[json.name].interval = setInterval(this.data[json.name].run, this.data[json.name].checkEvery);
}
} else if (this.data[json.name].status === 'completed' && reInit) {
this.data[json.name].interval = setInterval(this.data[json.name].run, this.data[json.name].checkEvery);
this.consoleLog(json.consoleLog, json.name + ' interval is already defined and complete, but was reinitiated');
} else {
this.consoleLog(json.consoleLog, json.name + ' interval is already defined, ' + this.data[json.name].status + ', and was not reinitiated');
}
}
},
mutations: {
attachToElem: {
array: [],
active: false,
consoleLogOn: false,
setup: function (json) {
if (!!this.array.find(function (obj) {
return obj.name === json.name
})) {
console.log('"' + json.name + '" matching element attachment already exists.');
} else {
if (!json.alreadyAttachedSelector) {
json.alreadyAttachedSelector = 'winduS_' + json.name;
json.excludeSelector = ':not(.' + json.alreadyAttachedSelector + ')';
}
json.timesRun = 0;
json.maxTimes = json.maxTimes || 10000;
json.fromMutation = true;
json.useSelector = json.selector.replace(/,/g, json.excludeSelector + ',') + json.excludeSelector;
window.windu.updateData('attaching during setup', json);
Array.prototype.slice.call(document.querySelectorAll(json.useSelector)).forEach(function (elem) {
if (!!elem) {
json.do(elem);
elem.classList.add(json.alreadyAttachedSelector);
json.timesRun++;
window.windu.consoleLog(json.consoleLog, json.name + ' : ran do() function during attachToElem mutation setup : ' + elem);
}
});
if (json.timesRun < json.maxTimes) {
this.array.push(json);
window.windu.updateData('active in attach mutation observer', json);
window.windu.consoleLog(json.consoleLog, json.name + ' : added to attachToElem mutation observer list');
} else {
window.windu.updateData('active in attach mutation observer', json);
}
if (this.array.length > 0 && !this.active) {
this.mutation.observe(document.body, {
childList: true,
subtree: true
});
this.active = true;
window.windu.consoleLog(json.consoleLog, 'setup "attachToElem" mutation observer');
}
}
},
mutation: new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
mutation.addedNodes.forEach(function (node) {
window.windu.mutations.attachToElem.array.forEach(function (json, i) {
var elements = [];
if (!!node.matches && node.matches(json.useSelector)) {
elements.push(node);
}
if (!!node.querySelectorAll) {
elements = elements.concat([].slice.call(node.querySelectorAll(json.useSelector)));
}
elements.forEach(function (elem) {
if (!!elem) {
json.do(elem);
elem.classList.add(json.alreadyAttachedSelector);
window.windu.consoleLog(json.consoleLog, json.name + ': ran do() function attached to ' + json.selector);
json.timesRun++;
}
});
if (json.timesRun > json.maxTimes) {
if (!!json.consoleLog) {
window.windu.mutations.attachToElem.consoleLogOn = true;
}
window.windu.mutations.attachToElem.array.splice(i, 1);
window.windu.updateData('completedMaxTimes_' + json.maxTimes, json);
}
});
});
})
if (window.windu.mutations.attachToElem.array.length === 0 && window.winduLog.attachToElem.active) {
window.windu.mutations.attachToElem.mutation.disconnect();
window.windu.mutations.attachToElem.active = false;
window.windu.consoleLog(window.windu.mutations.attachToElem.consoleLogOn, 'disconnected "attachToElem" mutation observer');
}
})
},
whenDo: {
array: [],
active: false,
consoleLogOn: false,
setup: function (json) {
if (!!this.array.find(function (obj) {
return obj.name === json.name
})) {
console.log('"' + json.name + '" whenDo function already exists.');
} else {
json.timesRun = 0;
json.maxTimes = json.maxTimes || 1;
json.lastTrue = false;
json.timesFalse = 0;
json.bipolar = json.bipolar || false;
json.bipolarState = true;
json.status = 'running';
var test = json.when();
if (json.bipolar) {
test = test === json.bipolarState;
}
if (test) {
try {
json.do(json);
} catch (err) {
console.log('windu/whendo error: ', err);
}
json.lastTrue = true;
json.timesRun++;
if (json.bipolar) {
json.bipolarState = !json.bipolarState;
window.windu.consoleLog(json.consoleLog, json.name + ': bipolar reversed to "' + json.bipolarState + '"');
}
window.windu.consoleLog(json.consoleLog, json.name + ' : ran do() function during whenDo mutation setup');
}
if (json.timesRun < json.maxTimes) {
this.array.push(json);
window.windu.updateData('active in when/do mutation observer', json)
if (!this.active) {
this.mutation.observe(document.body, {
childList: true,
subtree: true,
attributes: true
});
this.active = true;
window.windu.consoleLog(json.consoleLog, 'setup "whenDo" mutation observer');
}
} else {
window.windu.updateData('completedMaxTimes_' + json.maxTimes, json);
}
}
},
mutation: new MutationObserver(function (mutations) {
window.windu.mutations.whenDo.array.forEach(function (json, i) {
var test = json.when();
if (json.bipolar) {
test = test === json.bipolarState;
}
if (test) {
try {
json.do(json);
} catch (err) {
console.log('windu/whendo error: ', err);
}
json.lastTrue = true;
json.timesRun++;
if (json.bipolar) {
json.bipolarState = !json.bipolarState;
window.windu.consoleLog(json.consoleLog, json.name + ': bipolar reversed to "' + json.bipolarState + '"');
}
} else {
json.lastTrue = false;
json.timesFalse++;
}
if (json.timesRun >= json.maxTimes) {
if (!!json.consoleLog) {
window.windu.mutations.whenDo.consoleLogOn = true;
}
window.windu.mutations.whenDo.array.splice(i, 1);
window.windu.updateData('completedMaxTimes_' + json.maxTimes, json);
}
window.windu.data[json.name] = json;
});
if (window.windu.mutations.whenDo.array.length < 1 && window.windu.mutations.whenDo.active) {
window.windu.mutations.whenDo.mutation.disconnect();
window.windu.mutations.whenDo.active = false;
window.windu.consoleLog(window.windu.mutations.whenDo.consoleLogOn, 'disconnected "whenDo" mutation observer');
}
})
}
}
}