Auto-select OGame account

Redirect to current Ogame account if only one is available

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name           Auto-select OGame account
// @namespace      https://openuserjs.org/users/clemente
// @match          https://lobby.ogame.gameforge.com/*/hub
// @grant          GM_xmlhttpRequest
// @version        1.0
// @author         clemente
// @license        MIT
// @description    Redirect to current Ogame account if only one is available
// @icon           https://lobby.ogame.gameforge.com/favicon.ico
// @connect        lobby.ogame.gameforge.com
// @inject-into    content
// @noframes
// ==/UserScript==

function gm_fetch(url) {
  return new Promise((resolve, reject) => {
    GM_xmlhttpRequest({
      method: "GET",
      url: url,
      onload: function({ status, responseText }) {
        if (status < 200 && status >= 300) return reject();
        resolve(JSON.parse(responseText));
      },
      onerror: function() { reject(); },
    });
  });
}

async function redirectToAccount() {
  try {
    const accounts = await gm_fetch("https://lobby.ogame.gameforge.com/api/users/me/accounts");
    if (accounts.length > 1) return;
    const number = accounts[0].server.number;
    const language = accounts[0].server.language;

    document.location.href = `https://s${number}-${language}.ogame.gameforge.com/game/index.php?page=ingame&component=overview`;
  } catch (e) {
    // If there is an error, the user is probably not logged in
    console.error(e);
  }
}

redirectToAccount();