window.addEventListener('load', function() { let manager = new IFrameChildManager(); window.addEventListener("message", function(message){ manager.process(JSON.parse(message.data)); }, false); manager.init(); window.mlc_iframechildmanager = manager; }); function IFrameChildManager(){ this.parentInitialized = false; this.previousHeight = -1; this.previousCartItemCount = -1; }; IFrameChildManager.prototype.constructor = IFrameChildManager; IFrameChildManager.prototype.init = function(){ let that = this; let customData = { detail: { childInitialized: true } } window.addEventListener('resize', function() { that.onResize('inner'); /*that.onResize('outer');*/ }); window.addEventListener('load', function() { that.onResize('inner'); /*that.onResize('outer');*/ }); document.addEventListener('load', function() { that.onResize('inner'); /*that.onResize('outer');*/ }); // Select the node that will be observed for mutations //const targetNode = document.getElementById("some-id"); const targetNode = document.body; // Options for the observer (which mutations to observe) const config = { /*attributes: true,*/ childList: true, subtree: true }; const callback = (mutationList, observer) => { for (const mutation of mutationList) { if (mutation.type === "childList") { //console.log("A child node has been added or removed."); that.onResize(); } else if (mutation.type === "attributes") { //console.log(`The ${mutation.attributeName} attribute was modified.`); } } }; // Start observing the target node for configured mutations const observer = new MutationObserver(callback); observer.observe(targetNode, config); let hook = new AjaxHook(); hook.installHook(new RegExp('(.*)/shop/cart/update_json'), function(xhr){ let readyState = xhr.readyState??-1; if (readyState == 4){ let data = JSON.parse(xhr.responseText); if (data.result != null){ that.onCartContentChange(data.result.cart_quantity??0); } } }); this.bindAddToCart(); this.initializeCart(); this.initializeParent(); }; IFrameChildManager.prototype.process = function(message){ let event = message.event; switch(event){ case 'mlc:iframeparent:initialized': if (this.parentInitialized == false){ this.parentInitialized = true; this.initializeParent(); this.onResize('inner'); /*this.onResize('outer');*/ } break; } }; IFrameChildManager.prototype.initializeParent = function(){ let customData = {}; let event = { event: 'mlc:iframechild:initialized', data: customData}; let ORIGIN = '*'; window.parent.postMessage(JSON.stringify(event), (ORIGIN == 'file:' ? '*' : ORIGIN)); }; IFrameChildManager.prototype.onResize = function(content){ let newHeight = (content=='inner')?this.getWrapperInnerHeight():this.getHeight(); if (newHeight != this.previousHeight){ this.previousHeight = newHeight; let customData = { height: newHeight }; let event = { event: 'mlc:iframechild:resized', data: customData}; let ORIGIN = '*'; window.parent.postMessage(JSON.stringify(event), (ORIGIN == 'file:' ? '*' : ORIGIN)); } }; IFrameChildManager.prototype.getWrapperInnerHeight = function(){ let childs = document.querySelectorAll('#wrapwrap main > *'); let height = 0; childs.forEach(element => { height+=element.offsetHeight; }); return height; }; IFrameChildManager.prototype.getHeight = function(){ const body = document.body; const wrapper = document.querySelector('#wrapwrap main'); const html = document.documentElement; const height = Math.max(wrapper.offsetHeight, body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight); return height; }; IFrameChildManager.prototype.bindAddToCart = function(){ // const btn = document.querySelector('#add_to_cart'); // if (btn == null){ // return false; // } // btn.addEventListener('click', (event)=> { // //window.alert('click'); // return true; // }); }; IFrameChildManager.prototype.initializeCart = function(){ const httpRequest = new XMLHttpRequest(); function handler(data) { // Process the server response here. //console.log(httpRequest, data); } httpRequest.onreadystatechange = handler; httpRequest.open("POST", "/shop/cart/update_json", true); httpRequest.setRequestHeader("Content-Type", "application/json"); httpRequest.send(JSON.stringify({ id: 0, jsonrpc: "2.0", method: "call", params: { product_id: 94477, qty: 0 } })); }; IFrameChildManager.prototype.onCartContentChange = function(itemCount){ if (this.previousCartItemCount != itemCount){ this.previousCartItemCount = itemCount; let customData = { count: itemCount }; let event = { event: 'mlc:iframechild:cartitemcount', data: customData}; let ORIGIN = '*'; window.parent.postMessage(JSON.stringify(event), (ORIGIN == 'file:' ? '*' : ORIGIN)); } }; function AjaxHook(){ var hooks = {}; var initTime = new Date().getTime(); var self = this; var trim = function(strdata){ strdata.replace(/\s+/,''); }; var oldOpen = XMLHttpRequest.prototype.open; var oldSend = XMLHttpRequest.prototype.send; var convertStrToRegExp = function(str){ var targetExpr = str.substring(1,str.length-1); return new RegExp(targetExpr); }; var getHookCallbackForUrl = function(url){ var iter = 0; var urlPats = Object.keys(hooks); for (iter=0;iter