Auto-select OGame account

Redirect to current Ogame account if only one is available

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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();