/**
 * Read Roblox session and active tab URL (no manual paste).
 */
(function () {
  'use strict';

  const ROBLOX_ORIGIN = 'https://www.roblox.com';

  function getRobloxSecurityCookie() {
    return new Promise(function (resolve) {
      var urls = [
        ROBLOX_ORIGIN + '/',
        'https://roblox.com/',
        'https://create.roblox.com/'
      ];
      var i = 0;
      function tryUrl() {
        if (i >= urls.length) {
          chrome.cookies.getAll({ domain: '.roblox.com' }, function (list) {
            if (chrome.runtime.lastError || !list || !list.length) {
              resolve(null);
              return;
            }
            var found = list.filter(function (c) {
              return c.name === '.ROBLOSECURITY' && c.value;
            });
            resolve(found.length ? found[0].value : null);
          });
          return;
        }
        chrome.cookies.get({ url: urls[i++], name: '.ROBLOSECURITY' }, function (c) {
          if (!chrome.runtime.lastError && c && c.value) resolve(c.value);
          else tryUrl();
        });
      }
      tryUrl();
    });
  }

  var api = {
    getRobloxSecurityCookie: getRobloxSecurityCookie
  };
  var NAME = 'RbxRoblox';
  if (typeof globalThis !== 'undefined') globalThis[NAME] = api;
  if (typeof window !== 'undefined') window[NAME] = api;
})();
