// MV2 background page for a tab counter, used to check the guard.
var count = 0;
var blocked = ["ads.example.com"];

chrome.browserAction.onClicked.addListener(function (tab) {
  count++;
  chrome.browserAction.setBadgeText({ text: String(count) });
  chrome.tabs.executeScript(tab.id, { code: "document.body.style.border = '4px solid red'" });
  localStorage.setItem("count", count);
});

chrome.webRequest.onBeforeRequest.addListener(
  function (details) {
    return { cancel: blocked.some((d) => details.url.includes(d)) };
  },
  { urls: ["<all_urls>"] },
  ["blocking"]
);

chrome.storage.local.get("settings", function (s) {
  chrome.runtime.onMessage.addListener(function (msg, sender, reply) {
    reply({ count: count, settings: s.settings });
  });
});

setInterval(function () {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "https://example.com/rules.json");
  xhr.send();
}, 60 * 60 * 1000);

chrome.tabs.getSelected(null, function (tab) {
  console.log(tab.url, window.navigator.userAgent);
});
