-
Notifications
You must be signed in to change notification settings - Fork 27
/
set-constant.test.js
798 lines (687 loc) · 30.8 KB
/
set-constant.test.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/* eslint-disable no-underscore-dangle, no-console */
import { clearGlobalProps } from '../helpers';
const { test, module } = QUnit;
const name = 'set-constant';
const nativeConsole = console.log;
const afterEach = () => {
console.log = nativeConsole;
clearGlobalProps('hit', '__debug', 'counter');
};
module(name, { afterEach });
const runScriptletFromTag = (...args) => {
const params = {
name,
args,
verbose: true,
};
const script = document.createElement('script');
script.textContent = window.scriptlets.invoke(params);
document.body.append(script);
};
const runUboAliasFromTag = (...args) => {
const params = {
name: 'ubo-set-constant',
args,
verbose: true,
};
const script = document.createElement('script');
script.textContent = window.scriptlets.invoke(params);
document.body.append(script);
};
const addSetPropTag = (property, value) => {
const script = document.createElement('script');
script.textContent = `window['${property}'] = ${value};`;
document.body.append(script);
};
/**
* document.body.append does not work in Edge 15
* https://caniuse.com/mdn-api_element_append
*/
const isSupported = (() => typeof document.body.append !== 'undefined')();
test('Checking if alias name works', (assert) => {
const adgParams = {
name,
engine: 'test',
verbose: true,
};
const uboParams = {
name: 'ubo-set-constant.js',
engine: 'test',
verbose: true,
};
const codeByAdgParams = window.scriptlets.invoke(adgParams);
const codeByUboParams = window.scriptlets.invoke(uboParams);
assert.strictEqual(codeByAdgParams, codeByUboParams, 'ubo name - ok');
});
if (!isSupported) {
test('unsupported', (assert) => {
assert.ok(true, 'Browser does not support it');
});
} else {
test('sets values correctly', (assert) => {
// settings constant to true;
const trueProp = 'trueProp01';
runScriptletFromTag(trueProp, 'true');
assert.strictEqual(window[trueProp], true);
clearGlobalProps(trueProp);
// setting constant to false;
const falseProp = 'falseProp';
runScriptletFromTag(falseProp, 'false');
assert.strictEqual(window[falseProp], false);
clearGlobalProps(falseProp);
// setting constant to undefined;
const undefinedProp = 'undefinedProp';
runScriptletFromTag(undefinedProp, 'undefined');
assert.strictEqual(window[undefinedProp], undefined);
clearGlobalProps(undefinedProp);
// setting constant to null;
const nullProp1 = 'nullProp1';
runScriptletFromTag(nullProp1, 'null');
assert.strictEqual(window[nullProp1], null);
clearGlobalProps(nullProp1);
// setting constant to empty array
const emptyArr = 'emptyArr';
runScriptletFromTag(emptyArr, 'emptyArr');
assert.ok(window[emptyArr] instanceof Array);
assert.strictEqual(window[emptyArr].length, 0);
clearGlobalProps(emptyArr);
// setting constant to empty object
const emptyObj = 'emptyObj';
runScriptletFromTag(emptyObj, 'emptyObj');
assert.ok(window[emptyObj] instanceof Object);
assert.strictEqual(Object.keys(window[emptyObj]).length, 0);
clearGlobalProps(emptyObj);
// setting constant to noopFunc;
const noopFuncProp = 'noopFuncProp';
runScriptletFromTag(noopFuncProp, 'noopFunc');
assert.strictEqual(window[noopFuncProp](), undefined);
clearGlobalProps(noopFuncProp);
// setting constant to noopCallbackFunc;
const noopCallbackFuncProp = 'noopCallbackFunc';
runScriptletFromTag(noopCallbackFuncProp, 'noopCallbackFunc');
const noopFuncCb = window[noopCallbackFuncProp]();
assert.ok(typeof noopFuncCb === 'function', 'returns function');
assert.strictEqual(noopFuncCb(), undefined, 'function returns undefined');
clearGlobalProps(noopCallbackFuncProp);
// setting constant to noopCallbackFunc;
const throwFuncProp = 'throwFunc';
runScriptletFromTag(throwFuncProp, 'throwFunc');
assert.throws(() => window[throwFuncProp]()(), 'throwFuncProp throws an error');
clearGlobalProps(throwFuncProp);
// setting constant to trueFunc;
const trueFuncProp = 'trueFuncProp';
runScriptletFromTag(trueFuncProp, 'trueFunc');
assert.strictEqual(window[trueFuncProp](), true);
clearGlobalProps(trueFuncProp);
// setting constant to falseFunc;
const falseFuncProp = 'falseFuncProp';
runScriptletFromTag(falseFuncProp, 'falseFunc');
assert.strictEqual(window[falseFuncProp](), false);
clearGlobalProps(falseFuncProp);
// setting constant to noopPromiseReject;
const noopPromiseRejectProp = 'noopPromiseRejectProp';
runScriptletFromTag(noopPromiseRejectProp, 'noopPromiseReject');
assert.rejects(window[noopPromiseRejectProp]());
clearGlobalProps(noopPromiseRejectProp);
// setting constant to noopPromiseResolve;
const noopPromiseResolveProp = 'noopPromiseResolveProp';
runScriptletFromTag(noopPromiseResolveProp, 'noopPromiseResolve');
window[noopPromiseResolveProp]().then((response) => {
assert.ok(response);
});
clearGlobalProps(noopPromiseResolveProp);
// setting constant to number;
const numberProp = 'numberProp';
runScriptletFromTag(numberProp, 111);
assert.strictEqual(window[numberProp], 111);
clearGlobalProps(numberProp);
// setting constant to -1;
const minusOneProp = 'minusOneProp';
runScriptletFromTag(minusOneProp, '-1');
assert.strictEqual(window[minusOneProp], -1);
clearGlobalProps(minusOneProp);
// setting constant to empty string;
const emptyStringProp = 'emptyStringProp';
runScriptletFromTag(emptyStringProp, '');
assert.strictEqual(window[emptyStringProp], '');
clearGlobalProps(emptyStringProp);
// setting constant to 'yes';
const yesStringProp = 'yesStringProp';
runScriptletFromTag(yesStringProp, 'yes');
assert.strictEqual(window[yesStringProp], 'yes');
clearGlobalProps(yesStringProp);
// setting constant to 'no';
const noStringProp = 'noStringProp';
runScriptletFromTag(noStringProp, 'no');
assert.strictEqual(window[noStringProp], 'no');
clearGlobalProps(noStringProp);
// setting constant to illegalNumber doesn't works;
const illegalNumberProp = 'illegalNumberProp';
runScriptletFromTag(illegalNumberProp, 32768);
assert.strictEqual(window[illegalNumberProp], undefined);
clearGlobalProps(illegalNumberProp);
});
test('keep other keys after setting a value', (assert) => {
window.testObj = {
testChain: null,
};
runScriptletFromTag('testObj.testChain.testProp', 'true');
window.testObj.testChain = {
testProp: false,
otherProp: 'someValue',
testMethod: () => { },
};
assert.strictEqual(window.testObj.testChain.testProp, true, 'target prop set');
assert.strictEqual(window.testObj.testChain.otherProp, 'someValue', 'sibling value property is kept');
assert.strictEqual(typeof window.testObj.testChain.testMethod, 'function', 'sibling function property is kept');
clearGlobalProps('testObj');
});
test('set value on null prop', (assert) => {
// end prop is null
window.nullProp2 = null;
runScriptletFromTag('nullProp2', 15);
assert.strictEqual(window.nullProp2, 15, 'null end prop changed');
clearGlobalProps('nullProp2');
});
test('set value through chain with empty object', (assert) => {
window.emptyObj = {};
runScriptletFromTag('emptyObj.a.prop', 'true');
window.emptyObj.a = {};
assert.strictEqual(window.emptyObj.a.prop, true, 'target prop set');
clearGlobalProps('emptyObj');
});
test('set value through chain with null', (assert) => {
// null prop in chain
window.nullChain = {
nullProp: null,
};
runScriptletFromTag('nullChain.nullProp.endProp', 'true');
window.nullChain.nullProp = {
endProp: false,
};
assert.strictEqual(window.nullChain.nullProp.endProp, true, 'chain with null trapped');
clearGlobalProps('nullChain');
});
test('sets values to the chained properties', (assert) => {
window.chained = { property: {} };
runScriptletFromTag('chained.property.aaa', 'true');
assert.strictEqual(window.chained.property.aaa, true);
clearGlobalProps('chained');
});
test('sets values on the same chain (defined)', (assert) => {
window.chained = { property: {} };
runScriptletFromTag('chained.property.aaa', 'true');
runScriptletFromTag('chained.property.bbb', 10);
assert.strictEqual(window.chained.property.aaa, true);
assert.strictEqual(window.chained.property.bbb, 10);
clearGlobalProps('chained');
});
test('sets values on the same chain (undefined)', (assert) => {
runScriptletFromTag('chained.property.aaa', 'true');
runScriptletFromTag('chained.property.bbb', 10);
window.chained = { property: {} };
assert.strictEqual(window.chained.property.aaa, true);
assert.strictEqual(window.chained.property.bbb, 10);
clearGlobalProps('chained');
});
test('values with same types are not overwritten, values with different types are overwritten', (assert) => {
const property = 'customProperty';
const firstValue = 10;
const anotherValue = 100;
const anotherTypeValue = true;
runScriptletFromTag(property, firstValue);
assert.strictEqual(window[property], firstValue);
addSetPropTag(property, anotherValue);
assert.strictEqual(window[property], firstValue, 'values with same types are not overwritten');
addSetPropTag(property, anotherTypeValue);
assert.strictEqual(window[property], anotherTypeValue, 'values with different types are overwritten');
clearGlobalProps(property);
});
test('sets values correctly + stack match', (assert) => {
const stackMatch = 'set-constant';
const trueProp = 'trueProp02';
runScriptletFromTag(trueProp, 'true', stackMatch);
assert.strictEqual(window[trueProp], true, 'stack match: trueProp - ok');
clearGlobalProps(trueProp);
const numProp = 'numProp';
runScriptletFromTag(numProp, 123, stackMatch);
assert.strictEqual(window[numProp], 123, 'stack match: numProp - ok');
clearGlobalProps(numProp);
});
test('sets values correctly + no stack match', (assert) => {
window.chained = { property: {} };
const stackNoMatch = 'no_match.js';
runScriptletFromTag('chained.property.aaa', 'true', stackNoMatch);
assert.strictEqual(window.chained.property.aaa, undefined);
clearGlobalProps('chained');
const property = 'customProp';
const firstValue = 10;
runScriptletFromTag(property, firstValue, stackNoMatch);
assert.strictEqual(window[property], undefined);
clearGlobalProps(property);
});
test('set-constant: does not work - invalid regexp pattern for stack arg', (assert) => {
const stackArg = '/\\/';
const property = 'customProp';
const value = 10;
runScriptletFromTag(property, value, stackArg);
assert.strictEqual(window[property], undefined, 'property should not be set');
clearGlobalProps(property);
});
test('no value setting if chain is not relevant', (assert) => {
window.chain = { property: {} };
runScriptletFromTag('noprop.property.aaa', 'true');
assert.deepEqual(window.chain.property, {}, 'predefined obj was not changed');
assert.strictEqual(window.noprop, undefined, '"noprop" was not set');
clearGlobalProps('chain');
});
test('no value setting if some property in chain is undefined while loading', (assert) => {
const testObj = { prop: undefined };
window.chain = testObj;
runScriptletFromTag('chain.prop.aaa', 'true');
assert.deepEqual(window.chain, testObj, 'predefined obj was not changed');
clearGlobalProps('chain');
});
test('no value setting if first property in chain is null', (assert) => {
window.chain = null;
runScriptletFromTag('chain.property.aaa', 'true');
assert.strictEqual(window.chain, null, 'predefined obj was not changed');
clearGlobalProps('chain');
});
// for now the scriptlet does not set the chained property if one of chain prop is null.
// that might happen, for example, while loading the page.
// after the needed property is loaded, the scriptlet does not check it and do not set the value
// https://github.com/AdguardTeam/Scriptlets/issues/128
test('set value after timeout if it was null earlier', (assert) => {
window.chain = null;
runScriptletFromTag('chain.property.aaa', 'true');
assert.strictEqual(window.chain, null, 'predefined obj was not changed');
const done = assert.async();
setTimeout(() => {
window.chain = { property: {} };
}, 50);
setTimeout(() => {
assert.strictEqual(window.chain.property.aaa, undefined, 'chained prop was NOT set after delay');
done();
}, 100);
clearGlobalProps('chain');
});
test('set value after loop reassignment', (assert) => {
window.loopObj = {
chainProp: {
aaa: true,
},
};
runScriptletFromTag('loopObj.chainProp.bbb', '1');
// eslint-disable-next-line no-self-assign
window.loopObj = window.loopObj;
window.loopObj.chainProp.bbb = 0;
assert.strictEqual(window.loopObj.chainProp.bbb, 1, 'value set after loop reassignment');
clearGlobalProps('loopObj');
});
test('trying to set non-configurable silently exits', (assert) => {
assert.expect(2);
console.log = function log(input) {
assert.ok(input.includes('testProp'), 'non-configurable prop logged');
};
Object.defineProperty(window, 'testProp', {
value: 5,
configurable: false,
});
runScriptletFromTag('window.testProp', '0');
assert.strictEqual(window.testProp, 5, 'error avoided');
clearGlobalProps('testProp');
});
test('set value after undef object been created by new Function()', (assert) => {
function InitFunc() {
this.testFunc = () => 5;
}
runScriptletFromTag('window.a.b.c.testFunc', 'noopFunc');
window.a = {
b: {},
};
window.a.b.c = new InitFunc();
const result = window.a.b.c.testFunc();
assert.strictEqual(result, undefined, 'Value was set');
clearGlobalProps('a');
});
test('value wrappers returning correct values', (assert) => {
// Test asFunc
window.funcProp = null;
runScriptletFromTag('window.funcProp', 'yes', '', 'asFunction');
let func = window.funcProp;
assert.strictEqual(typeof func, 'function', 'Function was set');
let value = func();
assert.strictEqual(value, 'yes', 'function returns correct value');
// Test asCallback
window.callbackProp = null;
runScriptletFromTag('window.callbackProp', 'emptyArr', '', 'asCallback');
func = window.callbackProp;
assert.strictEqual(typeof func, 'function', 'Function was set');
const callback = func();
assert.strictEqual(typeof callback, 'function', 'Function returns callback');
value = callback();
assert.ok(Array.isArray(value), 'callback returns array');
assert.strictEqual(value.length, 0, 'callback returns empty array');
// Test asResolved
window.resolvedPromise = null;
runScriptletFromTag('window.resolvedPromise', 'noopFunc', '', 'asResolved');
window.resolvedPromise.then((func) => {
assert.strictEqual(typeof func, 'function', 'Promise resolved with correct value');
assert.strictEqual(func(), undefined, 'function', 'Promise resolved with correct value');
});
// Test asRejected
window.rejectedPromise = null;
runScriptletFromTag('window.rejectedPromise', 42, '', 'asRejected');
window.rejectedPromise
.catch((reason) => {
assert.strictEqual(reason, 42, 'Promise rejected with correct value');
});
clearGlobalProps('funcProp', 'callbackProp', 'resolvedPromise', 'rejectedPromise');
});
test('value wrapper argument correctly reorganized for ubo-set-constant', (assert) => {
// 'asFunction' will be moved from third argument (stack) to fourth argument (valueWrapper)
runUboAliasFromTag('window.funcProp', 'yes', 'asFunction');
let func = window.funcProp;
assert.strictEqual(typeof func, 'function', 'Function was set');
assert.strictEqual(func(), 'yes', 'get correct value after arguments swap');
clearGlobalProps('funcProp');
// '3' will be discarded
runUboAliasFromTag('window.funcProp', 'yes', '3');
func = window.funcProp;
assert.strictEqual(typeof func, 'function', 'Function was set');
assert.strictEqual(func(), 'yes', 'get correct value after skipping defer arg');
clearGlobalProps('funcProp');
});
// https://github.com/AdguardTeam/Scriptlets/issues/330
test('Check overriden value - array', (assert) => {
runScriptletFromTag('pageData.__banners.0.commercial.mediaUrl', '');
const done = assert.async();
window.pageData = {
__banners: [{
commercial: {
mediaUrl: 'test',
},
}, {
commercial: {
mediaUrl: 'test',
},
}],
};
setTimeout(() => {
window.pageData.__banners = [{
commercial: {
mediaUrl: 'test',
},
}, {
commercial: {
mediaUrl: 'test',
},
}];
}, 50);
setTimeout(() => {
const result = window.pageData.__banners[0].commercial.mediaUrl;
assert.strictEqual(result, '', 'pageData.__banners.0.commercial.mediaUrl was set to empty string');
done();
clearGlobalProps('pageData');
}, 100);
});
// https://github.com/AdguardTeam/Scriptlets/issues/330
test('Check overriden - array 2', (assert) => {
runScriptletFromTag('pageData.__banners.0.commercial.mediaUrl', '');
const done = assert.async();
window.pageData = {
__banners: [{
commercial: {
mediaUrl: 'test',
jumpUrl: 'test',
title: 'test',
notChange: 'test',
update: false,
},
}, {
commercial: {
mediaUrl: 'test',
},
}],
};
setTimeout(() => {
window.pageData.__banners = [{
commercial: {
mediaUrl: 'test',
jumpUrl: 'test1',
title: 'test1',
notChange: 'test',
asdf: 'test',
update: true,
},
}, {
commercial: {
mediaUrl: 'test',
foo: 1,
},
}];
}, 50);
setTimeout(() => {
// Should be changed
const result1 = window.pageData.__banners[0].commercial.mediaUrl;
assert.strictEqual(result1, '', 'pageData.__banners.0.commercial.mediaUrl was set to empty string');
// Should NOT be changed by scriptlet
const result2 = window.pageData.__banners[0].commercial.jumpUrl;
const result3 = window.pageData.__banners[0].commercial.title;
const result4 = window.pageData.__banners[0].commercial.notChange;
const result5 = window.pageData.__banners[0].commercial.asdf;
const result6 = window.pageData.__banners[0].commercial.update;
const result7 = window.pageData.__banners[1].commercial.mediaUrl;
const result8 = window.pageData.__banners[1].commercial.foo;
// eslint-disable-next-line max-len
assert.strictEqual(result2, 'test1', 'pageData.__banners.0.commercial.jumpUrl was not changed by scriptlet');
assert.strictEqual(result3, 'test1', 'pageData.__banners.0.commercial.title was not changed by scriptlet');
assert.strictEqual(result4, 'test', 'pageData.__banners.0.commercial.notChange was not changed');
assert.strictEqual(result5, 'test', 'pageData.__banners.0.commercial.asdf was not changed');
assert.strictEqual(result6, true, 'pageData.__banners.0.commercial.update was not changed by scriptlet');
assert.strictEqual(result7, 'test', 'pageData.__banners.1.commercial.mediaUrl was not changed');
assert.strictEqual(result8, 1, 'pageData.__banners.1.commercial.foo was not changed');
done();
clearGlobalProps('pageData');
}, 100);
});
// Fixing this issue will require synchronization between the scriptlets
// TODO: uncomment test when synchronization between the scriptlets is added
// https://github.com/AdguardTeam/Scriptlets/issues/330
// test('Check overriden few values - array', (assert) => {
// runScriptletFromTag('pageData.__banners.0.commercial.mediaUrl', '');
// runScriptletFromTag('pageData.__banners.0.commercial.jumpUrl', '');
// runScriptletFromTag('pageData.__banners.0.commercial.title', '');
// runScriptletFromTag('abcd.__banners.0.commercial.update', '');
// const done = assert.async();
// window.pageData = {
// __banners: [{
// commercial: {
// mediaUrl: 'test',
// jumpUrl: 'test',
// title: 'test',
// notChange: 'test',
// update: false,
// },
// }, {
// commercial: {
// mediaUrl: 'test',
// },
// }],
// };
// setTimeout(() => {
// window.pageData.__banners = [{
// commercial: {
// mediaUrl: 'test',
// jumpUrl: 'test',
// title: 'test',
// notChange: 'test',
// asdf: 'test',
// update: true,
// },
// }, {
// commercial: {
// mediaUrl: 'test',
// foo: 1,
// },
// }];
// }, 50);
// setTimeout(() => {
// // Should be changed
// const result1 = window.pageData.__banners[0].commercial.mediaUrl;
// const result2 = window.pageData.__banners[0].commercial.jumpUrl;
// const result3 = window.pageData.__banners[0].commercial.title;
// assert.strictEqual(result1, '', 'pageData.__banners.0.commercial.mediaUrl was set to empty string');
// assert.strictEqual(result2, '', 'pageData.__banners.0.commercial.jumpUrl was set to empty string');
// assert.strictEqual(result3, '', 'pageData.__banners.0.commercial.title was set to empty string');
// // Should NOT be changed by scriptlet
// const result4 = window.pageData.__banners[0].commercial.notChange;
// const result5 = window.pageData.__banners[0].commercial.asdf;
// const result6 = window.pageData.__banners[0].commercial.update;
// const result7 = window.pageData.__banners[1].commercial.mediaUrl;
// const result8 = window.pageData.__banners[1].commercial.foo;
// assert.strictEqual(result4, 'test', 'pageData.__banners.0.commercial.notChange was not changed');
// assert.strictEqual(result5, 'test', 'pageData.__banners.0.commercial.asdf was not changed');
// assert.strictEqual(result6, true, 'pageData.__banners.0.commercial.update was not changed by scriptlet');
// assert.strictEqual(result7, 'test', 'pageData.__banners.1.commercial.mediaUrl was not changed');
// assert.strictEqual(result8, 1, 'pageData.__banners.1.commercial.foo was not changed');
// done();
// clearGlobalProps('pageData');
// }, 100);
// });
// https://github.com/AdguardTeam/Scriptlets/issues/330
test('Check overriden value - object', (assert) => {
runScriptletFromTag('foo.prototype.abc.qwerty', 'false');
window.foo = function name() { };
window.foo.prototype = { bar: 1 };
window.foo.prototype.abc = {
qwerty: true,
};
const result = window.foo.prototype.abc.qwerty;
assert.strictEqual(result, false, 'foo.prototype.abc.qwerty was set to false');
clearGlobalProps('foo');
});
// https://github.com/AdguardTeam/Scriptlets/issues/330
test('Override value 2 times - object', (assert) => {
runScriptletFromTag('foo.prototype.abc.qwerty', 'false');
window.foo = function name() { };
window.foo.prototype = { bar: 1 };
window.foo.prototype.abc = {
boo: true,
};
window.foo.prototype.abc = {
qwerty: true,
};
const result = window.foo.prototype.abc.qwerty;
assert.strictEqual(result, false, 'foo.prototype.abc.qwerty was set to false');
clearGlobalProps('foo');
});
// Fixing this issue will require synchronization between the scriptlets
// TODO: uncomment test when synchronization between the scriptlets is added
// https://github.com/AdguardTeam/Scriptlets/issues/330
// test('Check overriden few values - object', (assert) => {
// runScriptletFromTag('foo.prototype.abc.qwerty', 'false');
// runScriptletFromTag('foo.prototype.bar', '0');
// window.foo = function name() { };
// window.foo.prototype = { bar: 1 };
// window.foo.prototype.abc = {
// qwerty: true,
// };
// const result1 = window.foo.prototype.abc.qwerty;
// const result2 = window.foo.prototype.bar;
// assert.strictEqual(result1, false, 'foo.prototype.abc.qwerty was set to false');
// assert.strictEqual(result2, 0, 'foo.prototype.bar was set to 0');
// clearGlobalProps('foo');
// });
// https://github.com/AdguardTeam/Scriptlets/issues/330
test('Check overriden value - object + similar object which should not be overriden', (assert) => {
runScriptletFromTag('foo.prototype.abc.qwerty', 'false');
window.foo = function name() { };
window.foo.prototype = { bar: 1 };
window.foo.prototype.abc = {
qwerty: true,
};
window.foo.prototype.zxc = {
abc: {
qwerty: true,
},
};
const result1 = window.foo.prototype.abc.qwerty;
assert.strictEqual(result1, false, 'foo.prototype.abc.qwerty was set to false');
const result2 = window.foo.prototype.zxc.abc.qwerty;
assert.strictEqual(result2, true, 'foo.prototype.zxc.abc.qwerty was not changed');
clearGlobalProps('foo');
});
// https://github.com/AdguardTeam/Scriptlets/issues/330
test('Check overriden value - object, do not overwrite', (assert) => {
runScriptletFromTag('foo.prototype.abc.qwerty', 'false');
runScriptletFromTag('baz.prototype.not.qwerty', 'false');
window.baz = function name() { };
window.baz.prototype = { bar: 1 };
window.baz.prototype.abc = {
qwerty: true,
};
const result = window.baz.prototype.abc.qwerty;
assert.strictEqual(result, true, 'baz.prototype.abc.qwerty was not changed');
clearGlobalProps('baz');
});
test('Check if Object.prototype.<property> has been overwriten', (assert) => {
runScriptletFromTag('Object.prototype.withAds', 'false');
let result;
(() => {
const testFunc = function test() { };
testFunc.prototype.withAds = true;
result = testFunc.prototype.withAds;
testFunc();
})();
assert.strictEqual(result, false, 'Object.prototype.withAds was set to false');
});
test('Check if property chain has been created', (assert) => {
runScriptletFromTag('ads', 'noopFunc');
runScriptletFromTag('ads.videoAd', 'noopFunc');
runScriptletFromTag('ads.videoAd.loadModule', 'trueFunc');
assert.strictEqual(typeof window.ads === 'function', true, 'ads was set to function');
assert.strictEqual(typeof window.ads.videoAd === 'function', true, 'ads.videoAd was set to function');
// eslint-disable-next-line max-len
assert.strictEqual(window.ads.videoAd.loadModule(), true, 'ads.videoAd.loadModule was set to function which returns true');
});
test('Test for reassignment 1', (assert) => {
runScriptletFromTag('zxcv.test.bar.qw', 'trueFunc');
const funcOne = () => 1;
window.zxcv = {};
// Reassign
window.zxcv = {};
window.zxcv.mnb = true;
window.zxcv.test = {
bar: {
qw: funcOne,
},
};
const result = window.zxcv.test.bar.qw();
assert.strictEqual(window.zxcv.mnb, true, 'redefined correctly');
assert.strictEqual(result, true, 'redefined correctly by scriptlet');
clearGlobalProps('zxcv');
});
test('Test for reassignment 2', (assert) => {
runScriptletFromTag('WO.adblock.useAdblocker', 'false');
window.WO = window.WO || {};
window.WO.strings = window.WO.strings || {};
// Reassign
window.WO = window.WO || {};
window.WO.strings = window.WO.strings || {};
window.WO.test = 1;
window.WO.adblock = {
useAdblocker: true,
};
assert.strictEqual(window.WO.test, 1, 'WO.test redefined correctly');
assert.strictEqual(window.WO.adblock.useAdblocker, false, 'WO.adblock.useAdblocker set to false by scriptlet');
clearGlobalProps('WO');
});
test('Should not set', (assert) => {
runScriptletFromTag('something.start.stop', 'false');
window.something = window.something || {};
assert.strictEqual(window.something.start, undefined, 'something.start was not set');
clearGlobalProps('something');
});
}