Auto-select OGame account

Redirect to current Ogame account if only one is available

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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();