/*
Wombat.js client-side rewriting engine for web archive replay
Copyright (C) 2014-2025 Webrecorder Software, Rhizome, and Contributors. Released under the GNU Affero General Public License.

This file is part of wombat.js, see https://github.com/webrecorder/wombat.js for the full source
Wombat.js is part of the Webrecorder project (https://github.com/webrecorder)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
!function(){"use strict";function t(e,o){if(!(this instanceof t))return new t(e,o);this.wombat=e,this.msgQ=[],this.mutationObz=null,this.styleTag=null,this.elemSelector="img[srcset], img[data-srcset], img[data-src], video[srcset], video[data-srcset], video[data-src], audio[srcset], audio[data-srcset], audio[data-src], picture > source[srcset], picture > source[data-srcset], picture > source[data-src], video > source[srcset], video > source[data-srcset], video > source[data-src], audio > source[srcset], audio > source[data-srcset], audio > source[data-src]",function(t){for(var e,o,i=t.__proto__||t.constructor.prototype||t.prototype,r=Object.getOwnPropertyNames(i),s=r.length,n=0;n<s;n++)o=t[e=r[n]],"constructor"!==e&&"function"==typeof o&&(t[e]=o.bind(t))}(this),this._init(o,!0)}function e(t,o){if(!(this instanceof e))return new e(t,o);this.wb_info=o,this.$wbwindow=t,this.wb_info.top_host=this.wb_info.top_host||"*",this.wb_info.wombat_opts=this.wb_info.wombat_opts||{},this.WBAutoFetchWorker=null,this.historyCB=null}t.prototype._init=function(t,e){var o=this,i=this.wombat;if("complete"===document.readyState)return this.styleTag=document.createElement("style"),this.styleTag.id="$wrStyleParser$",this.styleTag.disabled=!0,document.head.appendChild(this.styleTag),void(t.isTop?fetch(t.workerURL).then((function(t){t.text().then((function(t){var e=new Blob([t],{type:"text/javascript"});o.worker=new i.$wbwindow.Worker(URL.createObjectURL(e),{type:"classic",credentials:"include"}),o.startChecking()})).catch((t=>{console.error("Could not create the backing worker for AutoFetchWorkerProxyMode"),console.error(t)}))})):(this.worker={postMessage:function(t){t.wb_type||(t={wb_type:"aaworker",msg:t}),i.$wbwindow.top.postMessage(t,"*")},terminate:function(){}},this.startChecking()));if(e)var r=setInterval((function(){"complete"===document.readyState&&(o._init(t),clearInterval(r))}),1e3)},t.prototype.startChecking=function(){for(;this.worker&&this.msgQ.length;)this.postMessage(this.msgQ.shift());this.extractFromLocalDoc(),this.mutationObz=new MutationObserver(this.mutationCB),this.mutationObz.observe(document.documentElement,{characterData:!1,characterDataOldValue:!1,attributes:!0,attributeOldValue:!0,subtree:!0,childList:!0,attributeFilter:["src","srcset"]})},t.prototype.terminate=function(){this.worker&&this.worker.terminate()},t.prototype.justFetch=function(t){this.postMessage({type:"fetch-all",values:t})},t.prototype.fetchAsPage=function(t,e){if(t){var o={"X-Wombat-History-Page":t};if(e){var i=encodeURIComponent(e.trim());e&&(o["X-Wombat-History-Title"]=i)}var r={url:t,options:{headers:o,cache:"no-store"}};this.justFetch([r])}},t.prototype.postMessage=function(t){this.worker?this.worker.postMessage(t):this.msgQ.push(t)},t.prototype.handleMutatedStyleElem=function(t,e,o){var i,r=document.baseURI;if(o){if(!t.parentNode||"style"!==t.parentNode.localName)return;i=t.parentNode}else i=t;try{var s=this.extractMediaRules(i.sheet,r);if(s.length)return void(e.media=e.media.concat(s))}catch(t){}!o&&i.href&&e.deferred.push(this.fetchCSSAndExtract(i.href))},t.prototype.handleMutatedElem=function(t,e){var o=document.baseURI;if(t.nodeType===Node.TEXT_NODE)return this.handleMutatedStyleElem(t,e,!0);switch(t.localName){case"img":case"video":case"audio":case"source":return this.handleDomElement(t,o,e);case"style":return this.handleMutatedStyleElem(t,e);case"link":if("stylesheet"===t.rel||"preload"===t.rel&&"style"===t.as)return this.handleMutatedStyleElem(t,e)}return this.extractSrcSrcsetFrom(t,o,e)},t.prototype.mutationCB=function(t,e){for(var o={type:"values",srcset:[],src:[],media:[],deferred:[]},i=0;i<t.length;i++){var r=t[i],s=r.target;if(this.handleMutatedElem(s,o),"childList"===r.type&&r.addedNodes.length)for(var n=r.addedNodes.length,a=0;a<n;a++)this.handleMutatedElem(r.addedNodes[a],o)}if(o.deferred.length){var c=o.deferred;o.deferred=null,Promise.all(c).then(this.handleDeferredSheetResults)}(o.srcset.length||o.src.length||o.media.length)&&this.postMessage(o)},t.prototype.shouldSkipSheet=function(t){return"$wrStyleParser$"===t.id||!(!t.href||-1===t.href.indexOf(this.wombat.wb_info.proxy_magic))},t.prototype.validateSrcV=function(t){return t&&0!==t.indexOf("data:")&&0!==t.indexOf("blob:")?t:null},t.prototype.fetchCSSAndExtract=function(t){var e=location.protocol+"//"+this.wombat.wb_info.proxy_magic+"/proxy-fetch/"+t,o=this;return fetch(e).then((function(e){return e.text().then((function(e){o.styleTag.textContent=e;var i=o.extractMediaRules(o.styleTag.sheet,t);return o.styleTag.textContent="",i}))})).catch((function(t){return[]}))},t.prototype.extractMediaRules=function(t,e){var o,i=[];if(!t)return i;try{o=t.cssRules||t.rules}catch(t){return i}if(!o||0===o.length)return i;for(var r=o.length,s=t.href||e,n=0;n<r;++n){var a=o[n];a.type===CSSRule.MEDIA_RULE&&i.push({cssText:a.cssText,resolve:s})}return i},t.prototype.rwMod=function(t){switch(t.tagName){case"SOURCE":return t.parentElement&&"PICTURE"===t.parentElement.tagName?"im_":"oe_";case"IMG":return"im_"}return"oe_"},t.prototype.handleDomElement=function(t,e,o){var i=this.validateSrcV(t.src),r=i||e,s=this.rwMod(t);t.srcset&&(null==o.srcset&&(o={srcset:[]}),o.srcset.push({srcset:t.srcset,resolve:r,mod:s})),t.dataset&&t.dataset.srcset&&(null==o.srcset&&(o={srcset:[]}),o.srcset.push({srcset:t.dataset.srcset,resolve:r,mod:s})),t.dataset&&t.dataset.src&&(null==o.src&&(o.src=[]),o.src.push({src:t.dataset.src,resolve:r,mod:s})),"SOURCE"===t.tagName&&i&&(null==o.src&&(o.src=[]),o.src.push({src:i,resolve:e,mod:s}))},t.prototype.extractSrcSrcsetFrom=function(t,e,o){if(t.querySelectorAll){for(var i=t.querySelectorAll(this.elemSelector),r=i.length,s=null!=o?o:{type:"values",srcset:[],src:[]},n=0;n<r;n++)this.handleDomElement(i[n],e,s);null==o&&(s.srcset.length||s.src.length)&&this.postMessage(s)}},t.prototype.handleDeferredSheetResults=function(t){if(0!==t.length){for(var e=t.length,o=[],i=0;i<e;++i)o=o.concat(t[i]);o.length&&this.postMessage({type:"values",media:o})}},t.prototype.checkStyleSheets=function(t){for(var e=[],o=[],i=(t||document).styleSheets,r=i.length,s=0;s<r;s++){var n=i[s];if(!this.shouldSkipSheet(n))try{if(n.cssRules||n.rules){var a=this.extractMediaRules(n,t.baseURI);a.length&&(e=e.concat(a))}else null!=n.href&&o.push(this.fetchCSSAndExtract(n.href))}catch(t){null!=n.href&&o.push(this.fetchCSSAndExtract(n.href))}}e.length&&this.postMessage({type:"values",media:e}),o.length&&Promise.all(o).then(this.handleDeferredSheetResults)},t.prototype.extractFromLocalDoc=function(){this.extractSrcSrcsetFrom(this.wombat.$wbwindow.document,this.wombat.$wbwindow.document.baseURI),this.checkStyleSheets(this.wombat.$wbwindow.document)},e.prototype.initSeededRandom=function(t){this.$wbwindow.Math.seed=parseInt(t);var e=this;this.$wbwindow.Math.random=function(){return e.$wbwindow.Math.seed=(9301*e.$wbwindow.Math.seed+49297)%233280,e.$wbwindow.Math.seed/233280}},e.prototype.initCryptoRandom=function(){if(this.$wbwindow.crypto&&this.$wbwindow.Crypto){var t=this,e=function(e){for(var o=0;o<e.length;o++)e[o]=parseInt(4294967296*t.$wbwindow.Math.random());return e};this.$wbwindow.Crypto.prototype.getRandomValues=e,this.$wbwindow.crypto.getRandomValues=e}},e.prototype.initFixedRatio=function(){try{this.$wbwindow.devicePixelRatio=1}catch(t){}if(Object.defineProperty)try{Object.defineProperty(this.$wbwindow,"devicePixelRatio",{value:1,writable:!1})}catch(t){}},e.prototype.initDateOverride=function(t){if(!this.$wbwindow.__wb_Date_now){var e,o=1e3*parseInt(t),i=this.$wbwindow.Date.now()-(o-0),r=this.$wbwindow.Date,s=this.$wbwindow.Date.UTC,n=this.$wbwindow.Date.parse,a=this.$wbwindow.Date.now;this.$wbwindow.__wb_Date_now=a,this.$wbwindow.Date=(e=this.$wbwindow.Date,function(t,o,r,s,n,c,h){return void 0===t?new e(a()-i):void 0===o?new e(t):void 0===r?new e(t,o):void 0===s?new e(t,o,r):void 0===n?new e(t,o,r,s):void 0===c?new e(t,o,r,s,n):void 0===h?new e(t,o,r,s,n,c):new e(t,o,r,s,n,c,h)}),this.$wbwindow.Date.prototype=r.prototype,this.$wbwindow.Date.now=function(){return a()-i},this.$wbwindow.Date.UTC=s,this.$wbwindow.Date.parse=n,this.$wbwindow.Date.__WB_timediff=i,this.$wbwindow.Date.prototype.getTimezoneOffset=function(){return 0};var c=this.$wbwindow.Date.prototype.toString;this.$wbwindow.Date.prototype.toString=function(){return c.call(this).split(" GMT")[0]+" GMT+0000 (Coordinated Universal Time)"};var h=this.$wbwindow.Date.prototype.toTimeString;this.$wbwindow.Date.prototype.toTimeString=function(){return h.call(this).split(" GMT")[0]+" GMT+0000 (Coordinated Universal Time)"},Object.defineProperty(this.$wbwindow.Date.prototype,"constructor",{value:this.$wbwindow.Date})}},e.prototype.initDisableNotifications=function(){window.Notification&&(window.Notification.requestPermission=function(t){return t&&t("denied"),Promise.resolve("denied")});var t=function(t){t&&(t.getCurrentPosition&&(t.getCurrentPosition=function(t,e,o){e&&e({code:2,message:"not available"})}),t.watchPosition&&(t.watchPosition=function(t,e,o){e&&e({code:2,message:"not available"})}))};window.geolocation&&t(window.geolocation),window.navigator.geolocation&&t(window.navigator.geolocation)},e.prototype.initAutoFetchWorker=function(){if(this.$wbwindow.Worker){var e={isTop:this.$wbwindow.self===this.$wbwindow.top,workerURL:(this.wb_info.auto_fetch_worker_prefix||this.wb_info.static_prefix)+"autoFetchWorker.js"};if(null==this.$wbwindow.$WBAutoFetchWorker$?(this.WBAutoFetchWorker=new t(this,e),Object.defineProperty(this.$wbwindow,"$WBAutoFetchWorker$",{enumerable:!1,value:this.WBAutoFetchWorker})):this.WBAutoFetchWorker=this.$wbwindow.$WBAutoFetchWorker$,e.isTop){var o=this;this.$wbwindow.addEventListener("message",(function(t){t.data&&"aaworker"===t.data.wb_type&&o.WBAutoFetchWorker.postMessage(t.data.msg)}),!1)}}},e.prototype.initHistoryOverrides=function(){if(this.$wbwindow.self===this.$wbwindow.top){this.overrideHistoryFunc("pushState"),this.overrideHistoryFunc("replaceState");var t=this;this.$wbwindow.addEventListener("popstate",(function(e){t.historyCB&&t.historyCB(t.$wbwindow.location.href,t.$wbwindow.document.title,"popstate",e.state)}))}},e.prototype.overrideHistoryFunc=function(t){if(this.$wbwindow.history){var e=this.$wbwindow.history[t];if(e){this.$wbwindow.history["_orig_"+t]=e;var o=this,i=function(i,r,s){if(e.call(this,i,r,s),s){var n=o.$wbwindow.document.title;o.WBAutoFetchWorker&&(o.$wbwindow.setTimeout((function(){r||o.$wbwindow.document.title===n||(r=o.$wbwindow.document.title),o.WBAutoFetchWorker.fetchAsPage(o.$wbwindow.location.href,r)}),100),o.historyCB&&o.historyCB(o.$wbwindow.location.href,r,t,i))}};return this.$wbwindow.history[t]=i,this.$wbwindow.History&&this.$wbwindow.History.prototype&&(this.$wbwindow.History.prototype[t]=i),i}}},e.prototype.wombatInit=function(){this.wb_info.enable_auto_fetch&&this.wb_info.is_live&&this.initAutoFetchWorker(),this.initHistoryOverrides(),this.initSeededRandom(this.wb_info.wombat_sec),this.initCryptoRandom(),this.initFixedRatio(),this.initDateOverride(this.wb_info.wombat_sec),this.initDisableNotifications();var t=this;return{actual:!1,setHistoryCB:function(e){t.historyCB=e}}},window._WBWombat=e,window._WBWombatInit=function(t){if(this._wb_wombat&&this._wb_wombat.actual)this._wb_wombat||console.warn("_wb_wombat missing!");else{var o=new e(this,t);o.actual=!0,this._wb_wombat=o.wombatInit(),this._wb_wombat.actual=!0}}}();
