@@ -21,41 +21,60 @@ import {loadScript, validateData} from '#3p/3p';
21
21
* @param {!Object } data
22
22
*/
23
23
export function adskeeper ( global , data ) {
24
- validateData ( data , [ 'publisher' , 'widget' , 'container' ] , [ 'url' , 'options' ] ) ;
25
-
26
- const scriptRoot = global . document . createElement ( 'div' ) ;
27
- scriptRoot . id = data . container ;
28
-
29
- global . document . body . appendChild ( scriptRoot ) ;
30
-
31
- /**
32
- * Returns path for provided js filename
33
- * @param {string } publisher js filename
34
- * @return {string } Path to provided filename.
35
- */
36
- function getResourceFilePath ( publisher ) {
37
- const publisherStr = publisher . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) ;
38
- return `${ publisherStr [ 0 ] } /${ publisherStr [ 1 ] } ` ;
39
- }
40
-
41
- const url =
42
- `https://jsc.adskeeper.com/${ getResourceFilePath ( data . publisher ) } /` +
43
- `${ encodeURIComponent ( data . publisher ) } .` +
44
- `${ encodeURIComponent ( data . widget ) } .js` ;
24
+ validateData (
25
+ data ,
26
+ [ [ 'publisher' , 'website' ] , [ 'container' , 'website' ] , 'widget' ] ,
27
+ [ 'url' , 'options' ]
28
+ ) ;
45
29
46
30
global . uniqId = (
47
31
'00000' + Math . round ( Math . random ( ) * 100000 ) . toString ( 16 )
48
32
) . slice ( - 5 ) ;
49
- global [ 'ampOptions' + data . widget + '_' + global . uniqId ] = data . options ;
33
+ window [ 'ampOptions' + data . widget + '_' + global . uniqId ] = data . options ;
50
34
51
35
global . context . observeIntersection ( function ( changes ) {
52
36
/** @type {!Array } */ ( changes ) . forEach ( function ( c ) {
53
- global [ 'intersectionRect' + data . widget + '_' + global . uniqId ] =
37
+ window [ 'intersectionRect' + data . widget + '_' + global . uniqId ] =
54
38
c . intersectionRect ;
55
- global [ 'boundingClientRect' + data . widget + '_' + global . uniqId ] =
39
+ window [ 'boundingClientRect' + data . widget + '_' + global . uniqId ] =
56
40
c . boundingClientRect ;
57
41
} ) ;
58
42
} ) ;
59
43
60
- loadScript ( global , data . url || url ) ;
44
+ if ( data . website ) {
45
+ const widgetContainer = document . createElement ( 'div' ) ;
46
+ widgetContainer . dataset . type = '_mgwidget' ;
47
+ widgetContainer . dataset . widgetId = data . widget ;
48
+ document . body . appendChild ( widgetContainer ) ;
49
+
50
+ const url =
51
+ `https://jsc.adskeeper.com/site/` +
52
+ `${ encodeURIComponent ( data . website ) } .js?t=` +
53
+ Math . floor ( Date . now ( ) / 36e5 ) ;
54
+
55
+ loadScript ( global , data . url || url ) ;
56
+ } else {
57
+ const scriptRoot = document . createElement ( 'div' ) ;
58
+ scriptRoot . id = data . container ;
59
+
60
+ document . body . appendChild ( scriptRoot ) ;
61
+
62
+ /**
63
+ * Returns path for provided js filename
64
+ * @param {string } publisher js filename
65
+ * @return {string } Path to provided filename.
66
+ */
67
+ function getResourceFilePath ( publisher ) {
68
+ const publisherStr = publisher . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) ;
69
+ return `${ publisherStr [ 0 ] } /${ publisherStr [ 1 ] } ` ;
70
+ }
71
+
72
+ const url =
73
+ `https://jsc.adskeeper.com/${ getResourceFilePath ( data . publisher ) } /` +
74
+ `${ encodeURIComponent ( data . publisher ) } .` +
75
+ `${ encodeURIComponent ( data . widget ) } .js?t=` +
76
+ Math . floor ( Date . now ( ) / 36e5 ) ;
77
+
78
+ loadScript ( global , data . url || url ) ;
79
+ }
61
80
}
0 commit comments