Origin helper

Open and get permalink to user profiles

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Origin helper
// @namespace    BurstSnow
// @version      0.1
// @description  Open and get permalink to user profiles
// @author       You
// @match        https://www.origin.com/*
// @run-at       document-start
// @grant        GM_registerMenuCommand
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    function openUserId(userId) {
        const userEncodeUrl = `https://api1.origin.com/gifting/idobfuscate/users/${userId}/encodePair`;
        const accessTokenUrl = 'https://accounts.ea.com/connect/auth?client_id=ORIGIN_JS_SDK&response_type=token&redirect_uri=nucleus:rest&prompt=none&release_type=prod';

        GM_xmlhttpRequest({
            method: "GET",
            url: accessTokenUrl,
            onload: function(response) {
                const payload = JSON.parse(response.responseText);
                const token = payload.access_token;

                GM_xmlhttpRequest({
                    method: "GET",
                    url: userEncodeUrl,
                    headers: {
                        'AuthToken': token
                    },
                    onload: function(response) {
                        const payload = JSON.parse(response.responseText);
                        const encryptedId = payload.id;

                        document.location.replace(`https://www.origin.com/profile/user/${encryptedId}/achievements`);
                    }
                })
            }
        });
    }

    function decryptUserId(done) {
        // https://www.origin.com/deu/en-us/profile/user/F8HjoZfRTkyX_TKu3FB1Cw--
        const extracted = /\/user\/([^/]+).*$/.exec(document.location.href);
        if (extracted === null) {
            alert("Error: can't extract the encrypted user id.\nAre you really on a profile page?");
            return;
        }


        const encodedId = extracted[1];
        const userDecodeUrl = `https://api1.origin.com/gifting/idobfuscate/users/${encodedId}/decodePair`;

        const accessTokenUrl = 'https://accounts.ea.com/connect/auth?client_id=ORIGIN_JS_SDK&response_type=token&redirect_uri=nucleus:rest&prompt=none&release_type=prod';

        return GM_xmlhttpRequest({
            method: "GET",
            url: accessTokenUrl,
            onload: function(response) {
                const payload = JSON.parse(response.responseText);
                const token = payload.access_token;

                return GM_xmlhttpRequest({
                    method: "GET",
                    url: userDecodeUrl,
                    headers: {
                        'AuthToken': token
                    },
                    onload: function(response) {
                        const payload = JSON.parse(response.responseText);
                        const userId = payload.id;

                        done(userId);
                    }
                })
            }
        });
    }

    function promptOpenUserId() {
        const userId = prompt('Input user id here (e.g. 1011393993169)');
        openUserId(userId);
    }

    function alertUserId() {
        return decryptUserId(userId => alert(`https://www.origin.com/profile/achievements?userId=${userId}`));
    }

    function maybeRedirectToUser() {
        // https://www.origin.com/profile/achievements?userId=1003020580074
        const extracted = /(?:\?|&)userId=(\d+)/.exec(document.location.href);

        if (extracted === null) {
            return;
        }

        const userId = extracted[1];
        openUserId(userId);
    }


    GM_registerMenuCommand("Open Origin profile from userId", promptOpenUserId, "o");
    GM_registerMenuCommand("Get permalink of Origin profile", alertUserId, "g");

    maybeRedirectToUser();

})();