Snippets

Oleg Korsak waavo script

Created by Oleg Korsak
(function () {
    "use strict";
    
    if (document.getElementsByTagName('waavo:iframe')) {
        function parseIframeUrl(val) {
            val = val.replace("?", "").split('&');
            for (var i = 0; i < val.length; i++) {
                var val2 = val[i].split('=');
                if (val2[0] == 'wurl') {
                    return val2[1];
                }
            }
            return '';
        }
    
        var elem = document.getElementsByTagName('waavo:iframe');
    
        for (elemi = 0; elemi < elem.length; elemi++) {
            var project = elem[elemi].getAttribute('project');
            var host = 'waavo.lt';
            var http = 'http';
            var https = true;
    
            if (elem[elemi].getAttribute('https')) {
                https = elem[elemi].getAttribute('https');
            }
            if (elem[elemi].getAttribute('host')) {
                host = elem[elemi].getAttribute('host');
            }
            var url = elem[elemi].getAttribute('url');
            if (parseIframeUrl(window.location.search)) {
                url = parseIframeUrl(window.location.search);
            }
    
            var insertTo = 'waavo-insert-iframe';
            if (elem[elemi].getAttribute('insert-iframe-id')) {
                insertTo = elem[elemi].getAttribute('insert-iframe-id');
            }
            if (url == null) {
                url = '/';
            }
    
            var iframeUrl = project + '.' + host + url;
            if (elem[elemi].getAttribute('language')) {
                if (iframeUrl.indexOf('?') != -1) {
                    iframeUrl += '&language=' + elem[elemi].getAttribute('language');
                } else {
                    iframeUrl += '?language=' + elem[elemi].getAttribute('language');
                }
            }
            if (elem[elemi].getAttribute('currency')) {
                if (iframeUrl.indexOf('?') != -1) {
                    iframeUrl += '&currency=' + elem[elemi].getAttribute('currency');
                } else {
                    iframeUrl += '?currency=' + elem[elemi].getAttribute('currency');
                }
            }
    
            if (host == 'waavo.com') {
                http = 'https';
            }
            if (https == 'false') {
                http = 'http';
            }
            document.getElementById(insertTo).innerHTML = '<iframe id="waavoiframe' + elemi + '" src="' + http + '://' + iframeUrl + '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:262px;" allowTransparency="true"></iframe><div style="clear:both"></div>';
        }
    
        function changeUrl(url) {
            url = url.replace('#', '');
            var iframe = document.getElementById('waavoiframe');
            iframe.setAttribute('src', http + '://' + project + '.' + host + url);
        }
    
        var XD = function () {
            var interval_id, attached_callback, window = this;
            return {
                receiveMessage: function (callback, source_origin) {
    
                    /* browser supports window.postMessage */
                    if (window['postMessage']) {
                        /*  bind the callback to the actual event associated with window.postMessage */
                        if (callback) {
                            attached_callback = function (e) {
                                if (( typeof source_origin === 'string' && e.origin !== source_origin) || (Object.prototype.toString.call(source_origin) === "[object Function]" && source_origin(e.origin) === !1)) {
                                    return !1;
                                }
                                callback(e);
                            };
                        }
                        if (window['addEventListener']) {
                            window[callback ? 'addEventListener' : 'removeEventListener']('message', attached_callback, !1);
                        } else {
                            window[callback ? 'attachEvent' : 'detachEvent']('onmessage', attached_callback);
                        }
                    } else {
                        /* a polling loop is started & callback is called whenever the location.hash changes */
                        interval_id && clearInterval(interval_id);
                        interval_id = null;
    
                        if (callback) {
                            interval_id = setInterval(function () {
                                var hash = document.location.hash, re = /^#?\d+&/;
                                if (hash !== last_hash && re.test(hash)) {
                                    last_hash = hash;
                                    callback({
                                        data: hash.replace(re, '')
                                    });
                                }
                            }, 100);
                        }
                    }
                }
            };
        }();
    
        XD.receiveMessage(function (data) {
            if (data.data.substr(0, 1) != '{') {
                return false;
            }
            var frames = document.getElementsByTagName('iframe');
            var elementId = null;
            for (var i = 0; i < frames.length; i++) {
                if (frames[i].contentWindow === data.source) {
                    elementId = frames[i].id;
                    break;
                }
            }
    
            if(!elementId) {
                return false;
            }
            data = eval('(' + data.data + ')');
            data = data.data;
    
            if (!data) {
                return false;
            }
    
            // updates iframe height
            if (data.height) {
                changeFrameHeight(data.height, elementId);
            }
    
            if (data.scrollToTop) {
                scrollToIframeTop();
            }
    
            if (data.url) {
                if (window.history.pushState) {
                    url = replaceUrlParam(window.location.href, 'wurl', data.url);
                    window.history.pushState('page2', 'Title', url);
                }
                scroll(0, 0);
            }
        });
    
        function changeFrameHeight(height, id) {
            document.getElementById(id).style.height = height + 'px';
        }
    
        function scrollToIframeTop() {
            document.getElementById('waavo-insert-iframe').scrollIntoView();
        }
    
        function replaceUrlParam(url, paramName, paramValue) {
            var pattern = new RegExp('\\b(' + paramName + '=).*?(&|$)');
            if (url.search(pattern) >= 0) {
                return url.replace(pattern, '$1' + paramValue + '$2');
            }
            return url + (url.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + paramValue
        }
    }
}());

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.