Skip to content

Instantly share code, notes, and snippets.

@dmjcomdem
Last active March 26, 2021 08:20
Show Gist options
  • Select an option

  • Save dmjcomdem/4d2e592ae3f23f60ab40d508773d16c3 to your computer and use it in GitHub Desktop.

Select an option

Save dmjcomdem/4d2e592ae3f23f60ab40d508773d16c3 to your computer and use it in GitHub Desktop.

Revisions

  1. dmjcomdem revised this gist Mar 26, 2021. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions addserver-observer.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    console.log(123);

    // prettier-ignore
    (function() {
    function mutationObserver(
  2. dmjcomdem revised this gist Mar 26, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions addserver-observer.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    console.log(123);

    // prettier-ignore
    (function() {
    function mutationObserver(
  3. dmjcomdem revised this gist Mar 26, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions addserver-observer.js
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,8 @@
    observer.observe(node, options);
    return observer.disconnect;
    }

    console.log(mutationObserver)

    function resizeHandler(node) {
    const height = node.firstChild.contentDocument.body.firstChild.offsetHeight;
  4. dmjcomdem created this gist Mar 26, 2021.
    57 changes: 57 additions & 0 deletions addserver-observer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    // prettier-ignore
    (function() {
    function mutationObserver(
    node,
    callback,
    options = {
    childList: true,
    subtree: true,
    attributes: true,
    attributeOldValue: false,
    characterDataOldValue: false
    }
    ) {
    const observer = new MutationObserver(function([mutation]) {
    callback(mutation);
    });
    observer.observe(node, options);
    return observer.disconnect;
    }

    function resizeHandler(node) {
    const height = node.firstChild.contentDocument.body.firstChild.offsetHeight;
    node.style.width = '100%';
    node.style.height = height + 'px';
    }

    function autoResizeIframe(wrapperIframe) {
    mutationObserver(wrapperIframe, function(mutation) {
    resizeHandler(mutation.target)
    window.addEventListener('resize', () => resizeHandler(mutation.target));
    });
    }

    mutationObserver(document.body, function(mutation) {
    if (mutation.target.tagName === 'IAE-TAG') {
    const iframe = mutation.addedNodes[0];

    if (iframe instanceof HTMLIFrameElement) {
    autoResizeIframe(mutation.target);

    console.log(iframe);

    window.addEventListener('message', e => {
    if (e.data && e.data.startsWith('adserver-content')) {
    const data = JSON.parse(e.data.replace(/^adserver-content/g, ''));
    if (data.event === 'addToBasket') {
    setTimeout(() => {
    const result = { event: 'addToBasket', message: 'Отправленно в Iframe' };
    iframe.contentWindow.postMessage('adserver-content' + JSON.stringify(result),'*');
    }, 2000);
    }
    }
    });
    }
    }
    });
    })();