text: import handleGeneralProfile from "./handlers/generalProfile";
  import handleChatProfile from "./handlers/chatProfile";
  import handleChatMessage from "./handlers/chatMessage";
  import { storageGetAsync, compareFlatObjects } from "./utilities";
  import store from "./store";
  
  let INTERVAL;
  
  const init = (settings) => {
    if (!compareFlatObjects(settings, store.state.settings)) return;
  
    try {
      settings.profileName =
        localStorage.getItem("account_id") || document.cookie.match(/(?<=SDCUsername=)[^;]+/)[0];
    } catch (_) {
      // Can't find username; user not logged in
      return;
    }
  
    if (INTERVAL) clearInterval(INTERVAL);
  
    INTERVAL = setInterval(() => {
      // Only run when the tab is active
      if (document.visibilityState !== "visible") return;
  
      // General profile tweaks
      const generalProfiles = Array.from(
        document.querySelectorAll(
          ".sdc-column, .row > div > div[class=''], div[class^='sdc-popularity-column'], .sdct-profile"
        )
      );
  
      generalProfiles.forEach(handleGeneralProfile.bind(null, settings));
  
      // Chatroom profile tweaks
      const chatProfiles = Array.from(document.querySelectorAll(".user-item"));
      chatProfiles.forEach(handleChatProfile.bind(null, settings));
  
      // Chatroom message tweaks (limited to last 100)
      const chatMessages = Array.from(document.querySelectorAll("#messages-list > li"));
      chatMessages.slice(0).slice(-100).forEach(handleChatMessage.bind(null, settings));
    }, 750);
  };
  
  // Async settings load
  chrome.storage.onChanged.addListener(({ settings }) => init(settings.newValue));
  
  // Initial settings load
  storageGetAsync(["settings"])
    .then(({ settings = {} }) => init(settings))
    .catch((error) => console.error(error));
  