PluginProbe
Embed Privacy / 1.1.3
Embed Privacy v1.1.3
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / assets / js / embed-privacy.js

embed-privacy.js in Embed Privacy 1.1.3, at assets/js/embed-privacy.js

143 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Embed Privacy JavaScript functions.
3 *
4 * @author Epiphyt
5 * @license GPL2
6 * @package epiphyt\Embed_Privacy
7 */
8 document.addEventListener( 'DOMContentLoaded', function() {
9 var overlays = document.querySelectorAll( '.embed-privacy-overlay' );
10 var labels = document.querySelectorAll( '.embed-privacy-inner .embed-privacy-label' );
11
12 for ( var i = 0; i < overlays.length; i++ ) {
13 overlays[ i ].addEventListener( 'click', function( event ) {
14 var current_target = event.currentTarget;
15 var embed_content = current_target.nextElementSibling;
16
17 // hide the embed overlay
18 current_target.style.display = 'none';
19 // get stored content from JavaScript
20 var embed_object = JSON.parse( window[ '_' + current_target.parentNode.id ] );
21
22 embed_content.innerHTML = htmlentities_decode( embed_object.embed );
23
24 // get all script tags inside the embed
25 var script_tags = embed_content.querySelectorAll( 'script' );
26
27 // insert every script tag inside the embed as a new script
28 // to execute it
29 for ( var n = 0; n < script_tags.length; n++ ) {
30 var element = document.createElement( 'script' );
31
32 if ( script_tags[ n ].src ) {
33 // if script tag has a src attribute
34 element.src = script_tags[ n ].src;
35 }
36 else {
37 // if script tag has content
38 element.innerHTML = script_tags[ n ].innerHTML;
39 }
40
41 // append it to body
42 embed_content.appendChild( element );
43 }
44 } )
45 }
46
47 for ( var i = 0; i < labels.length; i++ ) {
48 labels[ i ].addEventListener( 'click', function( event ) {
49 // don't trigger the overlays click
50 event.stopPropagation();
51
52 var current_target = event.currentTarget;
53 var checkbox_id = current_target.getAttribute( 'for' );
54 var embed_provider = current_target.getAttribute( 'data-embed-provider' );
55 var cookie = ( get_cookie( 'embed-privacy' ) ? JSON.parse( get_cookie( 'embed-privacy' ) ) : '' );
56
57 if ( document.getElementById( checkbox_id ).checked ) {
58 // add|update the cookie's value
59 if ( cookie !== null && Object.keys( cookie ).length !== 0 && cookie.constructor === Object ) {
60 cookie[ embed_provider ] = true;
61
62 set_cookie( 'embed-privacy', JSON.stringify( cookie ) );
63 }
64 else {
65 set_cookie( 'embed-privacy', '{"' + embed_provider + '":true}', 365 );
66 }
67 }
68 else if ( cookie !== null ) {
69 delete cookie[ embed_provider ];
70
71 if ( Object.keys( cookie ).length !== 0 ) {
72 set_cookie( 'embed-privacy', JSON.stringify( cookie ), 365 );
73 }
74 else {
75 remove_cookie( 'embed-privacy' );
76 }
77 }
78 } );
79 }
80 } );
81
82 /**
83 * Get a cookie
84 *
85 * @link https://stackoverflow.com/a/24103596/3461955
86 *
87 * @param {String} name The name of the cookie
88 */
89 function get_cookie( name ) {
90 var nameEQ = name + '=';
91 var ca = document.cookie.split( ';' );
92 for ( var i = 0; i < ca.length; i++ ) {
93 var c = ca[ i ];
94 while ( c.charAt( 0 ) == ' ' ) c = c.substring( 1, c.length );
95 if ( c.indexOf( nameEQ ) == 0 ) return c.substring( nameEQ.length, c.length );
96 }
97 return null;
98 }
99
100 /**
101 * Decode a string with HTML entities.
102 *
103 * @param {string} content The content to decode
104 * @return {string} The decoded content
105 */
106 function htmlentities_decode( content ) {
107 var textarea = document.createElement( 'textarea' );
108
109 textarea.innerHTML = content;
110
111 return textarea.value;
112 }
113
114 /**
115 * Remove a cookie
116 *
117 * @link https://stackoverflow.com/a/24103596/3461955
118 *
119 * @param {String} name The name of the cookie
120 */
121 function remove_cookie( name ) {
122 document.cookie = name + '=; expires=0; path=/';
123 }
124
125 /**
126 * Set a cookie
127 *
128 * @link https://stackoverflow.com/a/24103596/3461955
129 *
130 * @param {String} name The name of the cookie
131 * @param {String} value The value of the cookie
132 * @param {Number} days The expiration in days
133 */
134 function set_cookie( name, value, days ) {
135 var expires = '';
136 if ( days ) {
137 var date = new Date();
138 date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
139 expires = '; expires=' + date.toUTCString();
140 }
141 document.cookie = name + '=' + ( value || '' ) + expires + '; path=/';
142 }
143