let lastSrcs;
const scriptReg = /<script.*?src=['"](.*?)['"]/gm;
async function extractNewScripts() {
  const html = await fetch("/?_t=" + Date.now()).then((res) => res.text());
  scriptReg.lastIndex = 0;
  const result = html.match(scriptReg);
  return result;
}
async function checkUpdate() {
  const newScripts = await extractNewScripts();
  if (!lastSrcs) {
    lastSrcs = newScripts;
    return false;
  }
  if (newScripts == null) return false;
  let result = false;
  if (lastSrcs.length !== newScripts.length) {
    result = true;
  }
  for (let i = 0; i < lastSrcs.length; i++) {
    if (lastSrcs[i] !== newScripts[i]) {
      result = true;
      break;
    }
  }
  lastSrcs = newScripts;
  return result;
}
function checkUpdateInterval(callbackFn, interval = 6e4) {
  setTimeout(async () => {
    const willUpdate = await checkUpdate();
    if (willUpdate) {
      callbackFn();
    }
    checkUpdateInterval(callbackFn, interval);
  }, interval);
}

export { checkUpdateInterval as default };
