Ubuntu

­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ /** * Advanced iframe pro external workaround file v7.6.x * Created: 2018-10-25 20:25:55 */ /* jslint devel: true, evil: false */ // I added this for IOS debugging as there it is hard to find out where the problem is var extendedDebug = false; if (typeof domain_advanced_iframe === 'undefined') { // Wordpress alert("Please set domain_advanced_iframe before you include the external Workaround file."); } function trimExtraChars(text) { return text == null ? '' : text.toString().replace(/^[\s:;]+|[\s:;]+$/g, ''); } function debugExtended(message) { if (extendedDebug) { if (console && console.log) { console.log(message); } } } /** * first we modify the iframe content * only once in case the script is included several times. */ function modifyIframe() { debugExtended('calling modifyIframe'); if (!ia_already_done) { // here we add unique keys if css should be modified if (iframe_hide_elements !== '' || onload_show_element_only !== '' || iframe_content_id !== '' || iframe_content_styles !== '' || change_iframe_links !== '' || change_iframe_links_target !== '') { if (add_css_class_iframe === 'true') { var iframeHref = window.location.toString(); if (iframeHref.substr(-1) === '/') { iframeHref = iframeHref.substr(0, iframeHref.length - 1); } var lastIndex = iframeHref.lastIndexOf('/'); var result = iframeHref.substring(lastIndex + 1); var newClass = result.replace(/[^A-Za-z0-9]/g, '-'); var iframeBody = jQuery('body'); iframeBody.addClass('ai-' + newClass); if (jQuery('#ai_wrapper_div').length) { jQuery('#ai_wrapper_div').children('div').each(function(i) { jQuery(this).addClass('ai-' + newClass + '-child-' + (i+1)); }); } else { iframeBody.children('div').each(function (i) { jQuery(this).addClass('ai-' + newClass + '-child-' + (i+1)); }); } } } if (iframe_hide_elements !== '' && write_css_directly === 'false') { jQuery(iframe_hide_elements).css('display', 'none').css('width', '0').css('height','0'); } if (onload_show_element_only !== '') { aiShowElementOnly(onload_show_element_only); } if (write_css_directly === 'false' && (iframe_content_id !== '' || iframe_content_styles !== '')) { var elementArray = iframe_content_id.split('|'); var valuesArray = iframe_content_styles.split('|'); if (elementArray.length !== valuesArray.length) { alert('Configuration error: The attributes iframe_content_id and iframe_content_styles have to have the amount of value sets separated by |.'); return; } else { for (var x = 0; x < elementArray.length; ++x) { var valuesArrayPairs = trimExtraChars(valuesArray[x]).split(';'); for (var y = 0; y < valuesArrayPairs.length; ++y) { var elements = valuesArrayPairs[y].split(':'); jQuery(elementArray[x]).css(elements[0],elements[1]); } } } } // Change links targets. if (change_iframe_links !== '' || change_iframe_links_target !== '') { var linksArray = change_iframe_links.split('|'); var targetArray = change_iframe_links_target.split('|'); if (linksArray.length !== targetArray.length) { alert('Configuration error: The attributes change_iframe_links and change_iframe_links_target have to have the amount of value sets separated by |.'); return; } else { for (var z = 0; z < linksArray.length; ++z) { jQuery(linksArray[z]).attr('target', targetArray[z]); } } } // scroll to top if (scroll_to_top !== '') { jQuery(document).on( 'click', scroll_to_top, function() { aiSendScrollToTop(); }); } ia_already_done = true; // we check if we have to set the modify cookie if (modify_iframe_if_cookie) { setCookie("aiIframeModifications", "true"); } } } /** * Removes all elements from an iframe except the given one * script tags are also not removed! * * @param iframeId id of the iframe * @param showElement the id, class (jQuery syntax) of the element that should be displayed. */ function aiShowElementOnly(showElement) { debugExtended('aiShowElementOnly'); if (showElement.indexOf('|') === -1) { // this is the old way where the element is attaced directly to the body // this changes the dom tree and might breaks css rules var iframe = jQuery('body'); var selectedBox = iframe.find(showElement).clone(true,true); iframe.find('*').not(jQuery('script')).remove(); iframe.prepend(selectedBox); } else { // This is the new way where everything except the element and the elments // up to the root is hidden. This keeps the dom tree and therefore css will work like before. var showElementSplit = showElement.split('|')[0]; var element = jQuery(showElementSplit); element.siblings().hide(); var parents = element.parents(); parents.siblings().hide(); parents.css('padding', '0px').css('margin', '0px'); } } /** * Init the resize element event. */ function aiInitElementResize_advanced_iframe() { debugExtended('aiInitElementResize_advanced_iframe'); if (resize_on_element_resize !== '') { if (ia_resize_init_done_advanced_iframe === false) { /*! jQuery resize event - v1.1 - 3/14/2010 http://benalman.com/projects/jquery-resize-plugin/ Copyright (c) 2010 "Cowboy" Ben Alman Dual licensed under the MIT and GPL licenses. http://benalman.com/about/license/ */ /* jshint ignore:start */ (function(e,t,n){"$:nomunge";function c(){s=t[o](function(){r.each(function(){var t=e(this),n=t.width(),r=t.height(),i=e.data(this,a);if(i&&n!==i.w||r!==i.h){t.trigger(u,[i.w=n,i.h=r])}});c()},i[f])}var r=e([]),i=e.resize=e.extend(e.resize,{}),s,o="setTimeout",u="resize",a=u+"-special-event",f="delay",l="throttleWindow";i[f]=250;i[l]=false;e.event.special[u]={setup:function(){if(!this.nodeName){return false}if(!i[l]&&this[o]){return false}var t=e(this);r=r.add(t);e.data(this,a,{w:t.width(),h:t.height()});if(r.length===1){c()}},teardown:function(){if(!i[l]&&this[o]){return false}var t=e(this);r=r.not(t);t.removeData(a);if(!r.length){clearTimeout(s)}},add:function(t){function s(t,i,s){var o=e(this),u=e.data(this,a);if(typeof u!=="undefined"){u.w=i!==n?i:o.width();u.h=s!==n?s:o.height()}r.apply(this,arguments)}if(!i[l]&&this[o]){return false}var r;if(e.isFunction(t)){r=t;return s}else{r=t.handler;t.handler=s}}};})(jQuery,this) /* jshint ignore:end */ if (!jQuery().resize) { alert('jQuery.resize is not available. Most likely you have included jQuery AFTER the ai_external.js. Please include jQuery before the ai_external.js. If you cannot do this please disable "Resize on element resize"'); } if (resize_on_element_resize_delay !== '' && parseInt(resize_on_element_resize_delay,10) >= 50 ) { jQuery.resize.delay=resize_on_element_resize_delay; } var res_element; if (resize_on_element_resize === 'body') { res_element = jQuery('body'); } else { res_element = jQuery('body').find(resize_on_element_resize); } if (res_element.length === 0) { // show an error if null if (console && console.log) { console.log('The configuration of "resize_on_element_resize" is invalid. The specified element ' + encodeURI(resize_on_element_resize) + ' could not be found. Please check your configuration. If your content is loaded dynamically please specify onload_resize_delay with a time that is longer then your content needs to load!'); } } else { res_element.resize(function(){ ia_already_done = false; onload_resize_delay = 0; aiExecuteWorkaround_advanced_iframe(false); }); } ia_resize_init_done_advanced_iframe = true; } } } /** * The function creates a hidden iframe and determines the height of the * current page. This is then set as height parameter for the iframe * which triggers the resize function in the parent. */ function aiExecuteWorkaround_advanced_iframe(init) { debugExtended('aiExecuteWorkaround_advanced_iframe'); init = (init === undefined) ? true : init; var modificationCookieSet = false; if (modify_iframe_if_cookie) { modificationCookieSet = getCookie("aiIframeModifications") == "true"; } if (window!==window.top) { /* I'm in a frame! */ // first we modify the iframe content - only once in case the script is included several times. if (onload_resize_delay === 0 && init) { modifyIframe(); aiInitElementResize_advanced_iframe(); } if (updateIframeHeight === 'true') { var showItNow = true; var url; // add the iframe dynamically if (!usePostMessage) { url = domain_advanced_iframe + '/js/iframe_height.html'; var empty_url = 'about:blank'; var newElementStr = ''; var newElement = aiCreate(newElementStr); document.body.appendChild(newElement); } if (init) { // add a wrapper div below the body to measure - if you remove this you have to measure the height of the body! // See below for this solution. The wrapper is only created if needed createAiWrapperDiv(); // remove any margin,padding from the body because each browser handles this differently // Overflow hidden is used to avoid scrollbars that can be shown for a milisecond aiAddCss('body {margin:0px;padding:0px;overflow:hidden;}'); } var newHeight = 0; if (onload_resize_delay === 0) { // get the height of the element right below the body or a custom element - Using this solution allows that the iframe shrinks also. var wrapperElement = aiGetWrapperElement(element_to_measure); var newHeightRaw = Math.max(wrapperElement.scrollHeight, wrapperElement.offsetHeight); newHeight = parseInt(newHeightRaw,10) + element_to_measure_offset; // Get the height from the body. The problem with this solution is that an iframe can not shrink anymore. // remove everything from createAiWrapperDiv() until here for the alternative solution. // var newHeight = Math.max(document.body.scrollHeight, document.body.offsetHeight, // document.documentElement.scrollHeight, document.documentElement.offsetHeight); // This is the width - need to detect a change of the iframe width at a browser resize! iframeWidth = getIframeWidth(); } if (iframe_advanced_iframe_last_height !== newHeight) { // we only resize if we have a change // if we have a height < 10 or > 10.000 the resize is done 500ms later because it seems like the // height could not be measured correctly. And if the page is really > 10.000 it does not matter because // no one does see that the resize is done later. if (onload_resize_delay === 0 && (newHeight < 10 || newHeight > 10000)) { onload_resize_delay = 500; } if (onload_resize_delay === 0) { // 4 pixels extra are needed because of IE! (2 for Chrome) // If you still have scrollbars add a little bit more offset. if (usePostMessage) { var data = { 'aitype' : 'height', 'height' : (newHeight + 4), 'width' : iframeWidth, 'id' : iframe_id_advanced_iframe, 'data' : {} }; if (add_iframe_url_as_param === 'remote') { data.loc = encodeURIComponent(window.location); } ai_extract_additional_content(dataPostMessage, data); var json_data = JSON.stringify(data); if (debugPostMessage && console && console.log) { console.log('postMessage sent: ' + json_data + ' - targetOrigin: ' + post_message_domain ); } parent.postMessage(json_data, post_message_domain); } else { var iframe = document.getElementById('ai_hidden_iframe_advanced_iframe'); var send_data = 'height=' + (newHeight + 4) + '&width=' + iframeWidth + '&id=' + iframe_id_advanced_iframe; if (add_iframe_url_as_param === 'remote') { send_data += '&loc=' + encodeURIComponent(window.location); } iframe.src = url + '?' + send_data; } } else { showItNow = false setTimeout(function () { resizeLater_advanced_iframe(); }, onload_resize_delay); } iframe_advanced_iframe_last_height = newHeight; } // set overflow to visible again. if (keepOverflowHidden === 'false') { var timeoutRemove = onload_resize_delay + 500; window.setTimeout(removeOverflowHidden, timeoutRemove); } if (enable_responsive_iframe === 'true' && init) { // resize size after resize of window. setup is done 1 sec after first resize to avoid double resize. window.setTimeout(initResize_advanced_iframe, onload_resize_delay + 1000); } if (showItNow) { document.documentElement.style.visibility = 'visible'; } } else if (hide_page_until_loaded_external === 'true') { // only one iframe is rendered - if auto height is disabled still the parent has to be informed to show the iframe ;). if (usePostMessage) { var dataShow = { 'aitype' : 'show', 'id' : iframe_id_advanced_iframe }; ai_extract_additional_content(dataPostMessage, dataShow); var jsonDataShow = JSON.stringify(dataShow); if (debugPostMessage && console && console.log) { console.log('postMessage sent: ' + jsonDataShow + ' - targetOrigin: ' + post_message_domain ); } parent.postMessage(jsonDataShow, post_message_domain); } else { // add the iframe dynamically var urlShow = domain_advanced_iframe + '/js/iframe_show.html?id='+ iframe_id_advanced_iframe; var newElementStrShow = ''; var newElementShow = aiCreate(newElementStrShow); document.body.appendChild(newElementShow); } document.documentElement.style.visibility = 'visible'; } else { document.documentElement.style.visibility = 'visible'; } } else if (modificationCookieSet) { if (onload_resize_delay === 0) { modifyIframe(); document.documentElement.style.visibility = 'visible'; } else { setTimeout(function () { modifyIframe(); document.documentElement.style.visibility = 'visible'; }, onload_resize_delay); } } } function resizeLater_advanced_iframe() { debugExtended('resizeLater_advanced_iframe'); // We also modify the iframe later as if the resize is delayed because of dynamic data // most of the time the elements that should be modified also are loaded dynamically if (onload_resize_delay !== 0) { modifyIframe(); aiInitElementResize_advanced_iframe(); } var url = domain_advanced_iframe + '/js/iframe_height.html'; var wrapperElement = aiGetWrapperElement(element_to_measure); var newHeightRaw = Math.max(wrapperElement.scrollHeight, wrapperElement.offsetHeight); var newHeight = parseInt(newHeightRaw,10) + element_to_measure_offset; var iframeWidth = getIframeWidth(); iframe_advanced_iframe_last_height = newHeight; if (newHeight > 10) { // Only resize if the height is > 10 if (usePostMessage) { var data = { 'aitype' : 'height', 'height' : (newHeight + 4), 'width' : iframeWidth, 'id' : iframe_id_advanced_iframe, 'data' : {} }; if (add_iframe_url_as_param === 'remote') { data.loc = encodeURIComponent(window.location); } ai_extract_additional_content(dataPostMessage, data); var json_data = JSON.stringify(data); if (debugPostMessage && console && console.log) { console.log('postMessage sent: ' + json_data + ' - targetOrigin: ' + post_message_domain); } parent.postMessage(json_data, post_message_domain); } else { var iframe = document.getElementById('ai_hidden_iframe_advanced_iframe'); var send_data = 'height=' + (newHeight + 4) + '&width=' + iframeWidth + '&id=' + iframe_id_advanced_iframe; if (add_iframe_url_as_param === 'remote') { send_data += '&loc=' + encodeURIComponent(window.location); } iframe.src = url + '?' + send_data; } if (enable_responsive_iframe === 'true') { // this is the width - need to detect a change of the iframe width at a browser resize! iframeWidth = getIframeWidth(); } } else { if (debugPostMessage && console && console.log) { console.log('Advanced iframe configuration problem: The height of the page cannot be detected with the current settings. Please check the documentation of "element_to_measure" how to define an alternative element to detect the height. Also you can try to add the style overflow:hidden to this element in your html or with the plugin dynamically.'); } } document.documentElement.style.visibility = 'visible'; } /** * Remove the overflow:hidden from the body which * what avoiding scrollbars during resize. */ function removeOverflowHidden() { document.body.style.overflow='auto'; } /** * Gets the text length from text nodes. For other nodes a dummy length is returned * browser do add empty text nodes between elements which should return a length * of 0 because they should not be counted. */ function getTextLength( obj ) { var value = obj.textContent ? obj.textContent : 'NO_TEXT'; return value.trim().length; } /** * Creates a wrapper div if needed. * It is not created if the body has only one single div below the body. * childNdes.length has to be > 2 because the iframe is already attached! */ function createAiWrapperDiv() { debugExtended('createAiWrapperDiv'); var countElements = 0; // Count tags which are not empty text nodes, no script and no iframe tags // because only if we have more than 1 of this tags a wrapper div is needed for (var i = 0; i < document.body.childNodes.length; ++i) { var nodeName = document.body.childNodes[i].nodeName.toLowerCase(); var nodeLength = getTextLength(document.body.childNodes[i]); if ( nodeLength !== 0 && nodeName !== 'script' && nodeName !== 'iframe') { countElements++; } } if (countElements > 1) { var div = document.createElement('div'); div.id = 'ai_wrapper_div'; // Move the body's children into this wrapper while (document.body.firstChild) { div.appendChild(document.body.firstChild); } // Append the wrapper to the body document.body.appendChild(div); // set the style div.style.cssText = 'margin:0px;padding:0px;border: none;overflow: hidden;' + additional_styles_wrapper_div; // If we have a wrapper we set this as default if (element_to_measure === 'default') { element_to_measure = '#' + div.id; } } } /** * Creates a new dom fragment from a string */ function aiCreate(htmlStr) { var frag = document.createDocumentFragment(), temp = document.createElement('div'); temp.innerHTML = htmlStr; while (temp.firstChild) { frag.appendChild(temp.firstChild); } return frag; } function getIframeWidth() { debugExtended('getIframeWidth'); var wrapperElement = aiGetWrapperElement(element_to_measure); var newWidthRaw = Math.max(wrapperElement.scrollWidth, wrapperElement.offsetWidth); // we have a width set and no max-width! var directWidth = jQuery(wrapperElement).css('width'); if (typeof directWidth !== typeof undefined && directWidth !== false) { var maxWidth = jQuery(wrapperElement).css('max-width'); if (!(typeof maxWidth !== typeof undefined && maxWidth !== 'none')) { newWidthRaw = directWidth; } } return parseInt(newWidthRaw,10); } function initResize_advanced_iframe() { debugExtended('initResize_advanced_iframe'); // resize the iframe only when the width changes! jQuery(window).resize(function() { if (enable_responsive_iframe === 'true') { if (iframeWidth !== getIframeWidth()) { iframeWidth = getIframeWidth(); // hide the overflow if not keept if (keepOverflowHidden === 'false') { document.body.style.overflow='hidden'; } ia_already_done = false; onload_resize_delay = 0; aiExecuteWorkaround_advanced_iframe(false); // set overflow to visible again. if (keepOverflowHidden === 'false') { window.setTimeout(removeOverflowHidden,500); } } } }); } function setCookie(name,value) { document.cookie = name + "=" + (value || "") + "; path=/"; } function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return ''; } /** * Adds a css style to the head */ function aiAddCss(cssCode) { var styleElement = document.createElement('style'); styleElement.type = 'text/css'; if (styleElement.styleSheet) { styleElement.styleSheet.cssText = cssCode; } else { styleElement.appendChild(document.createTextNode(cssCode)); } document.getElementsByTagName('head')[0].appendChild(styleElement); } if(typeof String.prototype.trim !== 'function') { String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }; } /** * Helper function without jQuery to add a onload event * even if there is already one attached. */ function addOnloadEvent(fnc){ debugExtended('addOnloadEvent'); if ( typeof window.addEventListener !== 'undefined' ) { window.addEventListener( 'load', fnc, false ); } else if ( typeof window.attachEvent !== 'undefined' ) { window.attachEvent( 'onload', fnc ); } else { if ( window.onload !== null ) { var oldOnload = window.onload; window.onload = function ( e ) { oldOnload( e ); window[fnc](); }; } else { window.onload = fnc; } } } function aiGetUrlParameter( name ) { name = name.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]'); var regexS = '[\\?&]'+name+'=([^&#]*)'; var regex = new RegExp( regexS ); var results = regex.exec( window.location.href ); if( results == null ) { return ''; } else { var allowedChars = new RegExp('^[a-zA-Z0-9_\-]+$'); if (!allowedChars.test(results[1])) { return ''; } return results[1]; } } /** * Gets the first element or the element you define at element_to_measure * Either by id if no # and . is found or by jquery otherwise. */ function aiGetWrapperElement(element_to_measure) { var element; if (element_to_measure === 'default') { element = document.body.children[0]; } else { if (element_to_measure.indexOf('#') > -1 || element_to_measure.indexOf('.') > -1) { element = jQuery(element_to_measure)[0]; } else { element = document.getElementById(element_to_measure); } } // show an error if null and set the default again. if (element == null || element.length === 0) { if (console && console.log) { console.log('The configuration of "element_to_measure" is invalid. The specified element ' + encodeURI(element_to_measure) + ' could not be found. Please check your configuration. As fallback "default" is used which measures the first child of the body.'); } element = document.body.children[0]; } // margins top and bottom are set to 0 because the ai wrapper is not always used. element.style.marginTop = element.style.marginBottom = 0; return element; } function writeCssDirectly() { debugExtended('writeCssDirectly'); var css_output = ''; if (iframe_hide_elements !== '') { css_output += iframe_hide_elements + '{ display: none !important; width:0px; height:0px }'; } if (iframe_content_id !== '' || iframe_content_styles !== '') { var elementArray = iframe_content_id.split('|'); var valuesArray = iframe_content_styles.split('|'); if (elementArray.length !== valuesArray.length) { alert('Configuration error: The attributes iframe_content_id and iframe_content_styles have to have the amount of value sets separated by |.'); return; } else { for (var x = 0; x < elementArray.length; ++x) { css_output += elementArray[x] + '{'; css_output += trimExtraChars(valuesArray[x]); css_output += '}'; } } } if (css_output !== '') { document.write('"); //} } } var ia_already_done = false; } // add the aiUpdateIframeHeight to the onload of the site. addOnloadEvent(aiExecuteWorkaround_advanced_iframe); if (write_css_directly === 'true' && (window!==window.top || modificationCookieSet)) { writeCssDirectly(); } if (additional_css_file_iframe !== '' && (window!==window.top || modificationCookieSet)) { document.write(''); } if (additional_js_file_iframe !== '' && (window!==window.top || modificationCookieSet)) { document.write(''); } if (additional_js_iframe !== '' && (window!==window.top || modificationCookieSet)) { document.write('