BF1Tracker自动获取玩家名

自动获取玩家名

// ==UserScript==
// @name         BF1Tracker自动获取玩家名
// @version      2024-03-30
// @description  自动获取玩家名
// @author       bilibili22
// @match        https://battlefieldtracker.com/bf1/*
// @icon         https://trackercdn.com/static-files/trackergg/production/dist/client/assets/0r7m9y2i.png
// @grant        GM_xmlhttpRequest
// @connect      ea-api.2788.pro
// @namespace https://greasyfork.org/users/1281680
// ==/UserScript==

(function() {
    'use strict';
    const xhrOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        const xhr = this;
        if (arguments[1].startsWith('https://api.tracker.gg/api/v2/bf1/standard/matches/')) {
            const getter = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'responseText').get;
            Object.defineProperty(xhr, 'responseText', {
                get: () => {
                    let res = getter.call(xhr);
                    res = JSON.parse(res)
                    const ids = []
                    res.data.segments.filter(data => data.type === "player" && data.metadata.playerName === "Unknown").forEach(player => { player.metadata.playerName = `#${player.attributes.playerId}`; ids.push(+player.attributes.playerId) });
                    res = JSON.stringify(res)

                    if (ids.length) GM_xmlhttpRequest({
                        url: "https://ea-api.2788.pro/players?" + ids.map(id => "personaId="+id).join("&"),
                        onload(xhr) {
                            const players = JSON.parse(xhr.responseText)
                            const div = document.getElementsByClassName("main")[1]
                            if (div) {
                                players.forEach(player => { div.innerHTML = div.innerHTML.replace(`href="/bf1/profile/origin/" class="name">#${player.personaId}`, `href="/bf1/profile/origin/${player.name}" class="name">${player.name}`) })
                            } else {
                                window.onload = function () {
                                    const div = document.getElementsByClassName("main")[1]
                                    players.forEach(player => { div.innerHTML = div.innerHTML.replace(`href="/bf1/profile/origin/" class="name">#${player.personaId}`, `href="/bf1/profile/origin/${player.name}" class="name">${player.name}`) })
                                }
                            }
                        }
                    });
                    return res;
                }
            });
        }
        return xhrOpen.apply(xhr, arguments);
    };
})();