src/js/iframe_embed/embedder.js
author ymh <ymh.work@gmail.com>
Sat, 19 Oct 2024 01:34:55 +0200
changeset 1075 92cb33eb7a75
parent 1072 ac1eacb3aa33
permissions -rw-r--r--
Preload image to avoid image flicker on play

/* This piece of code is directly requested by the page the player is embedded
   on. It creates the iframe the player is embedded in and it reflects changes
   to the iframe url in the page url.   
*/

const iFrameUpdater = function(_frameId) {
    
    var _frame = document.getElementById(_frameId),
        _blocked = false,
        _updater = function() {
            _blocked = true;
            window.setTimeout(function() {
                _blocked = false;
            }, 1000);
            _frame.contentWindow.postMessage(document.location.hash, "*");
        };
    
    window.onhashchange = _updater;
    
    window.addEventListener('message', function(_e) {
        if (/^#/.test(_e.data) && !_blocked) {
            if (typeof window.history !== "undefined" && typeof window.history.replaceState !== "undefined") {
                window.history.replaceState({}, "", _e.data);
            } else {
                document.location.hash = _e.data;
            }
        }
    });
    
    window.setTimeout(_updater, 2000);
    
};

export default iFrameUpdater;