PluginProbe
Embed Privacy / 0.1
Embed Privacy v0.1
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
← All changes | assets/js/embed-privacy.js +28 -318 1.10.9 → 0.1 View file →
@@ -1,334 +1,44 @@
1 1 /**
2 2 * Embed Privacy JavaScript functions.
3 3 *
4 - * @author Epiphyt
5 - * @license GPL2
6 - * @package epiphyt\Embed_Privacy
4 + * @author Epiphyt
5 + * @license GPL2
6 + * @package epiphyt\Embed_Privacy
7 + * @version 0.1
7 8 */
8 9 document.addEventListener( 'DOMContentLoaded', function() {
9 - var checkboxes = document.querySelectorAll( '.embed-privacy-inner .embed-privacy-input' );
10 - var labels = document.querySelectorAll( '.embed-privacy-inner .embed-privacy-label' );
11 - var overlays = document.querySelectorAll( '.embed-privacy-overlay' );
12 - var overlayLinks = document.querySelectorAll( '.embed-privacy-overlay a' );
10 + var overlays = document.querySelectorAll( '.embed-overlay' );
13 11
14 12 for ( var i = 0; i < overlays.length; i++ ) {
15 13 overlays[ i ].addEventListener( 'click', function( event ) {
16 - if ( event.currentTarget.tagName !== 'INPUT' ) {
17 - overlayClick( event.currentTarget );
18 - }
19 - } );
20 -
21 - var button = overlays[ i ].parentNode.querySelector( '.embed-privacy-enable' );
22 -
23 - if ( ! button ) {
24 - continue;
25 - }
26 -
27 - button.addEventListener( 'click', function( event ) {
28 - overlayClick( event.currentTarget.parentNode.querySelector( '.embed-privacy-overlay' ) );
29 - event.currentTarget.parentNode.removeChild( event.currentTarget ); // IE11 doesn't support .remove()
30 - } );
31 - button.addEventListener( 'keypress', function( event ) {
32 - if ( event.code === 'Enter' || event.code === 'Space' ) {
33 - event.preventDefault(); // prevent space from scrolling the page
34 - overlayClick( event.currentTarget.parentNode.querySelector( '.embed-privacy-overlay' ) );
35 - event.currentTarget.parentNode.removeChild( event.currentTarget ); // IE11 doesn't support .remove()
36 - }
37 - } );
38 - }
39 -
40 - enableAlwaysActiveProviders();
41 - optOut();
42 - setMinHeight();
43 -
44 - window.addEventListener( 'resize', function() {
45 - setMinHeight();
46 - } );
47 -
48 - for ( var i = 0; i < overlayLinks.length; i++ ) {
49 - overlayLinks[ i ].addEventListener( 'click', function( event ) {
50 - // don't trigger the overlays click
51 - event.stopPropagation();
52 - } );
53 - }
54 -
55 - for ( var i = 0; i < checkboxes.length; i++ ) {
56 - checkboxes[ i ].addEventListener( 'click', function( event ) {
57 - // don't trigger the overlays click
58 - event.stopPropagation();
14 + var current_target = event.currentTarget;
15 + var embed_content = current_target.nextElementSibling;
59 16
60 - checkboxActivation( event.currentTarget );
61 - } );
62 - }
63 -
64 - for ( var i = 0; i < labels.length; i++ ) {
65 - labels[ i ].addEventListener( 'click', function( event ) {
66 - // don't trigger the overlays click
67 - event.stopPropagation();
68 - } );
69 - }
70 -
71 - /**
72 - * Clicking on a checkbox to always enable/disable an embed provider.
73 - *
74 - * @since 1.2.0
75 - *
76 - * @param {element} target Target element
77 - */
78 - function checkboxActivation( target ) {
79 - var embedProvider = target.getAttribute( 'data-embed-provider' );
80 - var cookie = ( get_cookie( 'embed-privacy' ) ? JSON.parse( get_cookie( 'embed-privacy' ) ) : '' );
81 -
82 - if ( target.checked ) {
83 - // add|update the cookie's value
84 - if ( cookie !== null && Object.keys( cookie ).length !== 0 && cookie.constructor === Object ) {
85 - cookie[ embedProvider ] = true;
86 -
87 - set_cookie( 'embed-privacy', JSON.stringify( cookie ), 365 );
88 - }
89 - else {
90 - set_cookie( 'embed-privacy', '{"' + embedProvider + '":true}', 365 );
91 - }
17 + // hide the embed overlay
18 + current_target.style.display = 'none';
19 + // remove the HTML comments from the embed content
20 + embed_content.innerHTML = embed_content.innerHTML.replace( /<!--/, '' ).replace( /-->/, '' );
92 21
93 - enableAlwaysActiveProviders();
94 - }
95 - else if ( cookie !== null ) {
96 - delete cookie[ embedProvider ];
22 + // get all script tags inside the embed
23 + var script_tags = embed_content.querySelectorAll( 'script' );
97 24
98 - if ( Object.keys( cookie ).length !== 0 ) {
99 - set_cookie( 'embed-privacy', JSON.stringify( cookie ), 365 );
100 - }
101 - else {
102 - remove_cookie( 'embed-privacy' );
103 - }
104 - }
105 - }
106 -
107 - /**
108 - * Check whether to enable an always active provider by default.
109 - *
110 - * @since 1.2.0
111 - */
112 - function enableAlwaysActiveProviders() {
113 - var cookie = ( get_cookie( 'embed-privacy' ) ? JSON.parse( get_cookie( 'embed-privacy' ) ) : '' );
114 -
115 - if ( cookie === null || ! Object.keys( cookie ).length ) {
116 - return;
117 - }
118 -
119 - var providers = Object.keys( cookie );
120 -
121 - for ( var i = 0; i < overlays.length; i++ ) {
122 - var provider = overlays[ i ].parentNode.getAttribute( 'data-embed-provider' );
123 -
124 - if ( providers.includes( provider ) ) {
125 - overlays[ i ].click();
126 - }
127 - }
128 - }
129 -
130 - /**
131 - * Get always active providers from cookie.
132 - *
133 - * @since 1.4.8
134 - *
135 - * @return {string[]} List of always active providers
136 - */
137 - function getAlwaysActiveProviders() {
138 - const cookie = ( get_cookie( 'embed-privacy' ) ? JSON.parse( get_cookie( 'embed-privacy' ) ) : '' );
139 -
140 - if ( ! cookie ) {
141 - return [];
142 - }
143 -
144 - return Object.keys( cookie );
145 - }
146 -
147 - /**
148 - * Opting in/out for embed providers.
149 - *
150 - * @since 1.2.0
151 - */
152 - function optOut() {
153 - const optOutContainer = document.querySelector( '.embed-privacy-opt-out' );
154 -
155 - if ( ! optOutContainer ) {
156 - return;
157 - }
158 -
159 - var optOutCheckboxes = optOutContainer.querySelectorAll( '.embed-privacy-opt-out-input' );
160 - const showAll = optOutContainer.getAttribute( 'data-show-all' ) === '1';
161 -
162 - if ( ! optOutCheckboxes ) {
163 - return;
164 - }
165 -
166 - const alwaysActiveProviders = getAlwaysActiveProviders();
167 -
168 - for ( var i = 0; i < optOutCheckboxes.length; i++ ) {
169 - if ( alwaysActiveProviders.indexOf( optOutCheckboxes[ i ].getAttribute( 'data-embed-provider' ) ) !== -1 ) {
170 - optOutCheckboxes[ i ].checked = true;
171 - }
172 - else if ( ! showAll ) {
173 - optOutCheckboxes[ i ].parentNode.parentNode.classList.add( 'is-hidden' );
25 + // insert every script tag inside the embed as a new script
26 + // to execute it
27 + for ( var n = 0; n < script_tags.length; n++ ) {
28 + var element = document.createElement( 'script' );
174 29
175 - continue;
176 - }
177 -
178 - optOutCheckboxes[ i ].addEventListener( 'click', function( event ) {
179 - var currentTarget = event.currentTarget;
180 -
181 - if ( ! currentTarget ) {
182 - return;
30 + if ( script_tags[ n ].src ) {
31 + // if script tag has a src attribute
32 + element.src = script_tags[ n ].src;
183 33 }
34 + else {
35 + // if script tag has content
36 + element.innerHTML = script_tags[ n ].innerHTML;
37 + }
184 38
185 - checkboxActivation( currentTarget );
186 - } );
187 - }
188 -
189 - const nonHiddenProviders = optOutContainer.querySelectorAll( '.embed-privacy-provider:not(.is-hidden)' );
190 -
191 - // remove the container completely if there is no provider to display
192 - if ( ! showAll && ! nonHiddenProviders.length ) {
193 - optOutContainer.remove();
194 - }
195 - }
196 -
197 - /**
198 - * Clicking on an overlay.
199 - *
200 - * @since 1.2.0
201 - *
202 - * @param {element} target Target element
203 - */
204 - function overlayClick( target ) {
205 - var embedContainer = target.parentNode;
206 - var embedContent = target.nextElementSibling;
207 -
208 - embedContainer.classList.remove( 'is-disabled' );
209 - embedContainer.classList.add( 'is-enabled' );
210 - // hide the embed overlay
211 - target.style.display = 'none';
212 - // get stored content from JavaScript
213 - var embedObject = JSON.parse( window[ '_' + target.parentNode.getAttribute( 'data-embed-id' ) ] );
214 -
215 - embedContent.innerHTML = htmlentities_decode( embedObject.embed );
216 -
217 - // reset wrapper inline CSS set in setMinHeight()
218 - var wrapper = embedContainer.parentNode;
219 -
220 - if ( wrapper.classList.contains( 'wp-block-embed__wrapper' ) ) {
221 - wrapper.style.removeProperty( 'height' );
222 - }
223 -
224 - // get all script tags inside the embed
225 - var scriptTags = embedContent.querySelectorAll( 'script' );
226 -
227 - // insert every script tag inside the embed as a new script
228 - // to execute it
229 - for ( var n = 0; n < scriptTags.length; n++ ) {
230 - var element = document.createElement( 'script' );
231 -
232 - if ( scriptTags[ n ].src ) {
233 - // if script tag has a src attribute
234 - element.src = scriptTags[ n ].src;
39 + // append it to body
40 + embed_content.appendChild( element );
235 41 }
236 - else {
237 - // if script tag has content
238 - element.innerHTML = scriptTags[ n ].innerHTML;
239 - }
240 -
241 - // append it to body
242 - embedContent.appendChild( element );
243 - }
244 -
245 - if ( typeof jQuery !== 'undefined' ) {
246 - const videoShortcode = jQuery( '.wp-video-shortcode' );
247 -
248 - if ( videoShortcode.length ) {
249 - videoShortcode.mediaelementplayer();
250 - }
251 - }
42 + } )
252 43 }
253 -
254 - /**
255 - * Calculate min height of the embed wrapper depending of the overlay content.
256 - */
257 - function setMinHeight() {
258 - for ( var i = 0; i < overlays.length; i++ ) {
259 - var wrapper = overlays[ i ].parentNode.parentNode;
260 -
261 - if ( ! wrapper.classList.contains( 'wp-block-embed__wrapper' ) ) {
262 - continue;
263 - }
264 -
265 - wrapper.style.removeProperty( 'height' );
266 -
267 - if ( wrapper.offsetHeight < overlays[ i ].offsetHeight ) {
268 - wrapper.style.height = overlays[ i ].offsetHeight + 'px';
269 - }
270 - }
271 - }
272 -} );
273 -
274 -/**
275 - * Get a cookie.
276 - *
277 - * @link https://stackoverflow.com/a/24103596/3461955
278 - *
279 - * @param {string} name The name of the cookie
280 - */
281 -function get_cookie( name ) {
282 - var nameEQ = name + '=';
283 - var ca = document.cookie.split( ';' );
284 - for ( var i = 0; i < ca.length; i++ ) {
285 - var c = ca[ i ];
286 - while ( c.charAt( 0 ) == ' ' ) c = c.substring( 1, c.length );
287 - if ( c.indexOf( nameEQ ) == 0 ) return c.substring( nameEQ.length, c.length );
288 - }
289 - return null;
290 -}
291 -
292 -/**
293 - * Decode a string with HTML entities.
294 - *
295 - * @param {string} content The content to decode
296 - * @return {string} The decoded content
297 - */
298 -function htmlentities_decode( content ) {
299 - var textarea = document.createElement( 'textarea' );
300 -
301 - textarea.innerHTML = content;
302 -
303 - return textarea.value;
304 -}
305 -
306 -/**
307 - * Remove a cookie.
308 - *
309 - * @link https://stackoverflow.com/a/24103596/3461955
310 - *
311 - * @param {string} name The name of the cookie
312 - */
313 -function remove_cookie( name ) {
314 - document.cookie = name + '=; expires=0; path=/';
315 -}
316 -
317 -/**
318 - * Set a cookie.
319 - *
320 - * @link https://stackoverflow.com/a/24103596/3461955
321 - *
322 - * @param {string} name The name of the cookie
323 - * @param {string} value The value of the cookie
324 - * @param {number} days The expiration in days
325 - */
326 -function set_cookie( name, value, days ) {
327 - var expires = '';
328 - if ( days ) {
329 - var date = new Date();
330 - date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
331 - expires = '; expires=' + date.toUTCString();
332 - }
333 - document.cookie = name + '=' + ( value || '' ) + expires + '; path=/';
334 -}
44 +} );