const id = 140; const uid = function(){ return Date.now().toString(36) + Math.random().toString(36).substr(2); } var lib_token = localStorage.getItem("liberium-token"); console.log("lib-token", lib_token); if (lib_token === null) { lib_token = uid(); console.log("new lib-token", lib_token); localStorage.setItem("liberium-token", lib_token); } (() => { let oldPushState = history.pushState; history.pushState = function pushState() { let ret = oldPushState.apply(this, arguments); window.dispatchEvent(new Event('pushstate')); window.dispatchEvent(new Event('locationchange')); return ret; }; let oldReplaceState = history.replaceState; history.replaceState = function replaceState() { let ret = oldReplaceState.apply(this, arguments); window.dispatchEvent(new Event('replacestate')); window.dispatchEvent(new Event('locationchange')); return ret; }; window.addEventListener('popstate', () => { window.dispatchEvent(new Event('locationchange')); }); })(); function send_hit() { const currentUrl = window.location.href; console.log("Liberium pixel reached"); console.log(currentUrl); var url = 'https://liberium.net/adserver/pixel/hit?id=' + id + '&lib_token=' + lib_token + '&referer=' + encodeURIComponent(currentUrl); var img = document.createElement('img'); img.src = url; document.getElementById('body').appendChild(img); } send_hit(); var sending_hit = false; window.addEventListener('locationchange', function () { if (sending_hit) return; sending_hit = true; console.log('location changed!'); send_hit(); setTimeout(function(){sending_hit=false},100); }); window.addEventListener('hashchange', function() { if (sending_hit) return; sending_hit = true; console.log('location changed!'); send_hit(); setTimeout(function(){sending_hit=false},100); }); window.addEventListener("popstate", function() { if (sending_hit) return; sending_hit = true; console.log('location changed!'); send_hit(); setTimeout(function(){sending_hit=false},100); }); let pathname = location.pathname; window.addEventListener("click", function() { if (location.pathname != pathname) { if (sending_hit) return; sending_hit = true; console.log('location changed!'); pathname = location.pathname; console.log('clicked pathname changed!'); setTimeout( function(){ send_hit(); },100); setTimeout(function(){sending_hit=false},100); } });