Falcon Chat Auto Sign-In

Autofill login and submit on Falcon Chat

// ==UserScript==
// @name         Falcon Chat Auto Sign-In
// @description  Autofill login and submit on Falcon Chat
// @match        https://chat.falconllm.tii.ae/*
// @version 0.0.1.20250622083209
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==

(function() {
  'use strict';

  const EMAIL = '[email protected]';
  const PASSWORD = 'your_password';

  let observer = null;

  const fillAndSubmit = () => {
    const emailInput = document.querySelector('input[type="email"][name="email"]');
    const passwordInput = document.querySelector('input[type="password"][name="current-password"]');
    const signInButton = Array.from(document.querySelectorAll('button[type="submit"]'))
      .find(btn => btn.textContent.trim().toLowerCase() === 'sign in');

    if (emailInput && passwordInput && signInButton) {
      emailInput.value = EMAIL;
      passwordInput.value = PASSWORD;

      emailInput.dispatchEvent(new Event('input', { bubbles: true }));
      passwordInput.dispatchEvent(new Event('input', { bubbles: true }));

      setTimeout(() => signInButton.click(), 300);

      if (observer) {
        observer.disconnect(); // Stop watching after sign-in attempt
        observer = null;
      }
    }
  };

  const startObserving = () => {
    if (observer) return; // Avoid creating multiple observers

    observer = new MutationObserver(() => {
      const form = document.querySelector('form');
      if (form) {
        fillAndSubmit();
      }
    });

    observer.observe(document, { childList: true, subtree: true });
  };

  let lastUrl = location.href;

  const observeUrlChanges = () => {
    new MutationObserver(() => {
      const currentUrl = location.href;
      if (currentUrl !== lastUrl) {
        lastUrl = currentUrl;

        if (currentUrl.includes('/auth')) {
          startObserving();
        } else if (observer) {
          observer.disconnect();
          observer = null;
        }
      }
    }).observe(document, { childList: true, subtree: true });
  };

  // Initial run
  if (location.href.includes('/auth')) {
    startObserving();
  }

  // Watch for future URL changes
  observeUrlChanges();
})();