Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.0 kB
0
Indexable
Never
// popup.js

document.addEventListener("DOMContentLoaded", function () {
  const logoImage = document.getElementById("logo");
  const toggleSwitch = document.getElementById("toggleButton");

  // Set the src for the logo
  logoImage.src = chrome.runtime.getURL("images/logo_128.png");

  // Retrieve extension state from storage
  chrome.storage.local.get(["enabled"], function (result) {
    const isEnabled = result.enabled || false;
    toggleSwitch.checked = isEnabled; // Set the state of the switch

    // Initialize Firebase if the extension is enabled
    if (isEnabled) {
      console.log("Extension is enabled.");
      initializeFirebase();
    } else {
      console.log("Extension is disabled.");
    }
  });

  // Toggle switch change event
  toggleSwitch.addEventListener("change", function () {
    const isEnabled = toggleSwitch.checked;
    chrome.storage.local.set({ enabled: isEnabled }, () => {
      if (isEnabled) {
        console.log("Extension is enabled.");
        initializeFirebase();
      } else {
        console.log("Extension is disabled.");
      }
    });
  });
});

function initializeFirebase() {
  // Ensure the iframe exists and is correctly loaded before posting a message
  const firebaseSandboxIframe = document.getElementById("firebase-sandbox");
  if (firebaseSandboxIframe && firebaseSandboxIframe.contentWindow) {
    firebaseSandboxIframe.contentWindow.postMessage({ type: 'init-firebase' }, '*');
  } else {
    console.error("Cannot initialize Firebase: Sandbox iframe not found or not loaded.");
  }
}

// Event listener for messages from the sandboxed iframe
window.addEventListener('message', function (event) {
  // Ensure the message is the expected type
  if (event.data && event.data.type === 'firebase-initialized') {
    console.log("Firebase has been initialized in the sandbox:", event.data.detail);
    // Perform actions now that Firebase is initialized
  }
});
Leave a Comment