!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).PageWatcher=e()}(this,function(){"use strict";class Emitter{constructor(){this.dispatch={}}on(t,e){const i=this.dispatch[t]||[];return i.push(e),this.dispatch[t]=i,this}emit(t,e){const i=this.dispatch[t];i&&i.length>0&&i.forEach(t=>t(e))}}async function t(t){let e="/";!1!==t&&(e+=`?_=${(new Date).getTime()}`);const i=await fetch(e);if(!i.ok)throw new Error(`Failed to fetch endpoint: ${i.statusText}`);return i.text()}const e=/<!--.*?-->/gs,i=/<script(?:\s+[^>]*)?>(.*?)<\/script\s*>/gi;function s(t){const s=t.replace(e,"");return i.lastIndex=0,s.match(i)||[]}class Poller{constructor(){this.timerId=null}start(t,e){this.timerId&&this.stop(),this.timerId=window.setInterval(t,e)}stop(){this.timerId&&(window.clearInterval(this.timerId),this.timerId=null)}}function r(t){return t instanceof Error?t:new Error(`An unknown error occurred: ${String(t)}`)}class PageWatcher extends Emitter{constructor(t={}){super(),this.options=t,this.oldScripts=[],this.poller=new Poller}async init(){try{const e=await t(this.options.disableCache);this.oldScripts=s(e),this.startPolling()}catch(t){const e=r(t);throw console.error("Page Watchdog initialization failed:",e),this.emit("error",e),e}}static async create(t={}){const e=new PageWatcher(t);return await e.init(),e}startPolling(){this.poller.start(()=>this.checkForUpdates(),this.options.timer||1e4)}async checkForUpdates(){try{const e=s(await t(this.options.disableCache));!function(t,e){const i=new Set(t),s=new Set(e);if(i.size!==s.size)return!0;for(const t of i)if(!s.has(t))return!0;return!1}(this.oldScripts,e)?this.emit("unchanged",void 0):(this.oldScripts=e,this.emit("changed",void 0))}catch(t){const e=r(t);console.error("Page Watchdog failed to check for updates:",e),this.emit("error",e)}}stop(){this.poller.stop()}}return PageWatcher});
//# sourceMappingURL=page-watchdog.umd.min.js.map
